4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpKernel\HttpKernelInterface;
16 use Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel;
17 use Symfony\Component\HttpFoundation\RequestStack;
18 use Symfony\Component\HttpFoundation\Response;
19 use Symfony\Component\HttpFoundation\Request;
20 use Symfony\Component\EventDispatcher\EventDispatcher;
25 class ContainerAwareHttpKernelTest extends TestCase
28 * @dataProvider getProviderTypes
30 public function testHandle($type)
32 $request = new Request();
33 $expected = new Response();
34 $controller = function () use ($expected) {
38 $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
40 ->expectsEnterScopeOnce($container)
41 ->expectsLeaveScopeOnce($container)
42 ->expectsSetRequestWithAt($container, $request, 3)
43 ->expectsSetRequestWithAt($container, null, 4)
46 $dispatcher = new EventDispatcher();
47 $resolver = $this->getResolverMockFor($controller, $request);
48 $stack = new RequestStack();
49 $kernel = new ContainerAwareHttpKernel($dispatcher, $container, $resolver, $stack);
51 $actual = $kernel->handle($request, $type);
53 $this->assertSame($expected, $actual, '->handle() returns the response');
57 * @dataProvider getProviderTypes
59 public function testVerifyRequestStackPushPopDuringHandle($type)
61 $request = new Request();
62 $expected = new Response();
63 $controller = function () use ($expected) {
67 $stack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->setMethods(array('push', 'pop'))->getMock();
68 $stack->expects($this->at(0))->method('push')->with($this->equalTo($request));
69 $stack->expects($this->at(1))->method('pop');
71 $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
72 $dispatcher = new EventDispatcher();
73 $resolver = $this->getResolverMockFor($controller, $request);
74 $kernel = new ContainerAwareHttpKernel($dispatcher, $container, $resolver, $stack);
76 $kernel->handle($request, $type);
80 * @dataProvider getProviderTypes
82 public function testHandleRestoresThePreviousRequestOnException($type)
84 $request = new Request();
85 $expected = new \Exception();
86 $controller = function () use ($expected) {
90 $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
92 ->expectsEnterScopeOnce($container)
93 ->expectsLeaveScopeOnce($container)
94 ->expectsSetRequestWithAt($container, $request, 3)
95 ->expectsSetRequestWithAt($container, null, 4)
98 $dispatcher = new EventDispatcher();
99 $resolver = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface')->getMock();
100 $resolver = $this->getResolverMockFor($controller, $request);
101 $stack = new RequestStack();
102 $kernel = new ContainerAwareHttpKernel($dispatcher, $container, $resolver, $stack);
105 $kernel->handle($request, $type);
106 $this->fail('->handle() suppresses the controller exception');
107 } catch (\PHPUnit\Framework\Exception $e) {
109 } catch (\PHPUnit_Framework_Exception $e) {
111 } catch (\Exception $e) {
112 $this->assertSame($expected, $e, '->handle() throws the controller exception');
116 public function getProviderTypes()
119 array(HttpKernelInterface::MASTER_REQUEST),
120 array(HttpKernelInterface::SUB_REQUEST),
124 private function getResolverMockFor($controller, $request)
126 $resolver = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface')->getMock();
127 $resolver->expects($this->once())
128 ->method('getController')
130 ->will($this->returnValue($controller));
131 $resolver->expects($this->once())
132 ->method('getArguments')
133 ->with($request, $controller)
134 ->will($this->returnValue(array()));
139 private function expectsSetRequestWithAt($container, $with, $at)
142 ->expects($this->at($at))
144 ->with($this->equalTo('request'), $this->equalTo($with), $this->equalTo('request'))
150 private function expectsEnterScopeOnce($container)
153 ->expects($this->once())
154 ->method('enterScope')
155 ->with($this->equalTo('request'))
161 private function expectsLeaveScopeOnce($container)
164 ->expects($this->once())
165 ->method('leaveScope')
166 ->with($this->equalTo('request'))