TestMultipleHttpKernel.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 TestMultipleHttpKernel extends HttpKernel implements ControllerResolverInterface, ArgumentResolverInterface
  19. {
  20. protected $bodies = [];
  21. protected $statuses = [];
  22. protected $headers = [];
  23. protected $called = false;
  24. protected $backendRequest;
  25. public function __construct($responses)
  26. {
  27. foreach ($responses as $response) {
  28. $this->bodies[] = $response['body'];
  29. $this->statuses[] = $response['status'];
  30. $this->headers[] = $response['headers'];
  31. }
  32. parent::__construct(new EventDispatcher(), $this, null, $this);
  33. }
  34. public function getBackendRequest()
  35. {
  36. return $this->backendRequest;
  37. }
  38. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = false)
  39. {
  40. $this->backendRequest = $request;
  41. return parent::handle($request, $type, $catch);
  42. }
  43. public function getController(Request $request)
  44. {
  45. return [$this, 'callController'];
  46. }
  47. public function getArguments(Request $request, $controller)
  48. {
  49. return [$request];
  50. }
  51. public function callController(Request $request)
  52. {
  53. $this->called = true;
  54. $response = new Response(array_shift($this->bodies), array_shift($this->statuses), array_shift($this->headers));
  55. return $response;
  56. }
  57. public function hasBeenCalled()
  58. {
  59. return $this->called;
  60. }
  61. public function reset()
  62. {
  63. $this->called = false;
  64. }
  65. }