RequestDataCollectorTest.php 11 KB

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