From 62d6839cd0281f8b5323c4572ddf4cc9673e171a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20F=C3=BCsslin?= Date: Tue, 5 Dec 2017 16:30:10 +0100 Subject: [PATCH 1/5] UriPathStrategy now works with phpunit tests of the application --- src/SlmLocale/Strategy/UriPathStrategy.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/SlmLocale/Strategy/UriPathStrategy.php b/src/SlmLocale/Strategy/UriPathStrategy.php index 837729e..4e99be0 100644 --- a/src/SlmLocale/Strategy/UriPathStrategy.php +++ b/src/SlmLocale/Strategy/UriPathStrategy.php @@ -118,6 +118,10 @@ public function detect(LocaleEvent $event) public function found(LocaleEvent $event) { + if ('phpunit' === pathinfo($_SERVER['PHP_SELF'], PATHINFO_BASENAME)) { + return; + } + $request = $event->getRequest(); if (! $this->isHttpRequest($request)) { return; From 760124cdbd4f805e50661b67954a3160255d76b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20F=C3=BCsslin?= Date: Tue, 5 Dec 2017 17:06:45 +0100 Subject: [PATCH 2/5] updated UriPathStrategy and added test case. Updated README.md with instructions how to configure PHPUNIT --- README.md | 27 +++++++++++++++++++ src/SlmLocale/Strategy/UriPathStrategy.php | 2 +- .../Strategy/UriPathStrategyTest.php | 22 +++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index da89e0e..e6e2793 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,33 @@ Inject the detected language here with the following code: ``` +### Disable UriPathStrategy in PHPUNIT +This is necessary (at the moment) if you want to use ``this->dispatch('my/uri');`` in your `AbstractHttpControllerTestCase` unit tests. +Otherwise, if you check for responseCode you will get `302` where it should be `200`. + +Example: +``` +$this->dispatch('/to/my/uri'); +$this->assertResponseStatusCode(200); // this will be 302 instead of 200 + +$this->dispatch('/en/to/my/uri'); +$this->assertResponseStatusCode(200); // this will be 302 instead of 200 +``` + +To fix add the following to your phpunit config. + +XML: +``` + + ... + + + + +``` + +Or set ``$_SERVER['DISABLE_URIPATHSTRATEGY'] = true;`` in your bootstrap file of phpunit. + ### Create a list of available locales T.B.D diff --git a/src/SlmLocale/Strategy/UriPathStrategy.php b/src/SlmLocale/Strategy/UriPathStrategy.php index 4e99be0..f995314 100644 --- a/src/SlmLocale/Strategy/UriPathStrategy.php +++ b/src/SlmLocale/Strategy/UriPathStrategy.php @@ -118,7 +118,7 @@ public function detect(LocaleEvent $event) public function found(LocaleEvent $event) { - if ('phpunit' === pathinfo($_SERVER['PHP_SELF'], PATHINFO_BASENAME)) { + if (array_key_exists('DISABLE_URIPATHSTRATEGY', $_SERVER) && true === $_SERVER['DISABLE_URIPATHSTRATEGY']) { return; } diff --git a/tests/SlmLocaleTest/Strategy/UriPathStrategyTest.php b/tests/SlmLocaleTest/Strategy/UriPathStrategyTest.php index a49af0f..962e866 100644 --- a/tests/SlmLocaleTest/Strategy/UriPathStrategyTest.php +++ b/tests/SlmLocaleTest/Strategy/UriPathStrategyTest.php @@ -348,6 +348,28 @@ public function testAssembleWorksWithAliasesToo() $this->assertEquals($expected, $actual); } + public function testDisableUriPathStrategyPhpunit() + { + $_SERVER['DISABLE_URIPATHSTRATEGY'] = true; + + $uri = 'http://username:password@example.com:8080/some/deep/path/some.file?withsomeparam=true'; + $request = new HttpRequest(); + $request->setUri($uri); + + $this->event->setLocale('en'); + $this->event->setRequest($request); + $this->event->setResponse(new HttpResponse()); + + $this->strategy->found($this->event); + + $statusCode = $this->event->getResponse()->getStatusCode(); + $header = $this->event->getResponse()->getHeaders()->get('Location'); + $expected = 'Location: http://username:password@example.com:8080/en/some/deep/path/some.file?withsomeparam=true'; + $this->assertEquals(200, $statusCode); + + $_SERVER['DISABLE_URIPATHSTRATEGY'] = false; + } + protected function getPluginManager($console = false) { $sl = new ServiceManager(); From 2dcd82128cd196b0b2a3cef02c216a0e701378db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20F=C3=BCsslin?= Date: Tue, 5 Dec 2017 17:10:06 +0100 Subject: [PATCH 3/5] corrected spelling in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e6e2793..833e551 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ $this->assertResponseStatusCode(200); // this will be 302 instead of 200 To fix add the following to your phpunit config. -XML: +phpunit.xml: ``` ... From e83340642737057a31aa61ba46b8e0e4ed8dbe8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20F=C3=BCsslin?= Date: Wed, 6 Dec 2017 12:27:06 +0100 Subject: [PATCH 4/5] updated Readme to force travis to reran its test --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 833e551..a8a4633 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,7 @@ phpunit.xml: ``` -Or set ``$_SERVER['DISABLE_URIPATHSTRATEGY'] = true;`` in your bootstrap file of phpunit. +Or set ``$_SERVER['DISABLE_URIPATHSTRATEGY'] = true;`` in your bootstrap file of phpunit. ### Create a list of available locales From 63a8195d73b1af0728639a1ee30523eaddbcc63c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20F=C3=BCsslin?= Date: Thu, 7 Dec 2017 17:36:01 +0100 Subject: [PATCH 5/5] updated composer: set zendframework/zend-modulemanager to version ^2.8.2 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index dcd45ac..4541044 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "ext-intl": "*", "zendframework/zend-eventmanager": "^3.1", "zendframework/zend-http": "^2.7", - "zendframework/zend-modulemanager": "^2.8.1", + "zendframework/zend-modulemanager": "^2.8.2", "zendframework/zend-router": "^3.0", "zendframework/zend-servicemanager": "^3.2", "zendframework/zend-stdlib": "^3.1",