<?php
namespace Symfony\Component\HttpKernel\Tests\EventListener;
use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Routing\RequestContext;
class RouterListenerTest extends \PHPUnit_Framework_TestCase {
protected function setUp() {
if (!class_exists('Symfony\\Component\\EventDispatcher\\EventDispatcher')) {
$this
->markTestSkipped('The "EventDispatcher" component is not available');
}
if (!class_exists('Symfony\\Component\\Routing\\Router')) {
$this
->markTestSkipped('The "Routing" component is not available');
}
}
public function testPort($defaultHttpPort, $defaultHttpsPort, $uri, $expectedHttpPort, $expectedHttpsPort) {
$urlMatcher = $this
->getMockBuilder('Symfony\\Component\\Routing\\Matcher\\UrlMatcherInterface')
->disableOriginalConstructor()
->getMock();
$context = new RequestContext();
$context
->setHttpPort($defaultHttpPort);
$context
->setHttpsPort($defaultHttpsPort);
$urlMatcher
->expects($this
->any())
->method('getContext')
->will($this
->returnValue($context));
$listener = new RouterListener($urlMatcher);
$event = $this
->createGetResponseEventForUri($uri);
$listener
->onKernelRequest($event);
$this
->assertEquals($expectedHttpPort, $context
->getHttpPort());
$this
->assertEquals($expectedHttpsPort, $context
->getHttpsPort());
$this
->assertEquals(0 === strpos($uri, 'https') ? 'https' : 'http', $context
->getScheme());
}
public function getPortData() {
return array(
array(
80,
443,
'http://localhost/',
80,
443,
),
array(
80,
443,
'http://localhost:90/',
90,
443,
),
array(
80,
443,
'https://localhost/',
80,
443,
),
array(
80,
443,
'https://localhost:90/',
80,
90,
),
);
}
private function createGetResponseEventForUri($uri) {
$kernel = $this
->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
$request = Request::create($uri);
$request->attributes
->set('_controller', null);
return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
}
public function testInvalidMatcher() {
new RouterListener(new \stdClass());
}
public function testRequestMatcher() {
$kernel = $this
->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
$request = Request::create('http://localhost/');
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
$requestMatcher = $this
->getMock('Symfony\\Component\\Routing\\Matcher\\RequestMatcherInterface');
$requestMatcher
->expects($this
->once())
->method('matchRequest')
->with($this
->isInstanceOf('Symfony\\Component\\HttpFoundation\\Request'))
->will($this
->returnValue(array()));
$listener = new RouterListener($requestMatcher, new RequestContext());
$listener
->onKernelRequest($event);
}
public function testSubRequestWithDifferentMethod() {
$kernel = $this
->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
$request = Request::create('http://localhost/', 'post');
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
$requestMatcher = $this
->getMock('Symfony\\Component\\Routing\\Matcher\\RequestMatcherInterface');
$requestMatcher
->expects($this
->any())
->method('matchRequest')
->with($this
->isInstanceOf('Symfony\\Component\\HttpFoundation\\Request'))
->will($this
->returnValue(array()));
$context = new RequestContext();
$requestMatcher
->expects($this
->any())
->method('getContext')
->will($this
->returnValue($context));
$listener = new RouterListener($requestMatcher, new RequestContext());
$listener
->onKernelRequest($event);
$kernel = $this
->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
$request = Request::create('http://localhost/', 'get');
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::SUB_REQUEST);
$listener
->onKernelRequest($event);
$this
->assertEquals('GET', $context
->getMethod());
}
}