TestHttpKernel.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\ControllerResolverInterface;
  15. use Symfony\Component\HttpKernel\HttpKernel;
  16. use Symfony\Component\HttpKernel\HttpKernelInterface;
  17. class TestHttpKernel extends HttpKernel implements ControllerResolverInterface
  18. {
  19. protected $body;
  20. protected $status;
  21. protected $headers;
  22. protected $called = false;
  23. protected $customizer;
  24. protected $catch = false;
  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->trustedHeadersReflector = new \ReflectionProperty('Symfony\Component\HttpFoundation\Request', 'trustedHeaders');
  33. $this->trustedHeadersReflector->setAccessible(true);
  34. parent::__construct(new EventDispatcher(), $this);
  35. }
  36. public function assert(\Closure $callback)
  37. {
  38. $trustedConfig = array(Request::getTrustedProxies(), $this->trustedHeadersReflector->getValue());
  39. list($trustedProxies, $trustedHeaders, $backendRequest) = $this->backendRequest;
  40. Request::setTrustedProxies($trustedProxies);
  41. $this->trustedHeadersReflector->setValue(null, $trustedHeaders);
  42. try {
  43. $e = null;
  44. $callback($backendRequest);
  45. } catch (\Throwable $e) {
  46. } catch (\Exception $e) {
  47. }
  48. list($trustedProxies, $trustedHeaders) = $trustedConfig;
  49. Request::setTrustedProxies($trustedProxies);
  50. $this->trustedHeadersReflector->setValue(null, $trustedHeaders);
  51. if (null !== $e) {
  52. throw $e;
  53. }
  54. }
  55. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = false)
  56. {
  57. $this->catch = $catch;
  58. $this->backendRequest = array($request::getTrustedProxies(), $this->trustedHeadersReflector->getValue(), $request);
  59. return parent::handle($request, $type, $catch);
  60. }
  61. public function isCatchingExceptions()
  62. {
  63. return $this->catch;
  64. }
  65. public function getController(Request $request)
  66. {
  67. return array($this, 'callController');
  68. }
  69. public function getArguments(Request $request, $controller)
  70. {
  71. return array($request);
  72. }
  73. public function callController(Request $request)
  74. {
  75. $this->called = true;
  76. $response = new Response($this->body, $this->status, $this->headers);
  77. if (null !== $customizer = $this->customizer) {
  78. $customizer($request, $response);
  79. }
  80. return $response;
  81. }
  82. public function hasBeenCalled()
  83. {
  84. return $this->called;
  85. }
  86. public function reset()
  87. {
  88. $this->called = false;
  89. }
  90. }