TestHttpKernel.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\HttpCache;
  11. use Symfony\Component\HttpKernel\HttpKernel;
  12. use Symfony\Component\HttpKernel\HttpKernelInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  16. use Symfony\Component\EventDispatcher\EventDispatcher;
  17. class TestHttpKernel extends HttpKernel implements ControllerResolverInterface
  18. {
  19. protected $body;
  20. protected $status;
  21. protected $headers;
  22. protected $called;
  23. protected $customizer;
  24. protected $catch;
  25. protected $backendRequest;
  26. public function __construct($body, $status, $headers, \Closure $customizer = null)
  27. {
  28. $this->body = $body;
  29. $this->status = $status;
  30. $this->headers = $headers;
  31. $this->customizer = $customizer;
  32. $this->called = false;
  33. $this->catch = false;
  34. parent::__construct(new EventDispatcher(), $this);
  35. }
  36. public function getBackendRequest()
  37. {
  38. return $this->backendRequest;
  39. }
  40. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = false)
  41. {
  42. $this->catch = $catch;
  43. $this->backendRequest = $request;
  44. return parent::handle($request, $type, $catch);
  45. }
  46. public function isCatchingExceptions()
  47. {
  48. return $this->catch;
  49. }
  50. public function getController(Request $request)
  51. {
  52. return array($this, 'callController');
  53. }
  54. public function getArguments(Request $request, $controller)
  55. {
  56. return array($request);
  57. }
  58. public function callController(Request $request)
  59. {
  60. $this->called = true;
  61. $response = new Response($this->body, $this->status, $this->headers);
  62. if (null !== $this->customizer) {
  63. call_user_func($this->customizer, $request, $response);
  64. }
  65. return $response;
  66. }
  67. public function hasBeenCalled()
  68. {
  69. return $this->called;
  70. }
  71. public function reset()
  72. {
  73. $this->called = false;
  74. }
  75. }