Skip to content

Commit

Permalink
add host strategy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prolic committed Nov 10, 2014
1 parent 2d08e08 commit 5fa6172
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 3 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/Strategy/HostStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -201,4 +201,4 @@ public function assemble(LocaleEvent $event)

return $uri;
}
}
}
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 5fa6172

Please sign in to comment.