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