4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\EventListener;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpKernel\EventListener\RouterListener;
17 use Symfony\Component\HttpKernel\HttpKernelInterface;
18 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
19 use Symfony\Component\Routing\RequestContext;
21 class RouterListenerTest extends TestCase
23 private $requestStack;
25 protected function setUp()
27 $this->requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->disableOriginalConstructor()->getMock();
31 * @dataProvider getPortData
33 public function testPort($defaultHttpPort, $defaultHttpsPort, $uri, $expectedHttpPort, $expectedHttpsPort)
35 $urlMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcherInterface')
36 ->disableOriginalConstructor()
38 $context = new RequestContext();
39 $context->setHttpPort($defaultHttpPort);
40 $context->setHttpsPort($defaultHttpsPort);
41 $urlMatcher->expects($this->any())
42 ->method('getContext')
43 ->will($this->returnValue($context));
45 $listener = new RouterListener($urlMatcher, $this->requestStack);
46 $event = $this->createGetResponseEventForUri($uri);
47 $listener->onKernelRequest($event);
49 $this->assertEquals($expectedHttpPort, $context->getHttpPort());
50 $this->assertEquals($expectedHttpsPort, $context->getHttpsPort());
51 $this->assertEquals(0 === strpos($uri, 'https') ? 'https' : 'http', $context->getScheme());
54 public function getPortData()
57 array(80, 443, 'http://localhost/', 80, 443),
58 array(80, 443, 'http://localhost:90/', 90, 443),
59 array(80, 443, 'https://localhost/', 80, 443),
60 array(80, 443, 'https://localhost:90/', 80, 90),
67 * @return GetResponseEvent
69 private function createGetResponseEventForUri($uri)
71 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
72 $request = Request::create($uri);
73 $request->attributes->set('_controller', null); // Prevents going in to routing process
75 return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
79 * @expectedException \InvalidArgumentException
81 public function testInvalidMatcher()
83 new RouterListener(new \stdClass(), $this->requestStack);
86 public function testRequestMatcher()
88 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
89 $request = Request::create('http://localhost/');
90 $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
92 $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
93 $requestMatcher->expects($this->once())
94 ->method('matchRequest')
95 ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
96 ->will($this->returnValue(array()));
98 $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext());
99 $listener->onKernelRequest($event);
102 public function testSubRequestWithDifferentMethod()
104 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
105 $request = Request::create('http://localhost/', 'post');
106 $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
108 $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
109 $requestMatcher->expects($this->any())
110 ->method('matchRequest')
111 ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
112 ->will($this->returnValue(array()));
114 $context = new RequestContext();
116 $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext());
117 $listener->onKernelRequest($event);
119 // sub-request with another HTTP method
120 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
121 $request = Request::create('http://localhost/', 'get');
122 $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::SUB_REQUEST);
124 $listener->onKernelRequest($event);
126 $this->assertEquals('GET', $context->getMethod());
130 * @dataProvider getLoggingParameterData
132 public function testLoggingParameter($parameter, $log)
134 $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
135 $requestMatcher->expects($this->once())
136 ->method('matchRequest')
137 ->will($this->returnValue($parameter));
139 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
140 $logger->expects($this->once())
142 ->with($this->equalTo($log));
144 $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
145 $request = Request::create('http://localhost/');
147 $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext(), $logger);
148 $listener->onKernelRequest(new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST));
151 public function getLoggingParameterData()
154 array(array('_route' => 'foo'), 'Matched route "foo".'),
155 array(array(), 'Matched route "n/a".'),