2017-05-29 19:11:59 +00:00
|
|
|
<?php
|
2019-10-03 19:24:12 +00:00
|
|
|
|
2017-05-29 19:11:59 +00:00
|
|
|
/**
|
|
|
|
* LocaleMiddlewareTest class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Alltube\Test;
|
|
|
|
|
2020-10-22 20:39:09 +00:00
|
|
|
use Alltube\Exception\ConfigException;
|
2020-10-21 20:47:15 +00:00
|
|
|
use Alltube\Exception\DependencyException;
|
2020-10-20 21:13:48 +00:00
|
|
|
use Alltube\Middleware\LocaleMiddleware;
|
2017-05-29 19:11:59 +00:00
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
2020-10-22 20:39:09 +00:00
|
|
|
use SmartyException;
|
2017-05-29 19:11:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the FrontController class.
|
|
|
|
*/
|
2020-10-22 20:39:09 +00:00
|
|
|
class LocaleMiddlewareTest extends ContainerTest
|
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.
|
2020-10-22 20:39:09 +00:00
|
|
|
*
|
2020-10-21 20:47:15 +00:00
|
|
|
* @throws DependencyException
|
2020-10-22 20:39:09 +00:00
|
|
|
* @throws ConfigException
|
|
|
|
* @throws SmartyException
|
2017-05-29 19:11:59 +00:00
|
|
|
*/
|
2019-11-30 13:08:18 +00:00
|
|
|
protected function setUp(): void
|
2017-05-29 19:11:59 +00:00
|
|
|
{
|
2020-10-22 20:39:09 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-11-10 11:18:20 +00:00
|
|
|
$this->middleware = new LocaleMiddleware($this->container);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unset locale cookie after each test.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-11-30 13:08:18 +00:00
|
|
|
protected function tearDown(): void
|
2017-11-10 11:18:20 +00:00
|
|
|
{
|
2020-10-22 20:39:09 +00:00
|
|
|
parent::tearDown();
|
|
|
|
|
|
|
|
$this->container->get('locale')->unsetLocale();
|
2017-05-29 19:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the testLocale() function.
|
|
|
|
*
|
|
|
|
* @return void
|
2017-11-11 21:48:11 +00:00
|
|
|
* @requires OS Linux
|
2017-05-29 19:11:59 +00:00
|
|
|
*/
|
|
|
|
public function testTestLocale()
|
|
|
|
{
|
|
|
|
$locale = [
|
2017-11-11 21:39:41 +00:00
|
|
|
'language' => 'en',
|
2020-10-22 20:39:09 +00:00
|
|
|
'region' => 'US',
|
2017-05-29 19:11:59 +00:00
|
|
|
];
|
2017-11-11 21:39:41 +00:00
|
|
|
$this->assertEquals('en_US', $this->middleware->testLocale($locale));
|
2017-05-29 19:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the testLocale() function with an unsupported locale.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testLocaleWithWrongLocale()
|
|
|
|
{
|
|
|
|
$locale = [
|
2017-10-29 22:21:13 +00:00
|
|
|
'language' => 'foo',
|
2020-10-22 20:39:09 +00:00
|
|
|
'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
|
2020-12-17 21:43:05 +00:00
|
|
|
* @param Response $response
|
2017-11-10 11:18:20 +00:00
|
|
|
*
|
2020-12-17 21:43:05 +00:00
|
|
|
* @return Response
|
2017-11-10 11:18:20 +00:00
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function assertHeader(Request $request, Response $response): Response
|
2017-11-10 11:18:20 +00:00
|
|
|
{
|
|
|
|
$header = $request->getHeader('Accept-Language');
|
|
|
|
$this->assertEquals('foo-BAR', $header[0]);
|
2020-12-17 21:43:05 +00:00
|
|
|
|
|
|
|
return $response;
|
2017-11-10 11:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that the request contains no Accept-Language header.
|
|
|
|
*
|
|
|
|
* @param Request $request PSR-7 request
|
2020-12-17 21:43:05 +00:00
|
|
|
* @param Response $response
|
2017-10-02 18:29:09 +00:00
|
|
|
*
|
2020-12-17 21:43:05 +00:00
|
|
|
* @return Response
|
2017-10-02 18:29:09 +00:00
|
|
|
*/
|
2020-12-17 21:43:05 +00:00
|
|
|
public function assertNoHeader(Request $request, Response $response): Response
|
2017-10-02 18:29:09 +00:00
|
|
|
{
|
2017-11-10 11:18:20 +00:00
|
|
|
$header = $request->getHeader('Accept-Language');
|
|
|
|
$this->assertEmpty($header);
|
2020-12-17 21:43:05 +00:00
|
|
|
|
|
|
|
return $response;
|
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()
|
|
|
|
{
|
|
|
|
$this->middleware->__invoke(
|
2020-10-22 20:39:09 +00:00
|
|
|
$this->container->get('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()
|
|
|
|
{
|
|
|
|
$this->middleware->__invoke(
|
2020-10-22 20:39:09 +00:00
|
|
|
$this->container->get('request')->withoutHeader('Accept-Language'),
|
2017-05-30 21:41:26 +00:00
|
|
|
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-29 19:11:59 +00:00
|
|
|
}
|