TestHttpKernel.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\EventDispatcher\EventDispatcher;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
  15. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  16. use Symfony\Component\HttpKernel\HttpKernel;
  17. use Symfony\Component\HttpKernel\HttpKernelInterface;
  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 assert(\Closure $callback)
  36. {
  37. $trustedConfig = [Request::getTrustedProxies(), Request::getTrustedHeaderSet()];
  38. list($trustedProxies, $trustedHeaderSet, $backendRequest) = $this->backendRequest;
  39. Request::setTrustedProxies($trustedProxies, $trustedHeaderSet);
  40. try {
  41. $callback($backendRequest);
  42. } finally {
  43. list($trustedProxies, $trustedHeaderSet) = $trustedConfig;
  44. Request::setTrustedProxies($trustedProxies, $trustedHeaderSet);
  45. }
  46. }
  47. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = false)
  48. {
  49. $this->catch = $catch;
  50. $this->backendRequest = [Request::getTrustedProxies(), Request::getTrustedHeaderSet(), $request];
  51. return parent::handle($request, $type, $catch);
  52. }
  53. public function isCatchingExceptions()
  54. {
  55. return $this->catch;
  56. }
  57. public function getController(Request $request)
  58. {
  59. return [$this, 'callController'];
  60. }
  61. public function getArguments(Request $request, $controller)
  62. {
  63. return [$request];
  64. }
  65. public function callController(Request $request)
  66. {
  67. $this->called = true;
  68. $response = new Response($this->body, $this->status, $this->headers);
  69. if (null !== $customizer = $this->customizer) {
  70. $customizer($request, $response);
  71. }
  72. return $response;
  73. }
  74. public function hasBeenCalled()
  75. {
  76. return $this->called;
  77. }
  78. public function reset()
  79. {
  80. $this->called = false;
  81. }
  82. }