RequestDataCollectorTest.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. /**
  63. * @dataProvider provideControllerCallables
  64. */
  65. public function testControllerInspection($name, $callable, $expected)
  66. {
  67. $c = new RequestDataCollector();
  68. $request = $this->createRequest();
  69. $response = $this->createResponse();
  70. $this->injectController($c, $callable, $request);
  71. $c->collect($request, $response);
  72. $c->lateCollect();
  73. $this->assertSame($expected, $c->getController()->getValue(true), sprintf('Testing: %s', $name));
  74. }
  75. public function provideControllerCallables()
  76. {
  77. // make sure we always match the line number
  78. $r1 = new \ReflectionMethod($this, 'testControllerInspection');
  79. $r2 = new \ReflectionMethod($this, 'staticControllerMethod');
  80. $r3 = new \ReflectionClass($this);
  81. // test name, callable, expected
  82. return array(
  83. array(
  84. '"Regular" callable',
  85. array($this, 'testControllerInspection'),
  86. array(
  87. 'class' => __NAMESPACE__.'\RequestDataCollectorTest',
  88. 'method' => 'testControllerInspection',
  89. 'file' => __FILE__,
  90. 'line' => $r1->getStartLine(),
  91. ),
  92. ),
  93. array(
  94. 'Closure',
  95. function () { return 'foo'; },
  96. array(
  97. 'class' => __NAMESPACE__.'\{closure}',
  98. 'method' => null,
  99. 'file' => __FILE__,
  100. 'line' => __LINE__ - 5,
  101. ),
  102. ),
  103. array(
  104. 'Static callback as string',
  105. __NAMESPACE__.'\RequestDataCollectorTest::staticControllerMethod',
  106. array(
  107. 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
  108. 'method' => 'staticControllerMethod',
  109. 'file' => __FILE__,
  110. 'line' => $r2->getStartLine(),
  111. ),
  112. ),
  113. array(
  114. 'Static callable with instance',
  115. array($this, 'staticControllerMethod'),
  116. array(
  117. 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
  118. 'method' => 'staticControllerMethod',
  119. 'file' => __FILE__,
  120. 'line' => $r2->getStartLine(),
  121. ),
  122. ),
  123. array(
  124. 'Static callable with class name',
  125. array('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest', 'staticControllerMethod'),
  126. array(
  127. 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
  128. 'method' => 'staticControllerMethod',
  129. 'file' => __FILE__,
  130. 'line' => $r2->getStartLine(),
  131. ),
  132. ),
  133. array(
  134. 'Callable with instance depending on __call()',
  135. array($this, 'magicMethod'),
  136. array(
  137. 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
  138. 'method' => 'magicMethod',
  139. 'file' => 'n/a',
  140. 'line' => 'n/a',
  141. ),
  142. ),
  143. array(
  144. 'Callable with class name depending on __callStatic()',
  145. array('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest', 'magicMethod'),
  146. array(
  147. 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
  148. 'method' => 'magicMethod',
  149. 'file' => 'n/a',
  150. 'line' => 'n/a',
  151. ),
  152. ),
  153. array(
  154. 'Invokable controller',
  155. $this,
  156. array(
  157. 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
  158. 'method' => null,
  159. 'file' => __FILE__,
  160. 'line' => $r3->getStartLine(),
  161. ),
  162. ),
  163. );
  164. }
  165. public function testItIgnoresInvalidCallables()
  166. {
  167. $request = $this->createRequestWithSession();
  168. $response = new RedirectResponse('/');
  169. $c = new RequestDataCollector();
  170. $c->collect($request, $response);
  171. $this->assertSame('n/a', $c->getController());
  172. }
  173. public function testItAddsRedirectedAttributesWhenRequestContainsSpecificCookie()
  174. {
  175. $request = $this->createRequest();
  176. $request->cookies->add(array(
  177. 'sf_redirect' => '{}',
  178. ));
  179. $kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
  180. $c = new RequestDataCollector();
  181. $c->onKernelResponse(new FilterResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $this->createResponse()));
  182. $this->assertTrue($request->attributes->get('_redirected'));
  183. }
  184. public function testItSetsARedirectCookieIfTheResponseIsARedirection()
  185. {
  186. $c = new RequestDataCollector();
  187. $response = $this->createResponse();
  188. $response->setStatusCode(302);
  189. $response->headers->set('Location', '/somewhere-else');
  190. $c->collect($request = $this->createRequest(), $response);
  191. $c->lateCollect();
  192. $cookie = $this->getCookieByName($response, 'sf_redirect');
  193. $this->assertNotEmpty($cookie->getValue());
  194. }
  195. public function testItCollectsTheRedirectionAndClearTheCookie()
  196. {
  197. $c = new RequestDataCollector();
  198. $request = $this->createRequest();
  199. $request->attributes->set('_redirected', true);
  200. $request->cookies->add(array(
  201. 'sf_redirect' => '{"method": "POST"}',
  202. ));
  203. $c->collect($request, $response = $this->createResponse());
  204. $c->lateCollect();
  205. $this->assertEquals('POST', $c->getRedirect()['method']);
  206. $cookie = $this->getCookieByName($response, 'sf_redirect');
  207. $this->assertNull($cookie->getValue());
  208. }
  209. protected function createRequest($routeParams = array('name' => 'foo'))
  210. {
  211. $request = Request::create('http://test.com/foo?bar=baz');
  212. $request->attributes->set('foo', 'bar');
  213. $request->attributes->set('_route', 'foobar');
  214. $request->attributes->set('_route_params', $routeParams);
  215. $request->attributes->set('resource', fopen(__FILE__, 'r'));
  216. $request->attributes->set('object', new \stdClass());
  217. return $request;
  218. }
  219. private function createRequestWithSession()
  220. {
  221. $request = $this->createRequest();
  222. $request->attributes->set('_controller', 'Foo::bar');
  223. $request->setSession(new Session(new MockArraySessionStorage()));
  224. $request->getSession()->start();
  225. return $request;
  226. }
  227. protected function createResponse()
  228. {
  229. $response = new Response();
  230. $response->setStatusCode(200);
  231. $response->headers->set('Content-Type', 'application/json');
  232. $response->headers->set('X-Foo-Bar', null);
  233. $response->headers->setCookie(new Cookie('foo', 'bar', 1, '/foo', 'localhost', true, true));
  234. $response->headers->setCookie(new Cookie('bar', 'foo', new \DateTime('@946684800')));
  235. $response->headers->setCookie(new Cookie('bazz', 'foo', '2000-12-12'));
  236. return $response;
  237. }
  238. /**
  239. * Inject the given controller callable into the data collector.
  240. */
  241. protected function injectController($collector, $controller, $request)
  242. {
  243. $resolver = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface')->getMock();
  244. $httpKernel = new HttpKernel(new EventDispatcher(), $resolver, null, $this->getMockBuilder(ArgumentResolverInterface::class)->getMock());
  245. $event = new FilterControllerEvent($httpKernel, $controller, $request, HttpKernelInterface::MASTER_REQUEST);
  246. $collector->onKernelController($event);
  247. }
  248. /**
  249. * Dummy method used as controller callable.
  250. */
  251. public static function staticControllerMethod()
  252. {
  253. throw new \LogicException('Unexpected method call');
  254. }
  255. /**
  256. * Magic method to allow non existing methods to be called and delegated.
  257. */
  258. public function __call($method, $args)
  259. {
  260. throw new \LogicException('Unexpected method call');
  261. }
  262. /**
  263. * Magic method to allow non existing methods to be called and delegated.
  264. */
  265. public static function __callStatic($method, $args)
  266. {
  267. throw new \LogicException('Unexpected method call');
  268. }
  269. public function __invoke()
  270. {
  271. throw new \LogicException('Unexpected method call');
  272. }
  273. private function getCookieByName(Response $response, $name)
  274. {
  275. foreach ($response->headers->getCookies() as $cookie) {
  276. if ($cookie->getName() == $name) {
  277. return $cookie;
  278. }
  279. }
  280. throw new \InvalidArgumentException(sprintf('Cookie named "%s" is not in response', $name));
  281. }
  282. }