diff --git a/composer.json b/composer.json index af8b696..0e38b51 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/src/SlmLocale/Strategy/HostStrategy.php b/src/SlmLocale/Strategy/HostStrategy.php index e9502a8..bb7bf96 100644 --- a/src/SlmLocale/Strategy/HostStrategy.php +++ b/src/SlmLocale/Strategy/HostStrategy.php @@ -192,7 +192,7 @@ public function assemble(LocaleEvent $event) $port = $event->getRequest()->getServer()->get('SERVER_PORT'); $hostname = str_replace(self::LOCALE_KEY, $tld, $this->getDomain()); - if ($port != 80) { + if (null !== $port && 80 != $port) { $hostname .= ':' . $port; } @@ -201,4 +201,4 @@ public function assemble(LocaleEvent $event) return $uri; } -} \ No newline at end of file +} diff --git a/tests/SlmLocaleTest/Strategy/HostStrategyTest.php b/tests/SlmLocaleTest/Strategy/HostStrategyTest.php new file mode 100644 index 0000000..fd74961 --- /dev/null +++ b/tests/SlmLocaleTest/Strategy/HostStrategyTest.php @@ -0,0 +1,124 @@ +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); + } +}