alltube/tests/LocaleMiddlewareTest.php

114 lines
2.6 KiB
PHP
Raw Normal View History

2017-05-29 19:11:59 +00:00
<?php
/**
* LocaleMiddlewareTest class.
*/
namespace Alltube\Test;
2017-05-30 21:41:26 +00:00
use Alltube\LocaleManager;
2017-05-29 19:11:59 +00:00
use Alltube\LocaleMiddleware;
2017-05-30 21:41:26 +00:00
use Slim\Container;
2017-05-29 19:11:59 +00:00
use Slim\Http\Environment;
use Slim\Http\Request;
use Slim\Http\Response;
/**
* Unit tests for the FrontController class.
*/
class LocaleMiddlewareTest extends \PHPUnit_Framework_TestCase
{
2017-05-29 20:00:30 +00:00
/**
* Original locale.
*
* @var string
*/
private $locale;
2017-05-29 19:11:59 +00:00
/**
* Prepare tests.
*/
protected function setUp()
{
2017-05-29 20:00:30 +00:00
$this->locale = getenv('LANG');
2017-05-30 21:41:26 +00:00
$container = new Container();
$container['locale'] = new LocaleManager();
$this->middleware = new LocaleMiddleware($container);
2017-05-29 19:11:59 +00:00
}
2017-05-29 20:00:30 +00:00
/**
* Restore environment after the tests.
*
* @return void
*/
protected function tearDown()
{
putenv('LANG='.$this->locale);
setlocale(LC_ALL, $this->locale);
}
2017-05-29 19:11:59 +00:00
/**
* Test the testLocale() function.
*
* @return void
*/
public function testTestLocale()
{
$locale = [
2017-05-29 19:13:10 +00:00
'language'=> 'fr',
'region' => 'FR',
2017-05-29 19:11:59 +00:00
];
$this->assertEquals('fr_FR', $this->middleware->testLocale($locale));
}
/**
* Test the testLocale() function with an unsupported locale.
*
* @return void
*/
public function testLocaleWithWrongLocale()
{
$locale = [
2017-05-29 19:13:10 +00:00
'language'=> 'foo',
'region' => 'BAR',
2017-05-29 19:11:59 +00:00
];
$this->assertNull($this->middleware->testLocale($locale));
$this->assertNull($this->middleware->testLocale([]));
}
/**
* Test the __invoke() function.
*
* @return void
*/
public function testInvoke()
{
$request = Request::createFromEnvironment(Environment::mock());
$this->middleware->__invoke(
$request->withHeader('Accept-Language', 'fr-FR'),
new Response(),
function () {
}
);
$this->assertEquals('fr_FR', getenv('LANG'));
$this->assertEquals('fr_FR', setlocale(LC_ALL, null));
}
2017-05-30 21:41:26 +00:00
/**
* Test the __invoke() function withot the Accept-Language header.
*
* @return void
*/
public function testInvokeWithoutHeader()
{
$request = Request::createFromEnvironment(Environment::mock());
$this->middleware->__invoke(
$request->withoutHeader('Accept-Language'),
new Response(),
function () {
}
);
$this->assertEquals('en_US', getenv('LANG'));
$this->assertEquals('en_US', setlocale(LC_ALL, null));
}
2017-05-29 19:11:59 +00:00
}