Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,33 @@ Inject the detected language here with the following code:
<html lang="<?= Locale::getPrimaryLanguage(Locale::getDefault())?>">
```

### 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:
```
<phpunit...>
...
<php>
<server name="DISABLE_URIPATHSTRATEGY" value="true" />
</php>
</phpunit>
```

Or set ``$_SERVER['DISABLE_URIPATHSTRATEGY'] = true;`` in your bootstrap file of phpunit.

### Create a list of available locales

T.B.D
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/SlmLocale/Strategy/UriPathStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions tests/SlmLocaleTest/Strategy/UriPathStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,28 @@ public function testAssembleWorksWithAliasesToo()
$this->assertEquals($expected, $actual);
}

public function testDisableUriPathStrategyPhpunit()
{
$_SERVER['DISABLE_URIPATHSTRATEGY'] = true;

$uri = 'http://username:[email protected]: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:[email protected]: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();
Expand Down