TestHttpKernel.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Tests\Components\HttpKernel\Cache;
  11. use Symfony\Components\HttpKernel\HttpKernel;
  12. use Symfony\Components\HttpFoundation\Request;
  13. use Symfony\Components\HttpFoundation\Response;
  14. use Symfony\Components\EventDispatcher\EventDispatcher;
  15. use Symfony\Components\EventDispatcher\Event;
  16. class TestHttpKernel extends HttpKernel
  17. {
  18. protected $body;
  19. protected $status;
  20. protected $headers;
  21. protected $called;
  22. protected $customizer;
  23. public function __construct($body, $status, $headers, \Closure $customizer = null)
  24. {
  25. $this->body = $body;
  26. $this->status = $status;
  27. $this->headers = $headers;
  28. $this->customizer = $customizer;
  29. $this->called = false;
  30. $this->dispatcher = new EventDispatcher();
  31. $this->dispatcher->connect('core.load_controller', array($this, 'loadController'));
  32. }
  33. public function loadController(Event $event)
  34. {
  35. $event->setReturnValue(array(array($this, 'callController'), array($event['request'])));
  36. return true;
  37. }
  38. public function callController(Request $request)
  39. {
  40. $this->called = true;
  41. $response = new Response($this->body, $this->status, $this->headers);
  42. if (null !== $this->customizer) {
  43. call_user_func($this->customizer, $request, $response);
  44. }
  45. return $response;
  46. }
  47. public function hasBeenCalled()
  48. {
  49. return $this->called;
  50. }
  51. public function reset()
  52. {
  53. $this->called = false;
  54. }
  55. }