alltube/tests/LocaleMiddlewareTest.php
Pierre Rudloff 8848a8dbf3 Improve LocaleMiddlewareTest
Add a way to unset the current locale
2017-11-10 12:18:20 +01:00

144 lines
3.3 KiB
PHP

<?php
/**
* LocaleMiddlewareTest class.
*/
namespace Alltube\Test;
use Alltube\Locale;
use Alltube\LocaleManager;
use Alltube\LocaleMiddleware;
use PHPUnit\Framework\TestCase;
use Slim\Container;
use Slim\Http\Environment;
use Slim\Http\Request;
use Slim\Http\Response;
/**
* Unit tests for the FrontController class.
*/
class LocaleMiddlewareTest extends TestCase
{
/**
* LocaleMiddleware instance.
*
* @var LocaleMiddleware
*/
private $middleware;
/**
* Prepare tests.
*/
protected function setUp()
{
$this->container = new Container();
$this->container['locale'] = new LocaleManager();
$this->middleware = new LocaleMiddleware($this->container);
}
/**
* Unset locale cookie after each test.
*
* @return void
*/
protected function tearDown()
{
$this->container['locale']->unsetLocale();
}
/**
* Test the testLocale() function.
*
* @return void
*/
public function testTestLocale()
{
$this->markTestSkipped('For some reason, this test fails on Travis even if the fr_FR locale is installed.');
$locale = [
'language' => 'fr',
'region' => 'FR',
];
$this->assertEquals('fr_FR', $this->middleware->testLocale($locale));
}
/**
* Test the testLocale() function with an unsupported locale.
*
* @return void
*/
public function testLocaleWithWrongLocale()
{
$locale = [
'language' => 'foo',
'region' => 'BAR',
];
$this->assertNull($this->middleware->testLocale($locale));
$this->assertNull($this->middleware->testLocale([]));
}
/**
* Check that the request contains an Accept-Language header.
*
* @param Request $request PSR-7 request
*
* @return void
*/
public function assertHeader(Request $request)
{
$header = $request->getHeader('Accept-Language');
$this->assertEquals('foo-BAR', $header[0]);
}
/**
* Check that the request contains no Accept-Language header.
*
* @param Request $request PSR-7 request
*
* @return void
*/
public function assertNoHeader(Request $request)
{
$header = $request->getHeader('Accept-Language');
$this->assertEmpty($header);
}
/**
* Test the __invoke() function.
*
* @return void
*/
public function testInvoke()
{
$request = Request::createFromEnvironment(Environment::mock());
$this->middleware->__invoke(
$request->withHeader('Accept-Language', 'foo-BAR'),
new Response(),
[$this, 'assertHeader']
);
}
/**
* Test the __invoke() function without the Accept-Language header.
*
* @return void
*/
public function testInvokeWithoutHeader()
{
$request = Request::createFromEnvironment(Environment::mock());
$this->middleware->__invoke(
$request->withoutHeader('Accept-Language'),
new Response(),
[$this, 'assertNoHeader']
);
}
/**
* Test that the environment is correctly set up.
*
* @return void
*/
public function testEnv()
{
$this->markTestIncomplete('We need to find a way to reliably test LC_ALL and LANG values');
}
}