RequestDataCollectorTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\EventDispatcher\EventDispatcher;
  13. use Symfony\Component\HttpFoundation\Cookie;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
  17. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  18. use Symfony\Component\HttpKernel\HttpKernel;
  19. use Symfony\Component\HttpKernel\HttpKernelInterface;
  20. class RequestDataCollectorTest extends TestCase
  21. {
  22. public function testCollect()
  23. {
  24. $c = new RequestDataCollector();
  25. $c->collect($this->createRequest(), $this->createResponse());
  26. $attributes = $c->getRequestAttributes();
  27. $this->assertSame('request', $c->getName());
  28. $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestHeaders());
  29. $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestServer());
  30. $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestCookies());
  31. $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $attributes);
  32. $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestRequest());
  33. $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestQuery());
  34. $this->assertSame('html', $c->getFormat());
  35. $this->assertSame('foobar', $c->getRoute());
  36. $this->assertSame(array('name' => 'foo'), $c->getRouteParams());
  37. $this->assertSame(array(), $c->getSessionAttributes());
  38. $this->assertSame('en', $c->getLocale());
  39. $this->assertRegExp('/Resource\(stream#\d+\)/', $attributes->get('resource'));
  40. $this->assertSame('Object(stdClass)', $attributes->get('object'));
  41. $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getResponseHeaders());
  42. $this->assertSame('OK', $c->getStatusText());
  43. $this->assertSame(200, $c->getStatusCode());
  44. $this->assertSame('application/json', $c->getContentType());
  45. }
  46. /**
  47. * Test various types of controller callables.
  48. */
  49. public function testControllerInspection()
  50. {
  51. // make sure we always match the line number
  52. $r1 = new \ReflectionMethod($this, 'testControllerInspection');
  53. $r2 = new \ReflectionMethod($this, 'staticControllerMethod');
  54. $r3 = new \ReflectionClass($this);
  55. // test name, callable, expected
  56. $controllerTests = array(
  57. array(
  58. '"Regular" callable',
  59. array($this, 'testControllerInspection'),
  60. array(
  61. 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
  62. 'method' => 'testControllerInspection',
  63. 'file' => __FILE__,
  64. 'line' => $r1->getStartLine(),
  65. ),
  66. ),
  67. array(
  68. 'Closure',
  69. function () { return 'foo'; },
  70. array(
  71. 'class' => __NAMESPACE__.'\{closure}',
  72. 'method' => null,
  73. 'file' => __FILE__,
  74. 'line' => __LINE__ - 5,
  75. ),
  76. ),
  77. array(
  78. 'Static callback as string',
  79. 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest::staticControllerMethod',
  80. 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest::staticControllerMethod',
  81. ),
  82. array(
  83. 'Static callable with instance',
  84. array($this, 'staticControllerMethod'),
  85. array(
  86. 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
  87. 'method' => 'staticControllerMethod',
  88. 'file' => __FILE__,
  89. 'line' => $r2->getStartLine(),
  90. ),
  91. ),
  92. array(
  93. 'Static callable with class name',
  94. array('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest', 'staticControllerMethod'),
  95. array(
  96. 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
  97. 'method' => 'staticControllerMethod',
  98. 'file' => __FILE__,
  99. 'line' => $r2->getStartLine(),
  100. ),
  101. ),
  102. array(
  103. 'Callable with instance depending on __call()',
  104. array($this, 'magicMethod'),
  105. array(
  106. 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
  107. 'method' => 'magicMethod',
  108. 'file' => 'n/a',
  109. 'line' => 'n/a',
  110. ),
  111. ),
  112. array(
  113. 'Callable with class name depending on __callStatic()',
  114. array('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest', 'magicMethod'),
  115. array(
  116. 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
  117. 'method' => 'magicMethod',
  118. 'file' => 'n/a',
  119. 'line' => 'n/a',
  120. ),
  121. ),
  122. array(
  123. 'Invokable controller',
  124. $this,
  125. array(
  126. 'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
  127. 'method' => null,
  128. 'file' => __FILE__,
  129. 'line' => $r3->getStartLine(),
  130. ),
  131. ),
  132. );
  133. $c = new RequestDataCollector();
  134. $request = $this->createRequest();
  135. $response = $this->createResponse();
  136. foreach ($controllerTests as $controllerTest) {
  137. $this->injectController($c, $controllerTest[1], $request);
  138. $c->collect($request, $response);
  139. $this->assertSame($controllerTest[2], $c->getController(), sprintf('Testing: %s', $controllerTest[0]));
  140. }
  141. }
  142. protected function createRequest()
  143. {
  144. $request = Request::create('http://test.com/foo?bar=baz');
  145. $request->attributes->set('foo', 'bar');
  146. $request->attributes->set('_route', 'foobar');
  147. $request->attributes->set('_route_params', array('name' => 'foo'));
  148. $request->attributes->set('resource', fopen(__FILE__, 'r'));
  149. $request->attributes->set('object', new \stdClass());
  150. return $request;
  151. }
  152. protected function createResponse()
  153. {
  154. $response = new Response();
  155. $response->setStatusCode(200);
  156. $response->headers->set('Content-Type', 'application/json');
  157. $response->headers->setCookie(new Cookie('foo', 'bar', 1, '/foo', 'localhost', true, true));
  158. $response->headers->setCookie(new Cookie('bar', 'foo', new \DateTime('@946684800')));
  159. $response->headers->setCookie(new Cookie('bazz', 'foo', '2000-12-12'));
  160. return $response;
  161. }
  162. /**
  163. * Inject the given controller callable into the data collector.
  164. */
  165. protected function injectController($collector, $controller, $request)
  166. {
  167. $resolver = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface')->getMock();
  168. $httpKernel = new HttpKernel(new EventDispatcher(), $resolver);
  169. $event = new FilterControllerEvent($httpKernel, $controller, $request, HttpKernelInterface::MASTER_REQUEST);
  170. $collector->onKernelController($event);
  171. }
  172. /**
  173. * Dummy method used as controller callable.
  174. */
  175. public static function staticControllerMethod()
  176. {
  177. throw new \LogicException('Unexpected method call');
  178. }
  179. /**
  180. * Magic method to allow non existing methods to be called and delegated.
  181. */
  182. public function __call($method, $args)
  183. {
  184. throw new \LogicException('Unexpected method call');
  185. }
  186. /**
  187. * Magic method to allow non existing methods to be called and delegated.
  188. */
  189. public static function __callStatic($method, $args)
  190. {
  191. throw new \LogicException('Unexpected method call');
  192. }
  193. public function __invoke()
  194. {
  195. throw new \LogicException('Unexpected method call');
  196. }
  197. }