ContainerAwareHttpKernelTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\DependencyInjection;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\EventDispatcher\EventDispatcher;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel;
  17. use Symfony\Component\HttpKernel\HttpKernelInterface;
  18. /**
  19. * @group legacy
  20. */
  21. class ContainerAwareHttpKernelTest extends TestCase
  22. {
  23. /**
  24. * @dataProvider getProviderTypes
  25. */
  26. public function testHandle($type)
  27. {
  28. $request = new Request();
  29. $expected = new Response();
  30. $controller = function () use ($expected) {
  31. return $expected;
  32. };
  33. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  34. $this
  35. ->expectsEnterScopeOnce($container)
  36. ->expectsLeaveScopeOnce($container)
  37. ->expectsSetRequestWithAt($container, $request, 3)
  38. ->expectsSetRequestWithAt($container, null, 4)
  39. ;
  40. $dispatcher = new EventDispatcher();
  41. $resolver = $this->getResolverMockFor($controller, $request);
  42. $stack = new RequestStack();
  43. $kernel = new ContainerAwareHttpKernel($dispatcher, $container, $resolver, $stack);
  44. $actual = $kernel->handle($request, $type);
  45. $this->assertSame($expected, $actual, '->handle() returns the response');
  46. }
  47. /**
  48. * @dataProvider getProviderTypes
  49. */
  50. public function testVerifyRequestStackPushPopDuringHandle($type)
  51. {
  52. $request = new Request();
  53. $expected = new Response();
  54. $controller = function () use ($expected) {
  55. return $expected;
  56. };
  57. $stack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->setMethods(array('push', 'pop'))->getMock();
  58. $stack->expects($this->at(0))->method('push')->with($this->equalTo($request));
  59. $stack->expects($this->at(1))->method('pop');
  60. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  61. $dispatcher = new EventDispatcher();
  62. $resolver = $this->getResolverMockFor($controller, $request);
  63. $kernel = new ContainerAwareHttpKernel($dispatcher, $container, $resolver, $stack);
  64. $kernel->handle($request, $type);
  65. }
  66. /**
  67. * @dataProvider getProviderTypes
  68. */
  69. public function testHandleRestoresThePreviousRequestOnException($type)
  70. {
  71. $request = new Request();
  72. $expected = new \Exception();
  73. $controller = function () use ($expected) {
  74. throw $expected;
  75. };
  76. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  77. $this
  78. ->expectsEnterScopeOnce($container)
  79. ->expectsLeaveScopeOnce($container)
  80. ->expectsSetRequestWithAt($container, $request, 3)
  81. ->expectsSetRequestWithAt($container, null, 4)
  82. ;
  83. $dispatcher = new EventDispatcher();
  84. $resolver = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface')->getMock();
  85. $resolver = $this->getResolverMockFor($controller, $request);
  86. $stack = new RequestStack();
  87. $kernel = new ContainerAwareHttpKernel($dispatcher, $container, $resolver, $stack);
  88. try {
  89. $kernel->handle($request, $type);
  90. $this->fail('->handle() suppresses the controller exception');
  91. } catch (\PHPUnit\Framework\Exception $e) {
  92. throw $e;
  93. } catch (\PHPUnit_Framework_Exception $e) {
  94. throw $e;
  95. } catch (\Exception $e) {
  96. $this->assertSame($expected, $e, '->handle() throws the controller exception');
  97. }
  98. }
  99. public function getProviderTypes()
  100. {
  101. return array(
  102. array(HttpKernelInterface::MASTER_REQUEST),
  103. array(HttpKernelInterface::SUB_REQUEST),
  104. );
  105. }
  106. private function getResolverMockFor($controller, $request)
  107. {
  108. $resolver = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface')->getMock();
  109. $resolver->expects($this->once())
  110. ->method('getController')
  111. ->with($request)
  112. ->will($this->returnValue($controller));
  113. $resolver->expects($this->once())
  114. ->method('getArguments')
  115. ->with($request, $controller)
  116. ->will($this->returnValue(array()));
  117. return $resolver;
  118. }
  119. private function expectsSetRequestWithAt($container, $with, $at)
  120. {
  121. $container
  122. ->expects($this->at($at))
  123. ->method('set')
  124. ->with($this->equalTo('request'), $this->equalTo($with), $this->equalTo('request'))
  125. ;
  126. return $this;
  127. }
  128. private function expectsEnterScopeOnce($container)
  129. {
  130. $container
  131. ->expects($this->once())
  132. ->method('enterScope')
  133. ->with($this->equalTo('request'))
  134. ;
  135. return $this;
  136. }
  137. private function expectsLeaveScopeOnce($container)
  138. {
  139. $container
  140. ->expects($this->once())
  141. ->method('leaveScope')
  142. ->with($this->equalTo('request'))
  143. ;
  144. return $this;
  145. }
  146. }