mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-19 00:39:55 +03:00
Added more tests to Common module
This commit is contained in:
parent
00db8a7ea5
commit
2ce6c1f44b
2 changed files with 102 additions and 0 deletions
31
module/Common/test/Factory/TranslatorFactoryTest.php
Normal file
31
module/Common/test/Factory/TranslatorFactoryTest.php
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
namespace ShlinkioTest\Shlink\Common\Factory;
|
||||||
|
|
||||||
|
use PHPUnit_Framework_TestCase as TestCase;
|
||||||
|
use Shlinkio\Shlink\Common\Factory\TranslatorFactory;
|
||||||
|
use Zend\I18n\Translator\Translator;
|
||||||
|
use Zend\ServiceManager\ServiceManager;
|
||||||
|
|
||||||
|
class TranslatorFactoryTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var TranslatorFactory
|
||||||
|
*/
|
||||||
|
protected $factory;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->factory = new TranslatorFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function serviceIsCreated()
|
||||||
|
{
|
||||||
|
$instance = $this->factory->__invoke(new ServiceManager(['services' => [
|
||||||
|
'config' => [],
|
||||||
|
]]), '');
|
||||||
|
$this->assertInstanceOf(Translator::class, $instance);
|
||||||
|
}
|
||||||
|
}
|
71
module/Common/test/Middleware/LocaleMiddlewareTest.php
Normal file
71
module/Common/test/Middleware/LocaleMiddlewareTest.php
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
<?php
|
||||||
|
namespace ShlinkioTest\Shlink\Common\Middleware;
|
||||||
|
|
||||||
|
use PHPUnit_Framework_TestCase as TestCase;
|
||||||
|
use Shlinkio\Shlink\Common\Middleware\LocaleMiddleware;
|
||||||
|
use Zend\Diactoros\Response;
|
||||||
|
use Zend\Diactoros\ServerRequestFactory;
|
||||||
|
use Zend\I18n\Translator\Translator;
|
||||||
|
|
||||||
|
class LocaleMiddlewareTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var LocaleMiddleware
|
||||||
|
*/
|
||||||
|
protected $middleware;
|
||||||
|
/**
|
||||||
|
* @var Translator
|
||||||
|
*/
|
||||||
|
protected $translator;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->translator = Translator::factory(['locale' => 'ru']);
|
||||||
|
$this->middleware = new LocaleMiddleware($this->translator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function whenNoHeaderIsPresentLocaleIsNotChanged()
|
||||||
|
{
|
||||||
|
$this->assertEquals('ru', $this->translator->getLocale());
|
||||||
|
$this->middleware->__invoke(ServerRequestFactory::fromGlobals(), new Response(), function ($req, $resp) {
|
||||||
|
return $resp;
|
||||||
|
});
|
||||||
|
$this->assertEquals('ru', $this->translator->getLocale());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function whenTheHeaderIsPresentLocaleIsChanged()
|
||||||
|
{
|
||||||
|
$this->assertEquals('ru', $this->translator->getLocale());
|
||||||
|
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'es');
|
||||||
|
$this->middleware->__invoke($request, new Response(), function ($req, $resp) {
|
||||||
|
return $resp;
|
||||||
|
});
|
||||||
|
$this->assertEquals('es', $this->translator->getLocale());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function localeGetsNormalized()
|
||||||
|
{
|
||||||
|
$this->assertEquals('ru', $this->translator->getLocale());
|
||||||
|
|
||||||
|
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'es_ES');
|
||||||
|
$this->middleware->__invoke($request, new Response(), function ($req, $resp) {
|
||||||
|
return $resp;
|
||||||
|
});
|
||||||
|
$this->assertEquals('es', $this->translator->getLocale());
|
||||||
|
|
||||||
|
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'en-US');
|
||||||
|
$this->middleware->__invoke($request, new Response(), function ($req, $resp) {
|
||||||
|
return $resp;
|
||||||
|
});
|
||||||
|
$this->assertEquals('en', $this->translator->getLocale());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue