Yaffs site version 1.1
[yaffs-website] / vendor / symfony / http-kernel / Tests / HttpKernelTest.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;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpKernel\HttpKernel;
16 use Symfony\Component\HttpKernel\HttpKernelInterface;
17 use Symfony\Component\HttpKernel\KernelEvents;
18 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
19 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
20 use Symfony\Component\HttpFoundation\Request;
21 use Symfony\Component\HttpFoundation\Response;
22 use Symfony\Component\HttpFoundation\RedirectResponse;
23 use Symfony\Component\EventDispatcher\EventDispatcher;
24
25 class HttpKernelTest extends TestCase
26 {
27     /**
28      * @expectedException \RuntimeException
29      */
30     public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrue()
31     {
32         $kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
33
34         $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
35     }
36
37     /**
38      * @expectedException \RuntimeException
39      */
40     public function testHandleWhenControllerThrowsAnExceptionAndCatchIsFalseAndNoListenerIsRegistered()
41     {
42         $kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
43
44         $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
45     }
46
47     public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrueWithAHandlingListener()
48     {
49         $dispatcher = new EventDispatcher();
50         $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
51             $event->setResponse(new Response($event->getException()->getMessage()));
52         });
53
54         $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new \RuntimeException('foo'); }));
55         $response = $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
56
57         $this->assertEquals('500', $response->getStatusCode());
58         $this->assertEquals('foo', $response->getContent());
59     }
60
61     public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrueWithANonHandlingListener()
62     {
63         $exception = new \RuntimeException();
64
65         $dispatcher = new EventDispatcher();
66         $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
67             // should set a response, but does not
68         });
69
70         $kernel = new HttpKernel($dispatcher, $this->getResolver(function () use ($exception) { throw $exception; }));
71
72         try {
73             $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
74             $this->fail('LogicException expected');
75         } catch (\RuntimeException $e) {
76             $this->assertSame($exception, $e);
77         }
78     }
79
80     public function testHandleExceptionWithARedirectionResponse()
81     {
82         $dispatcher = new EventDispatcher();
83         $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
84             $event->setResponse(new RedirectResponse('/login', 301));
85         });
86
87         $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new AccessDeniedHttpException(); }));
88         $response = $kernel->handle(new Request());
89
90         $this->assertEquals('301', $response->getStatusCode());
91         $this->assertEquals('/login', $response->headers->get('Location'));
92     }
93
94     public function testHandleHttpException()
95     {
96         $dispatcher = new EventDispatcher();
97         $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
98             $event->setResponse(new Response($event->getException()->getMessage()));
99         });
100
101         $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new MethodNotAllowedHttpException(array('POST')); }));
102         $response = $kernel->handle(new Request());
103
104         $this->assertEquals('405', $response->getStatusCode());
105         $this->assertEquals('POST', $response->headers->get('Allow'));
106     }
107
108     /**
109      * @dataProvider getStatusCodes
110      */
111     public function testHandleWhenAnExceptionIsHandledWithASpecificStatusCode($responseStatusCode, $expectedStatusCode)
112     {
113         $dispatcher = new EventDispatcher();
114         $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) use ($responseStatusCode, $expectedStatusCode) {
115             $event->setResponse(new Response('', $responseStatusCode, array('X-Status-Code' => $expectedStatusCode)));
116         });
117
118         $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { throw new \RuntimeException(); }));
119         $response = $kernel->handle(new Request());
120
121         $this->assertEquals($expectedStatusCode, $response->getStatusCode());
122         $this->assertFalse($response->headers->has('X-Status-Code'));
123     }
124
125     public function getStatusCodes()
126     {
127         return array(
128             array(200, 404),
129             array(404, 200),
130             array(301, 200),
131             array(500, 200),
132         );
133     }
134
135     public function testHandleWhenAListenerReturnsAResponse()
136     {
137         $dispatcher = new EventDispatcher();
138         $dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
139             $event->setResponse(new Response('hello'));
140         });
141
142         $kernel = new HttpKernel($dispatcher, $this->getResolver());
143
144         $this->assertEquals('hello', $kernel->handle(new Request())->getContent());
145     }
146
147     /**
148      * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
149      */
150     public function testHandleWhenNoControllerIsFound()
151     {
152         $dispatcher = new EventDispatcher();
153         $kernel = new HttpKernel($dispatcher, $this->getResolver(false));
154
155         $kernel->handle(new Request());
156     }
157
158     /**
159      * @expectedException \LogicException
160      */
161     public function testHandleWhenTheControllerIsNotACallable()
162     {
163         $dispatcher = new EventDispatcher();
164         $kernel = new HttpKernel($dispatcher, $this->getResolver('foobar'));
165
166         $kernel->handle(new Request());
167     }
168
169     public function testHandleWhenTheControllerIsAClosure()
170     {
171         $response = new Response('foo');
172         $dispatcher = new EventDispatcher();
173         $kernel = new HttpKernel($dispatcher, $this->getResolver(function () use ($response) { return $response; }));
174
175         $this->assertSame($response, $kernel->handle(new Request()));
176     }
177
178     public function testHandleWhenTheControllerIsAnObjectWithInvoke()
179     {
180         $dispatcher = new EventDispatcher();
181         $kernel = new HttpKernel($dispatcher, $this->getResolver(new Controller()));
182
183         $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
184     }
185
186     public function testHandleWhenTheControllerIsAFunction()
187     {
188         $dispatcher = new EventDispatcher();
189         $kernel = new HttpKernel($dispatcher, $this->getResolver('Symfony\Component\HttpKernel\Tests\controller_func'));
190
191         $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
192     }
193
194     public function testHandleWhenTheControllerIsAnArray()
195     {
196         $dispatcher = new EventDispatcher();
197         $kernel = new HttpKernel($dispatcher, $this->getResolver(array(new Controller(), 'controller')));
198
199         $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
200     }
201
202     public function testHandleWhenTheControllerIsAStaticArray()
203     {
204         $dispatcher = new EventDispatcher();
205         $kernel = new HttpKernel($dispatcher, $this->getResolver(array('Symfony\Component\HttpKernel\Tests\Controller', 'staticcontroller')));
206
207         $this->assertResponseEquals(new Response('foo'), $kernel->handle(new Request()));
208     }
209
210     /**
211      * @expectedException \LogicException
212      */
213     public function testHandleWhenTheControllerDoesNotReturnAResponse()
214     {
215         $dispatcher = new EventDispatcher();
216         $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
217
218         $kernel->handle(new Request());
219     }
220
221     public function testHandleWhenTheControllerDoesNotReturnAResponseButAViewIsRegistered()
222     {
223         $dispatcher = new EventDispatcher();
224         $dispatcher->addListener(KernelEvents::VIEW, function ($event) {
225             $event->setResponse(new Response($event->getControllerResult()));
226         });
227         $kernel = new HttpKernel($dispatcher, $this->getResolver(function () { return 'foo'; }));
228
229         $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
230     }
231
232     public function testHandleWithAResponseListener()
233     {
234         $dispatcher = new EventDispatcher();
235         $dispatcher->addListener(KernelEvents::RESPONSE, function ($event) {
236             $event->setResponse(new Response('foo'));
237         });
238         $kernel = new HttpKernel($dispatcher, $this->getResolver());
239
240         $this->assertEquals('foo', $kernel->handle(new Request())->getContent());
241     }
242
243     public function testTerminate()
244     {
245         $dispatcher = new EventDispatcher();
246         $kernel = new HttpKernel($dispatcher, $this->getResolver());
247         $dispatcher->addListener(KernelEvents::TERMINATE, function ($event) use (&$called, &$capturedKernel, &$capturedRequest, &$capturedResponse) {
248             $called = true;
249             $capturedKernel = $event->getKernel();
250             $capturedRequest = $event->getRequest();
251             $capturedResponse = $event->getResponse();
252         });
253
254         $kernel->terminate($request = Request::create('/'), $response = new Response());
255         $this->assertTrue($called);
256         $this->assertEquals($kernel, $capturedKernel);
257         $this->assertEquals($request, $capturedRequest);
258         $this->assertEquals($response, $capturedResponse);
259     }
260
261     public function testVerifyRequestStackPushPopDuringHandle()
262     {
263         $request = new Request();
264
265         $stack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->setMethods(array('push', 'pop'))->getMock();
266         $stack->expects($this->at(0))->method('push')->with($this->equalTo($request));
267         $stack->expects($this->at(1))->method('pop');
268
269         $dispatcher = new EventDispatcher();
270         $kernel = new HttpKernel($dispatcher, $this->getResolver(), $stack);
271
272         $kernel->handle($request, HttpKernelInterface::MASTER_REQUEST);
273     }
274
275     /**
276      * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
277      */
278     public function testInconsistentClientIpsOnMasterRequests()
279     {
280         $dispatcher = new EventDispatcher();
281         $dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
282             $event->getRequest()->getClientIp();
283         });
284
285         $kernel = new HttpKernel($dispatcher, $this->getResolver());
286
287         $request = new Request();
288         $request->setTrustedProxies(array('1.1.1.1'));
289         $request->server->set('REMOTE_ADDR', '1.1.1.1');
290         $request->headers->set('FORWARDED', 'for=2.2.2.2');
291         $request->headers->set('X_FORWARDED_FOR', '3.3.3.3');
292
293         $kernel->handle($request, $kernel::MASTER_REQUEST, false);
294     }
295
296     protected function getResolver($controller = null)
297     {
298         if (null === $controller) {
299             $controller = function () { return new Response('Hello'); };
300         }
301
302         $resolver = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface')->getMock();
303         $resolver->expects($this->any())
304             ->method('getController')
305             ->will($this->returnValue($controller));
306         $resolver->expects($this->any())
307             ->method('getArguments')
308             ->will($this->returnValue(array()));
309
310         return $resolver;
311     }
312
313     protected function assertResponseEquals(Response $expected, Response $actual)
314     {
315         $expected->setDate($actual->getDate());
316         $this->assertEquals($expected, $actual);
317     }
318 }
319
320 class Controller
321 {
322     public function __invoke()
323     {
324         return new Response('foo');
325     }
326
327     public function controller()
328     {
329         return new Response('foo');
330     }
331
332     public static function staticController()
333     {
334         return new Response('foo');
335     }
336 }
337
338 function controller_func()
339 {
340     return new Response('foo');
341 }