ContainerAwareHttpKernelTest.php 5.7 KB

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