From 2ce6c1f44b8062556311de2b8f12629cfb260a8b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 30 Jul 2016 17:17:21 +0200 Subject: [PATCH] Added more tests to Common module --- .../test/Factory/TranslatorFactoryTest.php | 31 ++++++++ .../test/Middleware/LocaleMiddlewareTest.php | 71 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 module/Common/test/Factory/TranslatorFactoryTest.php create mode 100644 module/Common/test/Middleware/LocaleMiddlewareTest.php diff --git a/module/Common/test/Factory/TranslatorFactoryTest.php b/module/Common/test/Factory/TranslatorFactoryTest.php new file mode 100644 index 00000000..e70f820b --- /dev/null +++ b/module/Common/test/Factory/TranslatorFactoryTest.php @@ -0,0 +1,31 @@ +factory = new TranslatorFactory(); + } + + /** + * @test + */ + public function serviceIsCreated() + { + $instance = $this->factory->__invoke(new ServiceManager(['services' => [ + 'config' => [], + ]]), ''); + $this->assertInstanceOf(Translator::class, $instance); + } +} diff --git a/module/Common/test/Middleware/LocaleMiddlewareTest.php b/module/Common/test/Middleware/LocaleMiddlewareTest.php new file mode 100644 index 00000000..72e6bf77 --- /dev/null +++ b/module/Common/test/Middleware/LocaleMiddlewareTest.php @@ -0,0 +1,71 @@ +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()); + } +}