From bafdbfcc1b50c52bccd893b9a2d0699a0d0650c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20F=C3=BCsslin?= Date: Tue, 12 Dec 2017 16:21:35 +0100 Subject: [PATCH 1/6] created new strategy for phpunit tests --- src/SlmLocale/Strategy/PhpunitStrategy.php | 67 +++++++++++++++++++ src/SlmLocale/Strategy/UriPathStrategy.php | 4 -- .../Strategy/PhpunitStrategyTest.php | 57 ++++++++++++++++ .../Strategy/UriPathStrategyTest.php | 22 ------ 4 files changed, 124 insertions(+), 26 deletions(-) create mode 100644 src/SlmLocale/Strategy/PhpunitStrategy.php create mode 100644 tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php diff --git a/src/SlmLocale/Strategy/PhpunitStrategy.php b/src/SlmLocale/Strategy/PhpunitStrategy.php new file mode 100644 index 0000000..5f5fc60 --- /dev/null +++ b/src/SlmLocale/Strategy/PhpunitStrategy.php @@ -0,0 +1,67 @@ + [ + * 'default' => 'de_DE', + * 'supported' => ['en_GB', 'de_DE'], + * 'strategies' => [ + * [ + * 'name' => SlmLocale\Strategy\AssetStrategy::class, + * 'options' => [ + * 'file_extensions' => [ + * 'css', 'js' + * ] + * ] + * ], + * 'query', + * 'cookie', + * 'acceptlanguage' + * ], + * 'mappings' => [ + * 'en' => 'en_GB', + * 'de' => 'de_DE', + * ] + * ], + * + * This example config would ignore the file_extensions ".css", ".CSS", ".js", ".JS". + * + * Class PhpunitStrategy + * @package SlmLocale\Strategy + */ +final class PhpunitStrategy extends AbstractStrategy +{ + /** @var array */ + private $file_extensions = []; + + public function detect(LocaleEvent $event) + { + $this->stopPropagationIfPhpunit($event); + } + + public function found(LocaleEvent $event) + { + $this->stopPropagationIfPhpunit($event); + } + + private function stopPropagationIfPhpunit(LocaleEvent $event) + { + if (! $this->isHttpRequest($event->getRequest())) { + return; + } + + $isPhpunit = array_key_exists('DISABLE_URIPATHSTRATEGY', $_SERVER) && $_SERVER['DISABLE_URIPATHSTRATEGY']; + + // if the file extension of the uri is found within the configured file_extensions, we do not rewrite and skip further processing + if ($isPhpunit) { + $event->stopPropagation(); + } + } +} diff --git a/src/SlmLocale/Strategy/UriPathStrategy.php b/src/SlmLocale/Strategy/UriPathStrategy.php index f995314..837729e 100644 --- a/src/SlmLocale/Strategy/UriPathStrategy.php +++ b/src/SlmLocale/Strategy/UriPathStrategy.php @@ -118,10 +118,6 @@ 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/PhpunitStrategyTest.php b/tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php new file mode 100644 index 0000000..fab6dde --- /dev/null +++ b/tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php @@ -0,0 +1,57 @@ +router = new HttpRouter(); + + $this->strategy = new PhpunitStrategy(); + + $this->event = new LocaleEvent(); + $this->event->setSupported(['nl', 'de', 'en']); + } + + 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(); + $this->assertEquals(200, $statusCode); + + $_SERVER['DISABLE_URIPATHSTRATEGY'] = false; + } +} diff --git a/tests/SlmLocaleTest/Strategy/UriPathStrategyTest.php b/tests/SlmLocaleTest/Strategy/UriPathStrategyTest.php index 962e866..a49af0f 100644 --- a/tests/SlmLocaleTest/Strategy/UriPathStrategyTest.php +++ b/tests/SlmLocaleTest/Strategy/UriPathStrategyTest.php @@ -348,28 +348,6 @@ 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 6b87774b74f9e1156e7642d04cd13ace078bdda8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20F=C3=BCsslin?= Date: Tue, 12 Dec 2017 16:25:27 +0100 Subject: [PATCH 2/6] updated pluginmanager with new strategy (phpunit) --- src/SlmLocale/Strategy/StrategyPluginManager.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/SlmLocale/Strategy/StrategyPluginManager.php b/src/SlmLocale/Strategy/StrategyPluginManager.php index ae9a005..b12116a 100644 --- a/src/SlmLocale/Strategy/StrategyPluginManager.php +++ b/src/SlmLocale/Strategy/StrategyPluginManager.php @@ -58,6 +58,7 @@ class StrategyPluginManager extends AbstractPluginManager 'query' => QueryStrategy::class, 'uripath' => UriPathStrategy::class, 'asset' => AssetStrategy::class, + 'phpunit' => PhpunitStrategy::class, ]; /** @@ -70,5 +71,6 @@ class StrategyPluginManager extends AbstractPluginManager QueryStrategy::class => InvokableFactory::class, UriPathStrategy::class => UriPathStrategyFactory::class, AssetStrategy::class => InvokableFactory::class, + PhpunitStrategy::class => InvokableFactory::class, ]; } From f3d2b17760cb6ccb7d32360b2e32131a1090f21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20F=C3=BCsslin?= Date: Tue, 12 Dec 2017 16:46:12 +0100 Subject: [PATCH 3/6] updated strategies and readme files --- README.md | 28 +-------- docs/2.Strategies.md | 57 ++++++++++++++++++- src/SlmLocale/Strategy/PhpunitStrategy.php | 31 ++-------- .../Strategy/PhpunitStrategyTest.php | 25 ++++---- 4 files changed, 74 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index a8a4633..0b3a65e 100644 --- a/README.md +++ b/README.md @@ -113,32 +113,8 @@ 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. +### PHPUNIT configuration (as this module breaks some tests at the moment) +Have a look at [strategies documentation](docs/2.Strategies.md). ### Create a list of available locales diff --git a/docs/2.Strategies.md b/docs/2.Strategies.md index 3ab2be5..694c03e 100644 --- a/docs/2.Strategies.md +++ b/docs/2.Strategies.md @@ -89,4 +89,59 @@ To cope with such problems use AssetStrategy: The `file_extensions` array should contain all file endings you use for your assets. URIs of files which have one of the specified extensions will not be rewritten (the example above excludes `css` and `js` files). -**Important:** Make sure to add AssetStrategy first / have AssetStrategy added with highest priority in your config. Otherwise some other strategies may rewrite the URI of an asset. \ No newline at end of file +**Important:** Make sure to add AssetStrategy first / have AssetStrategy added with highest priority in your config. Otherwise some other strategies may rewrite the URI of an asset. + +### PHPUNIT strategy +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`. +More information at: https://github.com/basz/SlmLocale/issues/26 + +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_STRATEGIES'] = true;`` in your bootstrap file of phpunit. + +Do not forget to enable phpunit strategy in config (example): + +``` +'strategies' => [ + 'phpunit', + [ + 'name' => SlmLocale\Strategy\AssetStrategy::class, + 'options' => [ + 'file_extensions' => [ + 'css', 'js' + ] + ] + ], + 'query', + [ + 'name' => \SlmLocale\Strategy\UriPathStrategy::class, + 'options' => [ + 'redirect_when_found' => true, + 'aliases' => array('de' => 'de_DE', 'en' => 'en_GB'), + ] + ], + 'cookie', + 'acceptlanguage' +], +``` +**Important:** Make sure to add PhpunitStrategy first / have PhpunitStrategy added with highest priority in your config. Otherwise some other strategies may rewrite the URI of an asset. \ No newline at end of file diff --git a/src/SlmLocale/Strategy/PhpunitStrategy.php b/src/SlmLocale/Strategy/PhpunitStrategy.php index 5f5fc60..429d36a 100644 --- a/src/SlmLocale/Strategy/PhpunitStrategy.php +++ b/src/SlmLocale/Strategy/PhpunitStrategy.php @@ -5,33 +5,10 @@ use SlmLocale\LocaleEvent; /** - * This class checks whether the requested uri should deliver an asset and should therefore not be redirected. - * If it is an asset, we return false to stop further processing of other strategies in SlmLocale\Locale\Detector. + * This class checks if running in a phpunit environment. If so and phpunit is correctly configured, it will stop event processing as we cannot properly use this module with phpunit tests at the moment. + * @SEE https://github.com/basz/SlmLocale/pull/99 * - * Example config: - * 'slm_locale' => [ - * 'default' => 'de_DE', - * 'supported' => ['en_GB', 'de_DE'], - * 'strategies' => [ - * [ - * 'name' => SlmLocale\Strategy\AssetStrategy::class, - * 'options' => [ - * 'file_extensions' => [ - * 'css', 'js' - * ] - * ] - * ], - * 'query', - * 'cookie', - * 'acceptlanguage' - * ], - * 'mappings' => [ - * 'en' => 'en_GB', - * 'de' => 'de_DE', - * ] - * ], - * - * This example config would ignore the file_extensions ".css", ".CSS", ".js", ".JS". + * For configuration example have a look at README.md. * * Class PhpunitStrategy * @package SlmLocale\Strategy @@ -57,7 +34,7 @@ private function stopPropagationIfPhpunit(LocaleEvent $event) return; } - $isPhpunit = array_key_exists('DISABLE_URIPATHSTRATEGY', $_SERVER) && $_SERVER['DISABLE_URIPATHSTRATEGY']; + $isPhpunit = array_key_exists('DISABLE_STRATEGIES', $_SERVER) && $_SERVER['DISABLE_STRATEGIES']; // if the file extension of the uri is found within the configured file_extensions, we do not rewrite and skip further processing if ($isPhpunit) { diff --git a/tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php b/tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php index fab6dde..9016166 100644 --- a/tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php +++ b/tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php @@ -11,14 +11,13 @@ use PHPUnit\Framework\TestCase; use SlmLocale\LocaleEvent; use SlmLocale\Strategy\PhpunitStrategy; -use SlmLocale\Strategy\UriPathStrategy; use Zend\Http\PhpEnvironment\Request as HttpRequest; use Zend\Http\PhpEnvironment\Response as HttpResponse; use Zend\Router\Http\TreeRouteStack as HttpRouter; class PhpunitStrategyTest extends TestCase { - /** @var UriPathStrategy */ + /** @var PhpunitStrategy */ private $strategy; /** @var LocaleEvent */ private $event; @@ -30,28 +29,28 @@ public function setup() $this->router = new HttpRouter(); $this->strategy = new PhpunitStrategy(); - - $this->event = new LocaleEvent(); - $this->event->setSupported(['nl', 'de', 'en']); } - public function testDisableUriPathStrategyPhpunit() + public function testPreventStrategiesExecutionIfPhpunit() { - $_SERVER['DISABLE_URIPATHSTRATEGY'] = true; + $_SERVER['DISABLE_STRATEGIES'] = true; + + $event = new LocaleEvent(); + $event->setSupported(['nl', 'de', 'en']); $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()); + $event->setLocale('en'); + $event->setRequest($request); + $event->setResponse(new HttpResponse()); - $this->strategy->found($this->event); + $this->strategy->found($event); - $statusCode = $this->event->getResponse()->getStatusCode(); + $statusCode = $event->getResponse()->getStatusCode(); $this->assertEquals(200, $statusCode); - $_SERVER['DISABLE_URIPATHSTRATEGY'] = false; + $_SERVER['DISABLE_STRATEGIES'] = false; } } From 43989223daca38f141e4ba0318a14d8a2ee06666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20F=C3=BCsslin?= Date: Tue, 12 Dec 2017 17:23:54 +0100 Subject: [PATCH 4/6] updated key of disable strategies --- docs/2.Strategies.md | 4 ++-- src/SlmLocale/Strategy/PhpunitStrategy.php | 2 +- tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/2.Strategies.md b/docs/2.Strategies.md index 694c03e..d11396b 100644 --- a/docs/2.Strategies.md +++ b/docs/2.Strategies.md @@ -112,12 +112,12 @@ phpunit.xml: ... - + ``` -Or set ``$_SERVER['DISABLE_STRATEGIES'] = true;`` in your bootstrap file of phpunit. +Or set ``$_SERVER['SLMLOCALE_DISABLE_STRATEGIES'] = true;`` in your bootstrap file of phpunit. Do not forget to enable phpunit strategy in config (example): diff --git a/src/SlmLocale/Strategy/PhpunitStrategy.php b/src/SlmLocale/Strategy/PhpunitStrategy.php index 429d36a..11a598b 100644 --- a/src/SlmLocale/Strategy/PhpunitStrategy.php +++ b/src/SlmLocale/Strategy/PhpunitStrategy.php @@ -34,7 +34,7 @@ private function stopPropagationIfPhpunit(LocaleEvent $event) return; } - $isPhpunit = array_key_exists('DISABLE_STRATEGIES', $_SERVER) && $_SERVER['DISABLE_STRATEGIES']; + $isPhpunit = array_key_exists('SLMLOCALE_DISABLE_STRATEGIES', $_SERVER) && $_SERVER['SLMLOCALE_DISABLE_STRATEGIES']; // if the file extension of the uri is found within the configured file_extensions, we do not rewrite and skip further processing if ($isPhpunit) { diff --git a/tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php b/tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php index 9016166..9518fe4 100644 --- a/tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php +++ b/tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php @@ -33,7 +33,7 @@ public function setup() public function testPreventStrategiesExecutionIfPhpunit() { - $_SERVER['DISABLE_STRATEGIES'] = true; + $_SERVER['SLMLOCALE_DISABLE_STRATEGIES'] = true; $event = new LocaleEvent(); $event->setSupported(['nl', 'de', 'en']); @@ -51,6 +51,6 @@ public function testPreventStrategiesExecutionIfPhpunit() $statusCode = $event->getResponse()->getStatusCode(); $this->assertEquals(200, $statusCode); - $_SERVER['DISABLE_STRATEGIES'] = false; + $_SERVER['SLMLOCALE_DISABLE_STRATEGIES'] = false; } } From b37d7526bf521c0da3ef4721a1e1a5be89f78c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20F=C3=BCsslin?= Date: Tue, 12 Dec 2017 17:43:47 +0100 Subject: [PATCH 5/6] added two test cases --- .../Strategy/PhpunitStrategyTest.php | 69 ++++++++++++++++--- 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php b/tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php index 9518fe4..5b559a7 100644 --- a/tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php +++ b/tests/SlmLocaleTest/Strategy/PhpunitStrategyTest.php @@ -9,8 +9,10 @@ namespace SlmLocaleTest\Strategy; use PHPUnit\Framework\TestCase; +use SlmLocale\Locale\Detector; use SlmLocale\LocaleEvent; use SlmLocale\Strategy\PhpunitStrategy; +use Zend\EventManager\EventManager; use Zend\Http\PhpEnvironment\Request as HttpRequest; use Zend\Http\PhpEnvironment\Response as HttpResponse; use Zend\Router\Http\TreeRouteStack as HttpRouter; @@ -28,6 +30,9 @@ public function setup() { $this->router = new HttpRouter(); + $this->event = new LocaleEvent(); + $this->event->setSupported(['nl', 'de', 'en']); + $this->strategy = new PhpunitStrategy(); } @@ -35,22 +40,70 @@ public function testPreventStrategiesExecutionIfPhpunit() { $_SERVER['SLMLOCALE_DISABLE_STRATEGIES'] = true; - $event = new LocaleEvent(); - $event->setSupported(['nl', 'de', 'en']); - $uri = 'http://username:password@example.com:8080/some/deep/path/some.file?withsomeparam=true'; $request = new HttpRequest(); $request->setUri($uri); - $event->setLocale('en'); - $event->setRequest($request); - $event->setResponse(new HttpResponse()); + $this->event->setLocale('en'); + $this->event->setRequest($request); + $this->event->setResponse(new HttpResponse()); - $this->strategy->found($event); + $this->strategy->found($this->event); - $statusCode = $event->getResponse()->getStatusCode(); + $statusCode = $this->event->getResponse()->getStatusCode(); $this->assertEquals(200, $statusCode); $_SERVER['SLMLOCALE_DISABLE_STRATEGIES'] = false; } + + public function testPhpunitStrategyCanPreventOtherStrategiesExecution() + { + $_SERVER['SLMLOCALE_DISABLE_STRATEGIES'] = true; + + $request = new HttpRequest(); + $request->setUri('http://example.com/css/style.css'); + $query = $request->getQuery(); + $query->lang = 'de'; + $request->setQuery($query); + $this->event->setRequest($request); + + $detector = new Detector(); + $detector->setEventManager(new EventManager()); + $detector->setSupported(['nl', 'de', 'en']); + $detector->setDefault('en'); + $detector->addStrategy($this->strategy); + $detector->addStrategy(new \SlmLocale\Strategy\QueryStrategy()); + $response = new HttpResponse(); + + $result = $detector->detect($request, $response); + $this->assertEquals('en', $result); + + $_SERVER['SLMLOCALE_DISABLE_STRATEGIES'] = false; + } + + public function testPhpunitStrategyDoesNotPreventOtherStrategiesExecution() + { + // can also be null / not set + $_SERVER['SLMLOCALE_DISABLE_STRATEGIES'] = false; + + $request = new HttpRequest(); + $request->setUri('http://example.com/css/style.css'); + $query = $request->getQuery(); + $query->lang = 'de'; + $request->setQuery($query); + $this->event->setRequest($request); + + $detector = new Detector(); + $detector->setEventManager(new EventManager()); + $detector->setSupported(['nl', 'de', 'en']); + $detector->setDefault('en'); + $detector->addStrategy($this->strategy); + $detector->addStrategy(new \SlmLocale\Strategy\QueryStrategy()); + $response = new HttpResponse(); + + $result = $detector->detect($request, $response); + $this->assertEquals('de', $result); + + $_SERVER['SLMLOCALE_DISABLE_STRATEGIES'] = false; + } } From b599a025aff9756cbb47f5c27ae5709422cf2714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20F=C3=BCsslin?= Date: Wed, 13 Dec 2017 18:24:46 +0100 Subject: [PATCH 6/6] updated strategies --- src/SlmLocale/Module.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/SlmLocale/Module.php b/src/SlmLocale/Module.php index 1b7c6d1..44f25c7 100644 --- a/src/SlmLocale/Module.php +++ b/src/SlmLocale/Module.php @@ -58,6 +58,23 @@ public function getConfig() } public function onBootstrap(EventInterface $e) + { + $app = $e->getApplication(); + $sm = $app->getServiceManager(); + $detector = $sm->get(Detector::class); + + $em = $app->getEventManager(); + $em->attach(MvcEvent::EVENT_ROUTE, function ($e) use ($app, $detector) { + $result = $detector->detect($app->getRequest(), $app->getResponse()); + if ($result instanceof ResponseInterface) { + return $result; + } else { + Locale::setDefault($result); + } + }, PHP_INT_MAX); + } + + /*public function onBootstrap(EventInterface $e) { $app = $e->getApplication(); $sm = $app->getServiceManager(); @@ -76,7 +93,7 @@ public function onBootstrap(EventInterface $e) * * The listener is attached at PHP_INT_MAX to return the response as early as * possible. - */ + * $em = $app->getEventManager(); $em->attach(MvcEvent::EVENT_ROUTE, function ($e) use ($result) { return $result; @@ -84,5 +101,5 @@ public function onBootstrap(EventInterface $e) } else { Locale::setDefault($result); } - } + }*/ }