2017-05-29 19:11:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* LocaleMiddlewareTest class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube\Test;
|
|
|
|
|
2017-11-10 11:18:20 +00:00
|
|
|
use Alltube\Locale;
|
2017-05-30 21:41:26 +00:00
|
|
|
use Alltube\LocaleManager;
|
2017-05-29 19:11:59 +00:00
|
|
|
use Alltube\LocaleMiddleware;
|
2017-10-26 08:46:22 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
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.
|
|
|
|
*/
|
2017-10-26 08:46:22 +00:00
|
|
|
class LocaleMiddlewareTest extends TestCase
|
2017-05-29 19:11:59 +00:00
|
|
|
{
|
2017-05-30 21:49:38 +00:00
|
|
|
/**
|
|
|
|
* LocaleMiddleware instance.
|
|
|
|
*
|
|
|
|
* @var LocaleMiddleware
|
|
|
|
*/
|
|
|
|
private $middleware;
|
2017-05-29 20:00:30 +00:00
|
|
|
|
2017-05-29 19:11:59 +00:00
|
|
|
/**
|
|
|
|
* Prepare tests.
|
|
|
|
*/
|
|
|
|
protected function setUp()
|
|
|
|
{
|
2017-11-10 11:18:20 +00:00
|
|
|
$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();
|
2017-05-29 19:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the testLocale() function.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTestLocale()
|
|
|
|
{
|
2017-08-21 18:50:44 +00:00
|
|
|
$this->markTestSkipped('For some reason, this test fails on Travis even if the fr_FR locale is installed.');
|
2017-05-29 19:11:59 +00:00
|
|
|
$locale = [
|
2017-10-29 22:21:13 +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-10-29 22:21:13 +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([]));
|
|
|
|
}
|
|
|
|
|
2017-10-02 18:29:09 +00:00
|
|
|
/**
|
2017-11-10 11:18:20 +00:00
|
|
|
* 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
|
2017-10-02 18:29:09 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-11-10 11:18:20 +00:00
|
|
|
public function assertNoHeader(Request $request)
|
2017-10-02 18:29:09 +00:00
|
|
|
{
|
2017-11-10 11:18:20 +00:00
|
|
|
$header = $request->getHeader('Accept-Language');
|
|
|
|
$this->assertEmpty($header);
|
2017-10-02 18:29:09 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 19:11:59 +00:00
|
|
|
/**
|
|
|
|
* Test the __invoke() function.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvoke()
|
|
|
|
{
|
|
|
|
$request = Request::createFromEnvironment(Environment::mock());
|
|
|
|
$this->middleware->__invoke(
|
2017-11-10 11:18:20 +00:00
|
|
|
$request->withHeader('Accept-Language', 'foo-BAR'),
|
2017-05-29 19:11:59 +00:00
|
|
|
new Response(),
|
2017-11-10 11:18:20 +00:00
|
|
|
[$this, 'assertHeader']
|
2017-05-29 19:11:59 +00:00
|
|
|
);
|
|
|
|
}
|
2017-05-30 21:41:26 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-10 11:18:20 +00:00
|
|
|
* Test the __invoke() function without the Accept-Language header.
|
2017-05-30 21:41:26 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvokeWithoutHeader()
|
|
|
|
{
|
|
|
|
$request = Request::createFromEnvironment(Environment::mock());
|
|
|
|
$this->middleware->__invoke(
|
|
|
|
$request->withoutHeader('Accept-Language'),
|
|
|
|
new Response(),
|
2017-11-10 11:18:20 +00:00
|
|
|
[$this, 'assertNoHeader']
|
2017-05-30 21:41:26 +00:00
|
|
|
);
|
2017-05-30 21:59:15 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 22:48:50 +00:00
|
|
|
/**
|
|
|
|
* Test that the environment is correctly set up.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-05-30 21:59:15 +00:00
|
|
|
public function testEnv()
|
|
|
|
{
|
|
|
|
$this->markTestIncomplete('We need to find a way to reliably test LC_ALL and LANG values');
|
2017-05-30 21:41:26 +00:00
|
|
|
}
|
2017-05-29 19:11:59 +00:00
|
|
|
}
|