Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / http-kernel / Tests / EventListener / RouterListenerTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests\EventListener;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\EventDispatcher\EventDispatcher;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpFoundation\RequestStack;
18 use Symfony\Component\HttpFoundation\Response;
19 use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
20 use Symfony\Component\HttpKernel\Controller\ControllerResolver;
21 use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
22 use Symfony\Component\HttpKernel\EventListener\RouterListener;
23 use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener;
24 use Symfony\Component\HttpKernel\HttpKernelInterface;
25 use Symfony\Component\HttpKernel\HttpKernel;
26 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
27 use Symfony\Component\Routing\Exception\NoConfigurationException;
28 use Symfony\Component\Routing\RequestContext;
29
30 class RouterListenerTest extends TestCase
31 {
32     private $requestStack;
33
34     protected function setUp()
35     {
36         $this->requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->disableOriginalConstructor()->getMock();
37     }
38
39     /**
40      * @dataProvider getPortData
41      */
42     public function testPort($defaultHttpPort, $defaultHttpsPort, $uri, $expectedHttpPort, $expectedHttpsPort)
43     {
44         $urlMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcherInterface')
45             ->disableOriginalConstructor()
46             ->getMock();
47         $context = new RequestContext();
48         $context->setHttpPort($defaultHttpPort);
49         $context->setHttpsPort($defaultHttpsPort);
50         $urlMatcher->expects($this->any())
51             ->method('getContext')
52             ->will($this->returnValue($context));
53
54         $listener = new RouterListener($urlMatcher, $this->requestStack);
55         $event = $this->createGetResponseEventForUri($uri);
56         $listener->onKernelRequest($event);
57
58         $this->assertEquals($expectedHttpPort, $context->getHttpPort());
59         $this->assertEquals($expectedHttpsPort, $context->getHttpsPort());
60         $this->assertEquals(0 === strpos($uri, 'https') ? 'https' : 'http', $context->getScheme());
61     }
62
63     public function getPortData()
64     {
65         return array(
66             array(80, 443, 'http://localhost/', 80, 443),
67             array(80, 443, 'http://localhost:90/', 90, 443),
68             array(80, 443, 'https://localhost/', 80, 443),
69             array(80, 443, 'https://localhost:90/', 80, 90),
70         );
71     }
72
73     /**
74      * @param string $uri
75      *
76      * @return GetResponseEvent
77      */
78     private function createGetResponseEventForUri($uri)
79     {
80         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
81         $request = Request::create($uri);
82         $request->attributes->set('_controller', null); // Prevents going in to routing process
83
84         return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
85     }
86
87     /**
88      * @expectedException \InvalidArgumentException
89      */
90     public function testInvalidMatcher()
91     {
92         new RouterListener(new \stdClass(), $this->requestStack);
93     }
94
95     public function testRequestMatcher()
96     {
97         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
98         $request = Request::create('http://localhost/');
99         $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
100
101         $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
102         $requestMatcher->expects($this->once())
103                        ->method('matchRequest')
104                        ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
105                        ->will($this->returnValue(array()));
106
107         $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext());
108         $listener->onKernelRequest($event);
109     }
110
111     public function testSubRequestWithDifferentMethod()
112     {
113         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
114         $request = Request::create('http://localhost/', 'post');
115         $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
116
117         $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
118         $requestMatcher->expects($this->any())
119                        ->method('matchRequest')
120                        ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
121                        ->will($this->returnValue(array()));
122
123         $context = new RequestContext();
124
125         $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext());
126         $listener->onKernelRequest($event);
127
128         // sub-request with another HTTP method
129         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
130         $request = Request::create('http://localhost/', 'get');
131         $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::SUB_REQUEST);
132
133         $listener->onKernelRequest($event);
134
135         $this->assertEquals('GET', $context->getMethod());
136     }
137
138     /**
139      * @dataProvider getLoggingParameterData
140      */
141     public function testLoggingParameter($parameter, $log, $parameters)
142     {
143         $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
144         $requestMatcher->expects($this->once())
145             ->method('matchRequest')
146             ->will($this->returnValue($parameter));
147
148         $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
149         $logger->expects($this->once())
150             ->method('info')
151             ->with($this->equalTo($log), $this->equalTo($parameters));
152
153         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
154         $request = Request::create('http://localhost/');
155
156         $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext(), $logger);
157         $listener->onKernelRequest(new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST));
158     }
159
160     public function getLoggingParameterData()
161     {
162         return array(
163             array(array('_route' => 'foo'), 'Matched route "{route}".', array('route' => 'foo', 'route_parameters' => array('_route' => 'foo'), 'request_uri' => 'http://localhost/', 'method' => 'GET')),
164             array(array(), 'Matched route "{route}".', array('route' => 'n/a', 'route_parameters' => array(), 'request_uri' => 'http://localhost/', 'method' => 'GET')),
165         );
166     }
167
168     public function testWithBadRequest()
169     {
170         $requestStack = new RequestStack();
171
172         $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
173         $requestMatcher->expects($this->never())->method('matchRequest');
174
175         $dispatcher = new EventDispatcher();
176         $dispatcher->addSubscriber(new ValidateRequestListener());
177         $dispatcher->addSubscriber(new RouterListener($requestMatcher, $requestStack, new RequestContext()));
178         $dispatcher->addSubscriber(new ExceptionListener(function () {
179             return new Response('Exception handled', 400);
180         }));
181
182         $kernel = new HttpKernel($dispatcher, new ControllerResolver(), $requestStack, new ArgumentResolver());
183
184         $request = Request::create('http://localhost/');
185         $request->headers->set('host', '###');
186         $response = $kernel->handle($request);
187         $this->assertSame(400, $response->getStatusCode());
188     }
189
190     public function testNoRoutingConfigurationResponse()
191     {
192         $requestStack = new RequestStack();
193
194         $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
195         $requestMatcher
196             ->expects($this->once())
197             ->method('matchRequest')
198             ->willThrowException(new NoConfigurationException())
199         ;
200
201         $dispatcher = new EventDispatcher();
202         $dispatcher->addSubscriber(new RouterListener($requestMatcher, $requestStack, new RequestContext()));
203
204         $kernel = new HttpKernel($dispatcher, new ControllerResolver(), $requestStack, new ArgumentResolver());
205
206         $request = Request::create('http://localhost/');
207         $response = $kernel->handle($request);
208         $this->assertSame(404, $response->getStatusCode());
209         $this->assertContains('Welcome', $response->getContent());
210     }
211
212     /**
213      * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
214      */
215     public function testRequestWithBadHost()
216     {
217         $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
218         $request = Request::create('http://bad host %22/');
219         $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
220
221         $requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
222
223         $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext());
224         $listener->onKernelRequest($event);
225     }
226 }