Yaffs site version 1.1
[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\HttpKernel\HttpKernel;
16 use Symfony\Component\HttpKernel\HttpKernelInterface;
17 use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
18 use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
19 use Symfony\Component\HttpFoundation\Request;
20 use Symfony\Component\HttpFoundation\Response;
21 use Symfony\Component\HttpFoundation\Cookie;
22 use Symfony\Component\EventDispatcher\EventDispatcher;
23
24 class RequestDataCollectorTest extends TestCase
25 {
26     public function testCollect()
27     {
28         $c = new RequestDataCollector();
29
30         $c->collect($this->createRequest(), $this->createResponse());
31
32         $attributes = $c->getRequestAttributes();
33
34         $this->assertSame('request', $c->getName());
35         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestHeaders());
36         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestServer());
37         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestCookies());
38         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $attributes);
39         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestRequest());
40         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestQuery());
41         $this->assertSame('html', $c->getFormat());
42         $this->assertSame('foobar', $c->getRoute());
43         $this->assertSame(array('name' => 'foo'), $c->getRouteParams());
44         $this->assertSame(array(), $c->getSessionAttributes());
45         $this->assertSame('en', $c->getLocale());
46         $this->assertRegExp('/Resource\(stream#\d+\)/', $attributes->get('resource'));
47         $this->assertSame('Object(stdClass)', $attributes->get('object'));
48
49         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getResponseHeaders());
50         $this->assertSame('OK', $c->getStatusText());
51         $this->assertSame(200, $c->getStatusCode());
52         $this->assertSame('application/json', $c->getContentType());
53     }
54
55     /**
56      * Test various types of controller callables.
57      */
58     public function testControllerInspection()
59     {
60         // make sure we always match the line number
61         $r1 = new \ReflectionMethod($this, 'testControllerInspection');
62         $r2 = new \ReflectionMethod($this, 'staticControllerMethod');
63         $r3 = new \ReflectionClass($this);
64         // test name, callable, expected
65         $controllerTests = array(
66             array(
67                 '"Regular" callable',
68                 array($this, 'testControllerInspection'),
69                 array(
70                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
71                     'method' => 'testControllerInspection',
72                     'file' => __FILE__,
73                     'line' => $r1->getStartLine(),
74                 ),
75             ),
76
77             array(
78                 'Closure',
79                 function () { return 'foo'; },
80                 array(
81                     'class' => __NAMESPACE__.'\{closure}',
82                     'method' => null,
83                     'file' => __FILE__,
84                     'line' => __LINE__ - 5,
85                 ),
86             ),
87
88             array(
89                 'Static callback as string',
90                 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest::staticControllerMethod',
91                 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest::staticControllerMethod',
92             ),
93
94             array(
95                 'Static callable with instance',
96                 array($this, 'staticControllerMethod'),
97                 array(
98                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
99                     'method' => 'staticControllerMethod',
100                     'file' => __FILE__,
101                     'line' => $r2->getStartLine(),
102                 ),
103             ),
104
105             array(
106                 'Static callable with class name',
107                 array('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest', 'staticControllerMethod'),
108                 array(
109                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
110                     'method' => 'staticControllerMethod',
111                     'file' => __FILE__,
112                     'line' => $r2->getStartLine(),
113                 ),
114             ),
115
116             array(
117                 'Callable with instance depending on __call()',
118                 array($this, 'magicMethod'),
119                 array(
120                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
121                     'method' => 'magicMethod',
122                     'file' => 'n/a',
123                     'line' => 'n/a',
124                 ),
125             ),
126
127             array(
128                 'Callable with class name depending on __callStatic()',
129                 array('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest', 'magicMethod'),
130                 array(
131                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
132                     'method' => 'magicMethod',
133                     'file' => 'n/a',
134                     'line' => 'n/a',
135                 ),
136             ),
137
138             array(
139                 'Invokable controller',
140                 $this,
141                 array(
142                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
143                     'method' => null,
144                     'file' => __FILE__,
145                     'line' => $r3->getStartLine(),
146                 ),
147             ),
148         );
149
150         $c = new RequestDataCollector();
151         $request = $this->createRequest();
152         $response = $this->createResponse();
153         foreach ($controllerTests as $controllerTest) {
154             $this->injectController($c, $controllerTest[1], $request);
155             $c->collect($request, $response);
156             $this->assertSame($controllerTest[2], $c->getController(), sprintf('Testing: %s', $controllerTest[0]));
157         }
158     }
159
160     protected function createRequest()
161     {
162         $request = Request::create('http://test.com/foo?bar=baz');
163         $request->attributes->set('foo', 'bar');
164         $request->attributes->set('_route', 'foobar');
165         $request->attributes->set('_route_params', array('name' => 'foo'));
166         $request->attributes->set('resource', fopen(__FILE__, 'r'));
167         $request->attributes->set('object', new \stdClass());
168
169         return $request;
170     }
171
172     protected function createResponse()
173     {
174         $response = new Response();
175         $response->setStatusCode(200);
176         $response->headers->set('Content-Type', 'application/json');
177         $response->headers->setCookie(new Cookie('foo', 'bar', 1, '/foo', 'localhost', true, true));
178         $response->headers->setCookie(new Cookie('bar', 'foo', new \DateTime('@946684800')));
179         $response->headers->setCookie(new Cookie('bazz', 'foo', '2000-12-12'));
180
181         return $response;
182     }
183
184     /**
185      * Inject the given controller callable into the data collector.
186      */
187     protected function injectController($collector, $controller, $request)
188     {
189         $resolver = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface')->getMock();
190         $httpKernel = new HttpKernel(new EventDispatcher(), $resolver);
191         $event = new FilterControllerEvent($httpKernel, $controller, $request, HttpKernelInterface::MASTER_REQUEST);
192         $collector->onKernelController($event);
193     }
194
195     /**
196      * Dummy method used as controller callable.
197      */
198     public static function staticControllerMethod()
199     {
200         throw new \LogicException('Unexpected method call');
201     }
202
203     /**
204      * Magic method to allow non existing methods to be called and delegated.
205      */
206     public function __call($method, $args)
207     {
208         throw new \LogicException('Unexpected method call');
209     }
210
211     /**
212      * Magic method to allow non existing methods to be called and delegated.
213      */
214     public static function __callStatic($method, $args)
215     {
216         throw new \LogicException('Unexpected method call');
217     }
218
219     public function __invoke()
220     {
221         throw new \LogicException('Unexpected method call');
222     }
223 }