diff --git a/README.md b/README.md index da89e0e..a8a4633 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. + +phpunit.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/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", diff --git a/src/SlmLocale/Strategy/UriPathStrategy.php b/src/SlmLocale/Strategy/UriPathStrategy.php index 837729e..f995314 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 (array_key_exists('DISABLE_URIPATHSTRATEGY', $_SERVER) && true === $_SERVER['DISABLE_URIPATHSTRATEGY']) { + return; + } + $request = $event->getRequest(); if (! $this->isHttpRequest($request)) { 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();