RequestDataCollectorTest.php 11 KB

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