Improve LocaleMiddlewareTest

Add a way to unset the current locale
This commit is contained in:
Pierre Rudloff 2017-11-10 12:18:20 +01:00
parent c994f8ac9a
commit 8848a8dbf3
3 changed files with 57 additions and 9 deletions

View file

@ -95,4 +95,13 @@ class LocaleManager
$this->curLocale = $locale; $this->curLocale = $locale;
$this->sessionSegment->set('locale', $locale); $this->sessionSegment->set('locale', $locale);
} }
/**
* Unset the current locale.
*/
public function unsetLocale()
{
$this->curLocale = null;
$this->sessionSegment->clear('locale');
}
} }

View file

@ -75,4 +75,15 @@ class LocaleManagerTest extends TestCase
$this->assertInstanceOf(Locale::class, $locale); $this->assertInstanceOf(Locale::class, $locale);
$this->assertEquals('foo_BAR', (string) $locale); $this->assertEquals('foo_BAR', (string) $locale);
} }
/**
* Test the unsetLocale function.
*
* @return void
*/
public function testUnsetLocale()
{
$this->localeManager->unsetLocale();
$this->assertNull($this->localeManager->getLocale());
}
} }

View file

@ -5,6 +5,7 @@
namespace Alltube\Test; namespace Alltube\Test;
use Alltube\Locale;
use Alltube\LocaleManager; use Alltube\LocaleManager;
use Alltube\LocaleMiddleware; use Alltube\LocaleMiddleware;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -30,9 +31,19 @@ class LocaleMiddlewareTest extends TestCase
*/ */
protected function setUp() protected function setUp()
{ {
$container = new Container(); $this->container = new Container();
$container['locale'] = new LocaleManager(); $this->container['locale'] = new LocaleManager();
$this->middleware = new LocaleMiddleware($container); $this->middleware = new LocaleMiddleware($this->container);
}
/**
* Unset locale cookie after each test.
*
* @return void
*/
protected function tearDown()
{
$this->container['locale']->unsetLocale();
} }
/** /**
@ -66,12 +77,29 @@ class LocaleMiddlewareTest extends TestCase
} }
/** /**
* Mock function that does nothing. * Check that the request contains an Accept-Language header.
*
* @param Request $request PSR-7 request
* *
* @return void * @return void
*/ */
public function nothing() 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);
} }
/** /**
@ -83,14 +111,14 @@ class LocaleMiddlewareTest extends TestCase
{ {
$request = Request::createFromEnvironment(Environment::mock()); $request = Request::createFromEnvironment(Environment::mock());
$this->middleware->__invoke( $this->middleware->__invoke(
$request->withHeader('Accept-Language', 'fr-FR'), $request->withHeader('Accept-Language', 'foo-BAR'),
new Response(), new Response(),
[$this, 'nothing'] [$this, 'assertHeader']
); );
} }
/** /**
* Test the __invoke() function withot the Accept-Language header. * Test the __invoke() function without the Accept-Language header.
* *
* @return void * @return void
*/ */
@ -100,7 +128,7 @@ class LocaleMiddlewareTest extends TestCase
$this->middleware->__invoke( $this->middleware->__invoke(
$request->withoutHeader('Accept-Language'), $request->withoutHeader('Accept-Language'),
new Response(), new Response(),
[$this, 'nothing'] [$this, 'assertNoHeader']
); );
} }