db backup prior to drupal security update
[yaffs-website] / vendor / symfony / http-kernel / Tests / DependencyInjection / ContainerAwareHttpKernelTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
13
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;
21
22 /**
23  * @group legacy
24  */
25 class ContainerAwareHttpKernelTest extends TestCase
26 {
27     /**
28      * @dataProvider getProviderTypes
29      */
30     public function testHandle($type)
31     {
32         $request = new Request();
33         $expected = new Response();
34         $controller = function () use ($expected) {
35             return $expected;
36         };
37
38         $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
39         $this
40             ->expectsEnterScopeOnce($container)
41             ->expectsLeaveScopeOnce($container)
42             ->expectsSetRequestWithAt($container, $request, 3)
43             ->expectsSetRequestWithAt($container, null, 4)
44         ;
45
46         $dispatcher = new EventDispatcher();
47         $resolver = $this->getResolverMockFor($controller, $request);
48         $stack = new RequestStack();
49         $kernel = new ContainerAwareHttpKernel($dispatcher, $container, $resolver, $stack);
50
51         $actual = $kernel->handle($request, $type);
52
53         $this->assertSame($expected, $actual, '->handle() returns the response');
54     }
55
56     /**
57      * @dataProvider getProviderTypes
58      */
59     public function testVerifyRequestStackPushPopDuringHandle($type)
60     {
61         $request = new Request();
62         $expected = new Response();
63         $controller = function () use ($expected) {
64             return $expected;
65         };
66
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');
70
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);
75
76         $kernel->handle($request, $type);
77     }
78
79     /**
80      * @dataProvider getProviderTypes
81      */
82     public function testHandleRestoresThePreviousRequestOnException($type)
83     {
84         $request = new Request();
85         $expected = new \Exception();
86         $controller = function () use ($expected) {
87             throw $expected;
88         };
89
90         $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
91         $this
92             ->expectsEnterScopeOnce($container)
93             ->expectsLeaveScopeOnce($container)
94             ->expectsSetRequestWithAt($container, $request, 3)
95             ->expectsSetRequestWithAt($container, null, 4)
96         ;
97
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);
103
104         try {
105             $kernel->handle($request, $type);
106             $this->fail('->handle() suppresses the controller exception');
107         } catch (\PHPUnit\Framework\Exception $e) {
108             throw $e;
109         } catch (\PHPUnit_Framework_Exception $e) {
110             throw $e;
111         } catch (\Exception $e) {
112             $this->assertSame($expected, $e, '->handle() throws the controller exception');
113         }
114     }
115
116     public function getProviderTypes()
117     {
118         return array(
119             array(HttpKernelInterface::MASTER_REQUEST),
120             array(HttpKernelInterface::SUB_REQUEST),
121         );
122     }
123
124     private function getResolverMockFor($controller, $request)
125     {
126         $resolver = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface')->getMock();
127         $resolver->expects($this->once())
128             ->method('getController')
129             ->with($request)
130             ->will($this->returnValue($controller));
131         $resolver->expects($this->once())
132             ->method('getArguments')
133             ->with($request, $controller)
134             ->will($this->returnValue(array()));
135
136         return $resolver;
137     }
138
139     private function expectsSetRequestWithAt($container, $with, $at)
140     {
141         $container
142             ->expects($this->at($at))
143             ->method('set')
144             ->with($this->equalTo('request'), $this->equalTo($with), $this->equalTo('request'))
145         ;
146
147         return $this;
148     }
149
150     private function expectsEnterScopeOnce($container)
151     {
152         $container
153             ->expects($this->once())
154             ->method('enterScope')
155             ->with($this->equalTo('request'))
156         ;
157
158         return $this;
159     }
160
161     private function expectsLeaveScopeOnce($container)
162     {
163         $container
164             ->expects($this->once())
165             ->method('leaveScope')
166             ->with($this->equalTo('request'))
167         ;
168
169         return $this;
170     }
171 }