Skip to content

Commit

Permalink
[#3135] - Added getBestLanguageIso and reorganized a few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niden committed Feb 4, 2020
1 parent 470ad10 commit c084fa0
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 125 deletions.
15 changes: 15 additions & 0 deletions phalcon/Http/Request.zep
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ class Request extends AbstractInjectionAware implements RequestInterface
return this->getBestQuality(this->getLanguages(), "language");
}

/**
* Gets best language accepted by the browser/client from
* _SERVER["HTTP_ACCEPT_LANGUAGE"] and returns the base part of it i.e.
* `en` instead of `en-US`
*/
public function getBestLanguageIso() -> string
{
var language;

let language = this->getBestLanguage(),
language = explode("-", language);

return language[0];
}

/**
* Gets most possible client IPv4 Address. This method searches in
* `$_SERVER["REMOTE_ADDR"]` and optionally in
Expand Down
45 changes: 44 additions & 1 deletion tests/unit/Http/Request/GetBestCharsetCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

namespace Phalcon\Test\Unit\Http\Request;

use Phalcon\Test\Fixtures\Traits\DiTrait;
use UnitTester;

class GetBestCharsetCest
{
use DiTrait;

/**
* Tests Phalcon\Http\Request :: getBestCharset()
*
Expand All @@ -27,6 +30,46 @@ public function httpRequestGetBestCharset(UnitTester $I)
{
$I->wantToTest('Http\Request - getBestCharset()');

$I->skipTest('Need implementation');
$store = $_SERVER ?? [];

$this->setNewFactoryDefault();
$request = $this->container->get('request');

$_SERVER = [
'HTTP_ACCEPT_CHARSET' => 'iso-8859-5,unicode-1-1;q=0.8'
];

$accept = $request->getClientCharsets();
$I->assertCount(2, $accept);


$firstAccept = $accept[0];
$I->assertEquals(
'iso-8859-5',
$firstAccept['charset']
);

$I->assertEquals(
1,
$firstAccept['quality']
);

$lastAccept = $accept[1];
$I->assertEquals(
'unicode-1-1',
$lastAccept['charset']
);

$I->assertEquals(
0.8,
$lastAccept['quality']
);

$I->assertEquals(
'iso-8859-5',
$request->getBestCharset()
);

$_SERVER = $store;
}
}
55 changes: 54 additions & 1 deletion tests/unit/Http/Request/GetBestLanguageCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

namespace Phalcon\Test\Unit\Http\Request;

use Phalcon\Test\Fixtures\Traits\DiTrait;
use UnitTester;

class GetBestLanguageCest
{
use DiTrait;

/**
* Tests Phalcon\Http\Request :: getBestLanguage()
*
Expand All @@ -27,6 +30,56 @@ public function httpRequestGetBestLanguage(UnitTester $I)
{
$I->wantToTest('Http\Request - getBestLanguage()');

$I->skipTest('Need implementation');
$store = $_SERVER ?? [];

$this->setNewFactoryDefault();
$request = $this->container->get('request');

$_SERVER = [
'HTTP_ACCEPT_LANGUAGE' => 'es,es-ar;q=0.8,en;q=0.5,en-us;q=0.3,de-de; q=0.9'
];

$accept = $request->getLanguages();
$I->assertCount(5, $accept);


$firstAccept = $accept[0];
$I->assertEquals(
'es',
$firstAccept['language']
);

$I->assertEquals(
1,
$firstAccept['quality']
);


$fourthAccept = $accept[3];
$I->assertEquals(
'en-us',
$fourthAccept['language']
);

$I->assertEquals(
0.3,
$fourthAccept['quality']
);


$lastAccept = $accept[4];
$I->assertEquals(
'de-de',
$lastAccept['language']
);

$I->assertEquals(
0.9,
$lastAccept['quality']
);

$I->assertEquals('es', $request->getBestLanguage());

$_SERVER = $store;
}
}
65 changes: 65 additions & 0 deletions tests/unit/Http/Request/GetBestLanguageIsoCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Test\Unit\Http\Request;

use Phalcon\Test\Fixtures\Traits\DiTrait;
use UnitTester;

class GetBestLanguageIsoCest
{
use DiTrait;

/**
* Tests Phalcon\Http\Request :: getBestLanguageIso()
*
* @author Phalcon Team <[email protected]>
* @since 2018-11-13
*/
public function httpRequestGetBestLanguageIso(UnitTester $I)
{
$I->wantToTest('Http\Request - getBestLanguageIso()');

$store = $_SERVER ?? [];

$this->setNewFactoryDefault();
$request = $this->container->get('request');

$_SERVER = [
'HTTP_ACCEPT_LANGUAGE' => 'es,es-ar;q=0.8'
];
$I->assertEquals('es', $request->getBestLanguageIso());

$_SERVER = [
'HTTP_ACCEPT_LANGUAGE' => 'es-ar;q=0.8'
];
$I->assertEquals('es', $request->getBestLanguageIso());

$_SERVER = [
'HTTP_ACCEPT_LANGUAGE' => 'en-US'
];
$I->assertEquals('en', $request->getBestLanguageIso());

$_SERVER = [
'HTTP_ACCEPT_LANGUAGE' => 'en'
];
$I->assertEquals('en', $request->getBestLanguageIso());

$_SERVER = [
'HTTP_ACCEPT_LANGUAGE' => ''
];
$I->assertEquals('', $request->getBestLanguageIso());

$_SERVER = $store;
}
}
34 changes: 33 additions & 1 deletion tests/unit/Http/Request/GetClientAddressCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

namespace Phalcon\Test\Unit\Http\Request;

use Phalcon\Test\Fixtures\Traits\DiTrait;
use UnitTester;

class GetClientAddressCest
{
use DiTrait;

/**
* Tests Phalcon\Http\Request :: getClientAddress()
*
Expand All @@ -27,6 +30,35 @@ public function httpRequestGetClientAddress(UnitTester $I)
{
$I->wantToTest('Http\Request - getClientAddress()');

$I->skipTest('Need implementation');
$store = $_SERVER ?? [];

$this->setNewFactoryDefault();
$request = $this->container->get('request');

$_SERVER = [
'REMOTE_ADDR' => '192.168.0.1',
'HTTP_X_FORWARDED_FOR' => '192.168.7.21'
];

$I->assertEquals(
'192.168.0.1',
$request->getClientAddress()
);

$I->assertEquals(
'192.168.7.21',
$request->getClientAddress(true)
);

$_SERVER = [
'REMOTE_ADDR' => '86.45.89.47, 214.55.34.56'
];

$I->assertEquals(
'86.45.89.47',
$request->getClientAddress()
);

$_SERVER = $store;
}
}
Loading

0 comments on commit c084fa0

Please sign in to comment.