RequestDataCollectorTest.php 8.6 KB

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