Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / http-kernel / Tests / DataCollector / RequestDataCollectorTest.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\DataCollector;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\RedirectResponse;
16 use Symfony\Component\HttpFoundation\Session\Session;
17 use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
18 use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
19 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
20 use Symfony\Component\HttpKernel\HttpKernel;
21 use Symfony\Component\HttpKernel\HttpKernelInterface;
22 use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
23 use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
24 use Symfony\Component\HttpFoundation\Request;
25 use Symfony\Component\HttpFoundation\Response;
26 use Symfony\Component\HttpFoundation\Cookie;
27 use Symfony\Component\EventDispatcher\EventDispatcher;
28 use Symfony\Component\VarDumper\Cloner\Data;
29 use Symfony\Component\VarDumper\Cloner\VarCloner;
30
31 class RequestDataCollectorTest extends TestCase
32 {
33     public function testCollect()
34     {
35         $c = new RequestDataCollector();
36
37         $c->collect($request = $this->createRequest(), $this->createResponse());
38
39         $cloner = new VarCloner();
40         $attributes = $c->getRequestAttributes();
41
42         $this->assertSame('request', $c->getName());
43         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestHeaders());
44         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestServer());
45         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestCookies());
46         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $attributes);
47         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestRequest());
48         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestQuery());
49         $this->assertSame('html', $c->getFormat());
50         $this->assertEquals('foobar', $c->getRoute());
51         $this->assertEquals(array('name' => $cloner->cloneVar(array('name' => 'foo'))->seek('name')), $c->getRouteParams());
52         $this->assertSame(array(), $c->getSessionAttributes());
53         $this->assertSame('en', $c->getLocale());
54         $this->assertEquals($cloner->cloneVar($request->attributes->get('resource')), $attributes->get('resource'));
55         $this->assertEquals($cloner->cloneVar($request->attributes->get('object')), $attributes->get('object'));
56
57         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getResponseHeaders());
58         $this->assertSame('OK', $c->getStatusText());
59         $this->assertSame(200, $c->getStatusCode());
60         $this->assertSame('application/json', $c->getContentType());
61     }
62
63     public function testCollectWithoutRouteParams()
64     {
65         $request = $this->createRequest(array());
66
67         $c = new RequestDataCollector();
68         $c->collect($request, $this->createResponse());
69
70         $this->assertEquals(array(), $c->getRouteParams());
71     }
72
73     public function testKernelResponseDoesNotStartSession()
74     {
75         $kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
76         $request = new Request();
77         $session = new Session(new MockArraySessionStorage());
78         $request->setSession($session);
79         $response = new Response();
80
81         $c = new RequestDataCollector();
82         $c->onKernelResponse(new FilterResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response));
83
84         $this->assertFalse($session->isStarted());
85     }
86
87     /**
88      * @dataProvider provideControllerCallables
89      */
90     public function testControllerInspection($name, $callable, $expected)
91     {
92         $c = new RequestDataCollector();
93         $request = $this->createRequest();
94         $response = $this->createResponse();
95         $this->injectController($c, $callable, $request);
96         $c->collect($request, $response);
97
98         $this->assertSame($expected, $c->getController(), sprintf('Testing: %s', $name));
99     }
100
101     public function provideControllerCallables()
102     {
103         // make sure we always match the line number
104         $r1 = new \ReflectionMethod($this, 'testControllerInspection');
105         $r2 = new \ReflectionMethod($this, 'staticControllerMethod');
106         $r3 = new \ReflectionClass($this);
107
108         // test name, callable, expected
109         return array(
110             array(
111                 '"Regular" callable',
112                 array($this, 'testControllerInspection'),
113                 array(
114                     'class' => __NAMESPACE__.'\RequestDataCollectorTest',
115                     'method' => 'testControllerInspection',
116                     'file' => __FILE__,
117                     'line' => $r1->getStartLine(),
118                 ),
119             ),
120
121             array(
122                 'Closure',
123                 function () { return 'foo'; },
124                 array(
125                     'class' => __NAMESPACE__.'\{closure}',
126                     'method' => null,
127                     'file' => __FILE__,
128                     'line' => __LINE__ - 5,
129                 ),
130             ),
131
132             array(
133                 'Static callback as string',
134                 __NAMESPACE__.'\RequestDataCollectorTest::staticControllerMethod',
135                 array(
136                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
137                     'method' => 'staticControllerMethod',
138                     'file' => __FILE__,
139                     'line' => $r2->getStartLine(),
140                 ),
141             ),
142
143             array(
144                 'Static callable with instance',
145                 array($this, 'staticControllerMethod'),
146                 array(
147                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
148                     'method' => 'staticControllerMethod',
149                     'file' => __FILE__,
150                     'line' => $r2->getStartLine(),
151                 ),
152             ),
153
154             array(
155                 'Static callable with class name',
156                 array('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest', 'staticControllerMethod'),
157                 array(
158                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
159                     'method' => 'staticControllerMethod',
160                     'file' => __FILE__,
161                     'line' => $r2->getStartLine(),
162                 ),
163             ),
164
165             array(
166                 'Callable with instance depending on __call()',
167                 array($this, 'magicMethod'),
168                 array(
169                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
170                     'method' => 'magicMethod',
171                     'file' => 'n/a',
172                     'line' => 'n/a',
173                 ),
174             ),
175
176             array(
177                 'Callable with class name depending on __callStatic()',
178                 array('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest', 'magicMethod'),
179                 array(
180                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
181                     'method' => 'magicMethod',
182                     'file' => 'n/a',
183                     'line' => 'n/a',
184                 ),
185             ),
186
187             array(
188                 'Invokable controller',
189                 $this,
190                 array(
191                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
192                     'method' => null,
193                     'file' => __FILE__,
194                     'line' => $r3->getStartLine(),
195                 ),
196             ),
197         );
198     }
199
200     public function testItIgnoresInvalidCallables()
201     {
202         $request = $this->createRequestWithSession();
203         $response = new RedirectResponse('/');
204
205         $c = new RequestDataCollector();
206         $c->collect($request, $response);
207
208         $this->assertSame('n/a', $c->getController());
209     }
210
211     protected function createRequest($routeParams = array('name' => 'foo'))
212     {
213         $request = Request::create('http://test.com/foo?bar=baz');
214         $request->attributes->set('foo', 'bar');
215         $request->attributes->set('_route', 'foobar');
216         $request->attributes->set('_route_params', $routeParams);
217         $request->attributes->set('resource', fopen(__FILE__, 'r'));
218         $request->attributes->set('object', new \stdClass());
219
220         return $request;
221     }
222
223     private function createRequestWithSession()
224     {
225         $request = $this->createRequest();
226         $request->attributes->set('_controller', 'Foo::bar');
227         $request->setSession(new Session(new MockArraySessionStorage()));
228         $request->getSession()->start();
229
230         return $request;
231     }
232
233     protected function createResponse()
234     {
235         $response = new Response();
236         $response->setStatusCode(200);
237         $response->headers->set('Content-Type', 'application/json');
238         $response->headers->set('X-Foo-Bar', null);
239         $response->headers->setCookie(new Cookie('foo', 'bar', 1, '/foo', 'localhost', true, true));
240         $response->headers->setCookie(new Cookie('bar', 'foo', new \DateTime('@946684800')));
241         $response->headers->setCookie(new Cookie('bazz', 'foo', '2000-12-12'));
242
243         return $response;
244     }
245
246     /**
247      * Inject the given controller callable into the data collector.
248      */
249     protected function injectController($collector, $controller, $request)
250     {
251         $resolver = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface')->getMock();
252         $httpKernel = new HttpKernel(new EventDispatcher(), $resolver, null, $this->getMockBuilder(ArgumentResolverInterface::class)->getMock());
253         $event = new FilterControllerEvent($httpKernel, $controller, $request, HttpKernelInterface::MASTER_REQUEST);
254         $collector->onKernelController($event);
255     }
256
257     /**
258      * Dummy method used as controller callable.
259      */
260     public static function staticControllerMethod()
261     {
262         throw new \LogicException('Unexpected method call');
263     }
264
265     /**
266      * Magic method to allow non existing methods to be called and delegated.
267      */
268     public function __call($method, $args)
269     {
270         throw new \LogicException('Unexpected method call');
271     }
272
273     /**
274      * Magic method to allow non existing methods to be called and delegated.
275      */
276     public static function __callStatic($method, $args)
277     {
278         throw new \LogicException('Unexpected method call');
279     }
280
281     public function __invoke()
282     {
283         throw new \LogicException('Unexpected method call');
284     }
285 }