No Description

RequestDataCollectorTest.php 11KB

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