StackedHttpKernelTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace Stack;
  3. use PHPUnit\Framework\TestCase;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\HttpKernelInterface;
  7. use Symfony\Component\HttpKernel\TerminableInterface;
  8. class StackedHttpKernelTest extends TestCase
  9. {
  10. /** @test */
  11. public function handleShouldDelegateToApp()
  12. {
  13. $app = $this->getHttpKernelMock(new Response('ok'));
  14. $kernel = new StackedHttpKernel($app, array($app));
  15. $request = Request::create('/');
  16. $response = $kernel->handle($request);
  17. $this->assertSame('ok', $response->getContent());
  18. }
  19. /** @test */
  20. public function handleShouldStillDelegateToAppWithMiddlewares()
  21. {
  22. $app = $this->getHttpKernelMock(new Response('ok'));
  23. $bar = $this->getHttpKernelMock(new Response('bar'));
  24. $foo = $this->getHttpKernelMock(new Response('foo'));
  25. $kernel = new StackedHttpKernel($app, array($foo, $bar, $app));
  26. $request = Request::create('/');
  27. $response = $kernel->handle($request);
  28. $this->assertSame('ok', $response->getContent());
  29. }
  30. /** @test */
  31. public function terminateShouldDelegateToMiddlewares()
  32. {
  33. $first = new TerminableKernelSpy();
  34. $second = new TerminableKernelSpy($first);
  35. $third = new KernelSpy($second);
  36. $fourth = new TerminableKernelSpy($third);
  37. $fifth = new TerminableKernelSpy($fourth);
  38. $kernel = new StackedHttpKernel($fifth, $middlewares = array($fifth, $fourth, $third, $second, $first));
  39. $request = Request::create('/');
  40. $response = $kernel->handle($request);
  41. $kernel->terminate($request, $response);
  42. $this->assertTerminablesCalledOnce($middlewares);
  43. }
  44. private function assertTerminablesCalledOnce(array $middlewares)
  45. {
  46. foreach ($middlewares as $kernel) {
  47. if ($kernel instanceof TerminableInterface) {
  48. $this->assertEquals(1, $kernel->terminateCallCount(), "Terminate was called {$kernel->terminateCallCount()} times");
  49. }
  50. }
  51. }
  52. private function getHttpKernelMock(Response $response)
  53. {
  54. $app = $this->createMock(HttpKernelInterface::class);
  55. $app->expects($this->any())
  56. ->method('handle')
  57. ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
  58. ->will($this->returnValue($response));
  59. return $app;
  60. }
  61. private function getTerminableMock(Response $response = null)
  62. {
  63. $app = $this->getMock('Stack\TerminableHttpKernel');
  64. if ($response) {
  65. $app->expects($this->any())
  66. ->method('handle')
  67. ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
  68. ->will($this->returnValue($response));
  69. }
  70. $app->expects($this->once())
  71. ->method('terminate')
  72. ->with(
  73. $this->isInstanceOf('Symfony\Component\HttpFoundation\Request'),
  74. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response')
  75. );
  76. return $app;
  77. }
  78. private function getDelegatingTerminableMock(TerminableInterface $next)
  79. {
  80. $app = $this->getMock('Stack\TerminableHttpKernel');
  81. $app->expects($this->once())
  82. ->method('terminate')
  83. ->with(
  84. $this->isInstanceOf('Symfony\Component\HttpFoundation\Request'),
  85. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response')
  86. )
  87. ->will($this->returnCallback(function ($request, $response) use ($next) {
  88. $next->terminate($request, $response);
  89. }));
  90. return $app;
  91. }
  92. }
  93. class KernelSpy implements HttpKernelInterface
  94. {
  95. private $handleCallCount = 0;
  96. public function __construct(HttpKernelInterface $kernel = null)
  97. {
  98. $this->kernel = $kernel;
  99. }
  100. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  101. {
  102. $this->handleCallCount++;
  103. if ($this->kernel) {
  104. return $this->kernel->handle($request, $type, $catch);
  105. }
  106. return new Response('OK');
  107. }
  108. public function handleCallCount()
  109. {
  110. return $this->handleCallCount;
  111. }
  112. }
  113. class TerminableKernelSpy extends KernelSpy implements TerminableInterface
  114. {
  115. private $terminateCallCount = 0;
  116. public function terminate(Request $request, Response $response)
  117. {
  118. $this->terminateCallCount++;
  119. if ($this->kernel && $this->kernel instanceof TerminableInterface) {
  120. return $this->kernel->terminate($request, $response);
  121. }
  122. }
  123. public function terminateCallCount()
  124. {
  125. return $this->terminateCallCount;
  126. }
  127. }