From 8848a8dbf398963ef6e523f5e5560ef4db8608c0 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Fri, 10 Nov 2017 12:18:20 +0100 Subject: [PATCH] Improve LocaleMiddlewareTest Add a way to unset the current locale --- classes/LocaleManager.php | 9 +++++++ tests/LocaleManagerTest.php | 11 ++++++++ tests/LocaleMiddlewareTest.php | 46 +++++++++++++++++++++++++++------- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/classes/LocaleManager.php b/classes/LocaleManager.php index 7e36e8c..03531fb 100644 --- a/classes/LocaleManager.php +++ b/classes/LocaleManager.php @@ -95,4 +95,13 @@ class LocaleManager $this->curLocale = $locale; $this->sessionSegment->set('locale', $locale); } + + /** + * Unset the current locale. + */ + public function unsetLocale() + { + $this->curLocale = null; + $this->sessionSegment->clear('locale'); + } } diff --git a/tests/LocaleManagerTest.php b/tests/LocaleManagerTest.php index 7eb5cdb..2808362 100644 --- a/tests/LocaleManagerTest.php +++ b/tests/LocaleManagerTest.php @@ -75,4 +75,15 @@ class LocaleManagerTest extends TestCase $this->assertInstanceOf(Locale::class, $locale); $this->assertEquals('foo_BAR', (string) $locale); } + + /** + * Test the unsetLocale function. + * + * @return void + */ + public function testUnsetLocale() + { + $this->localeManager->unsetLocale(); + $this->assertNull($this->localeManager->getLocale()); + } } diff --git a/tests/LocaleMiddlewareTest.php b/tests/LocaleMiddlewareTest.php index 1845327..35c30e2 100644 --- a/tests/LocaleMiddlewareTest.php +++ b/tests/LocaleMiddlewareTest.php @@ -5,6 +5,7 @@ namespace Alltube\Test; +use Alltube\Locale; use Alltube\LocaleManager; use Alltube\LocaleMiddleware; use PHPUnit\Framework\TestCase; @@ -30,9 +31,19 @@ class LocaleMiddlewareTest extends TestCase */ protected function setUp() { - $container = new Container(); - $container['locale'] = new LocaleManager(); - $this->middleware = new LocaleMiddleware($container); + $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(); } /** @@ -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 */ - 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()); $this->middleware->__invoke( - $request->withHeader('Accept-Language', 'fr-FR'), + $request->withHeader('Accept-Language', 'foo-BAR'), 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 */ @@ -100,7 +128,7 @@ class LocaleMiddlewareTest extends TestCase $this->middleware->__invoke( $request->withoutHeader('Accept-Language'), new Response(), - [$this, 'nothing'] + [$this, 'assertNoHeader'] ); }