StackedHttpKernelTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Stack;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\HttpKernelInterface;
  6. use Symfony\Component\HttpKernel\TerminableInterface;
  7. class StackedHttpKernelTest extends \PHPUnit_Framework_TestCase
  8. {
  9. /** @test */
  10. public function handleShouldDelegateToApp()
  11. {
  12. $app = $this->getHttpKernelMock(new Response('ok'));
  13. $kernel = new StackedHttpKernel($app, array($app));
  14. $request = Request::create('/');
  15. $response = $kernel->handle($request);
  16. $this->assertSame('ok', $response->getContent());
  17. }
  18. /** @test */
  19. public function handleShouldStillDelegateToAppWithMiddlewares()
  20. {
  21. $app = $this->getHttpKernelMock(new Response('ok'));
  22. $foo = $this->getHttpKernelMock(new Response('foo'));
  23. $bar = $this->getHttpKernelMock(new Response('bar'));
  24. $kernel = new StackedHttpKernel($app, array($app, $foo, $bar));
  25. $request = Request::create('/');
  26. $response = $kernel->handle($request);
  27. $this->assertSame('ok', $response->getContent());
  28. }
  29. /** @test */
  30. public function terminateShouldDelegateToMiddlewares()
  31. {
  32. $app = $this->getTerminableMock(new Response('ok'));
  33. $foo = $this->getTerminableMock();
  34. $bar = $this->getTerminableMock();
  35. $kernel = new StackedHttpKernel($app, array($app, $foo, $bar));
  36. $request = Request::create('/');
  37. $response = $kernel->handle($request);
  38. $kernel->terminate($request, $response);
  39. }
  40. private function getHttpKernelMock(Response $response)
  41. {
  42. $app = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
  43. $app->expects($this->any())
  44. ->method('handle')
  45. ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
  46. ->will($this->returnValue($response));
  47. return $app;
  48. }
  49. private function getTerminableMock(Response $response = null)
  50. {
  51. $app = $this->getMock('Stack\TerminableHttpKernel');
  52. if ($response) {
  53. $app->expects($this->any())
  54. ->method('handle')
  55. ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
  56. ->will($this->returnValue($response));
  57. }
  58. $app->expects($this->once())
  59. ->method('terminate')
  60. ->with(
  61. $this->isInstanceOf('Symfony\Component\HttpFoundation\Request'),
  62. $this->isInstanceOf('Symfony\Component\HttpFoundation\Response')
  63. );
  64. return $app;
  65. }
  66. }