Skip to content

Commit

Permalink
Merge pull request #61 from prolic/host
Browse files Browse the repository at this point in the history
create correct urls for locale menu with host strategy
  • Loading branch information
Jurian Sluiman committed Nov 10, 2014
2 parents 87e605f + 5fa6172 commit 986835b
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"require-dev": {
"zendframework/zend-console": "~2.1",
"zendframework/zend-mvc": "~2.1"
"zendframework/zend-mvc": "~2.1",
"phpunit/phpunit": "4.2.*"
},
"suggest": {
"zendframework/zend-mvc": "For using the router in the UriPath strategy"
Expand Down
4 changes: 2 additions & 2 deletions src/SlmLocale/Locale/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@ public function detect(RequestInterface $request, ResponseInterface $response =
return $locale;
}

public function assemble($locale, $uri)
public function assemble($locale, $uri, RequestInterface $request)
{
$event = new LocaleEvent(LocaleEvent::EVENT_ASSEMBLE, $this);
$event->setLocale($locale);
$event->setRequest($request);

if ($this->hasSupported()) {
$event->setSupported($this->getSupported());
Expand All @@ -187,7 +188,6 @@ public function assemble($locale, $uri)

$events = $this->getEventManager();
$results = $events->trigger($event);

if (!$results->stopped()) {
return $uri;
}
Expand Down
34 changes: 33 additions & 1 deletion src/SlmLocale/Strategy/HostStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
namespace SlmLocale\Strategy;

use SlmLocale\LocaleEvent;
use SlmLocale\Strategy\Exception\InvalidArgumentException;
use Zend\Uri\Uri;

class HostStrategy extends AbstractStrategy
{
Expand Down Expand Up @@ -169,4 +171,34 @@ protected function getAliasForLocale($locale)
}
}
}
}

/**
* {@inheritdoc}
*/
public function assemble(LocaleEvent $event)
{
$locale = $event->getLocale();

foreach ($this->getAliases() as $alias => $item) {
if ($item == $locale) {
$tld = $alias;
}
}

if (!isset($tld)) {
throw new InvalidArgumentException('No matching tld found for current locale');
}

$port = $event->getRequest()->getServer()->get('SERVER_PORT');
$hostname = str_replace(self::LOCALE_KEY, $tld, $this->getDomain());

if (null !== $port && 80 != $port) {
$hostname .= ':' . $port;
}

$uri = $event->getUri();
$uri->setHost($hostname);

return $uri;
}
}
5 changes: 2 additions & 3 deletions src/SlmLocale/View/Helper/LocaleUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public function __invoke($locale, $name = null, $params = array(), $options = ar
$url = $this->getRequest()->getUri()->getPath();
}

return $this->getDetector()->assemble($locale, $url);
return $this->getDetector()->assemble($locale, $url, $this->getRequest());
}

}
}
124 changes: 124 additions & 0 deletions tests/SlmLocaleTest/Strategy/HostStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace SlmLocaleTest\Strategy;

use PHPUnit_Framework_TestCase as TestCase;
use SlmLocale\LocaleEvent;
use SlmLocale\Strategy\HostStrategy;
use Zend\Http\PhpEnvironment\Request;
use Zend\Stdlib\Parameters;
use Zend\Uri\Uri;

class HostStrategyTest extends TestCase
{
public function testDetectNonHttpRequestReturnsNull()
{
$event = new LocaleEvent();
$event->setRequest($this->getMockForAbstractClass('Zend\Stdlib\RequestInterface'));

$strategy = new HostStrategy();
$this->assertNull($strategy->detect($event));
}

public function testDetectWithoutSupportedReturnsNull()
{
$event = new LocaleEvent();
$event->setRequest($this->getMockForAbstractClass('Zend\Http\Request'));
$event->setSupported(array());

$strategy = new HostStrategy();
$this->assertNull($strategy->detect($event));
}

/**
* @expectedException \SlmLocale\Strategy\Exception\InvalidArgumentException
*/
public function testDetectWithoutDomainThrowsInvalidArgumentException()
{
$event = new LocaleEvent();
$event->setRequest($this->getMockForAbstractClass('Zend\Http\Request'));
$event->setSupported(array('en_GB', 'de_DE'));

$strategy = new HostStrategy();
$strategy->setOptions(array('domain' => 'test'));
$this->assertNull($strategy->detect($event));
}

public function testDetectUnsupportedReturnsNull()
{
$request = new Request();
$request->setUri('http://test.fr');
$event = new LocaleEvent();
$event->setRequest($request);
$event->setSupported(array('en_GB', 'de_DE'));

$strategy = new HostStrategy();
$strategy->setOptions(array(
'domain' => 'test.:locale',
'aliases' => array('de' => 'de_DE', 'co.uk' => 'en_GB')
));
$result = $strategy->detect($event);

$this->assertNull($result);
}

public function testDetect()
{
$request = new Request();
$request->setUri('http://test.de');
$event = new LocaleEvent();
$event->setRequest($request);
$event->setSupported(array('en_GB', 'de_DE'));

$strategy = new HostStrategy();
$strategy->setOptions(array(
'domain' => 'test.:locale',
'aliases' => array('de' => 'de_DE', 'co.uk' => 'en_GB')
));
$result = $strategy->detect($event);

$this->assertSame('de_DE', $result);
}

public function testAssemble()
{
$params = new Parameters(array('SERVER_NAME' => 'test.co.uk'));
$request = new Request();
$request->setServer($params);

$event = new LocaleEvent();
$event->setLocale('de_DE');
$event->setUri(new Uri('http://test.co.uk'));
$event->setRequest($request);

$strategy = new HostStrategy();
$strategy->setOptions(array(
'domain' => 'test.:locale',
'aliases' => array('de' => 'de_DE', 'co.uk' => 'en_GB')
));

$result = $strategy->assemble($event)->getHost();
$this->assertSame('test.de', $result);
}

public function testAssembleWithPort()
{
$params = new Parameters(array('SERVER_NAME' => 'test.co.uk', 'SERVER_PORT' => 8080));
$request = new Request();
$request->setServer($params);

$event = new LocaleEvent();
$event->setLocale('de_DE');
$event->setUri(new Uri('http://test.co.uk'));
$event->setRequest($request);

$strategy = new HostStrategy();
$strategy->setOptions(array(
'domain' => 'test.:locale',
'aliases' => array('de' => 'de_DE', 'co.uk' => 'en_GB')
));

$result = $strategy->assemble($event)->getHost();
$this->assertSame('test.de:8080', $result);
}
}

0 comments on commit 986835b

Please sign in to comment.