Added more tests to Common module

This commit is contained in:
Alejandro Celaya 2016-07-30 17:17:21 +02:00
parent 00db8a7ea5
commit 2ce6c1f44b
2 changed files with 102 additions and 0 deletions

View 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);
}
}

View 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());
}
}