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\DataCollector;
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;
24 class RequestDataCollectorTest extends TestCase
26 public function testCollect()
28 $c = new RequestDataCollector();
30 $c->collect($this->createRequest(), $this->createResponse());
32 $attributes = $c->getRequestAttributes();
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'));
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());
56 * Test various types of controller callables.
58 public function testControllerInspection()
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(
68 array($this, 'testControllerInspection'),
70 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
71 'method' => 'testControllerInspection',
73 'line' => $r1->getStartLine(),
79 function () { return 'foo'; },
81 'class' => __NAMESPACE__.'\{closure}',
84 'line' => __LINE__ - 5,
89 'Static callback as string',
90 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest::staticControllerMethod',
91 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest::staticControllerMethod',
95 'Static callable with instance',
96 array($this, 'staticControllerMethod'),
98 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
99 'method' => 'staticControllerMethod',
101 'line' => $r2->getStartLine(),
106 'Static callable with class name',
107 array('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest', 'staticControllerMethod'),
109 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
110 'method' => 'staticControllerMethod',
112 'line' => $r2->getStartLine(),
117 'Callable with instance depending on __call()',
118 array($this, 'magicMethod'),
120 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
121 'method' => 'magicMethod',
128 'Callable with class name depending on __callStatic()',
129 array('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest', 'magicMethod'),
131 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
132 'method' => 'magicMethod',
139 'Invokable controller',
142 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
145 'line' => $r3->getStartLine(),
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]));
160 protected function createRequest()
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());
172 protected function createResponse()
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'));
185 * Inject the given controller callable into the data collector.
187 protected function injectController($collector, $controller, $request)
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);
196 * Dummy method used as controller callable.
198 public static function staticControllerMethod()
200 throw new \LogicException('Unexpected method call');
204 * Magic method to allow non existing methods to be called and delegated.
206 public function __call($method, $args)
208 throw new \LogicException('Unexpected method call');
212 * Magic method to allow non existing methods to be called and delegated.
214 public static function __callStatic($method, $args)
216 throw new \LogicException('Unexpected method call');
219 public function __invoke()
221 throw new \LogicException('Unexpected method call');