|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony framework. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* This source file is subject to the MIT license that is bundled |
|
|
9 |
* with this source code in the file LICENSE. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Bundle\FrameworkBundle\Tests; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
|
15 |
use Symfony\Component\HttpFoundation\Response; |
|
|
16 |
use Symfony\Component\HttpFoundation\Request; |
|
|
17 |
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
|
18 |
use Symfony\Bundle\FrameworkBundle\HttpKernel; |
|
|
19 |
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
|
20 |
|
|
|
21 |
class HttpKernelTest extends \PHPUnit_Framework_TestCase |
|
|
22 |
{ |
|
|
23 |
/** |
|
|
24 |
* @dataProvider getProviderTypes |
|
|
25 |
*/ |
|
|
26 |
public function testHandle($type) |
|
|
27 |
{ |
|
|
28 |
$request = new Request(); |
|
|
29 |
$expected = new Response(); |
|
|
30 |
|
|
|
31 |
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
|
|
32 |
$container |
|
|
33 |
->expects($this->once()) |
|
|
34 |
->method('enterScope') |
|
|
35 |
->with($this->equalTo('request')) |
|
|
36 |
; |
|
|
37 |
$container |
|
|
38 |
->expects($this->once()) |
|
|
39 |
->method('leaveScope') |
|
|
40 |
->with($this->equalTo('request')) |
|
|
41 |
; |
|
|
42 |
$container |
|
|
43 |
->expects($this->once()) |
|
|
44 |
->method('set') |
|
|
45 |
->with($this->equalTo('request'), $this->equalTo($request), $this->equalTo('request')) |
|
|
46 |
; |
|
|
47 |
|
|
|
48 |
$dispatcher = new EventDispatcher(); |
|
|
49 |
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface'); |
|
|
50 |
$kernel = new HttpKernel($dispatcher, $container, $resolver); |
|
|
51 |
|
|
|
52 |
$controller = function() use($expected) |
|
|
53 |
{ |
|
|
54 |
return $expected; |
|
|
55 |
}; |
|
|
56 |
|
|
|
57 |
$resolver->expects($this->once()) |
|
|
58 |
->method('getController') |
|
|
59 |
->with($request) |
|
|
60 |
->will($this->returnValue($controller)); |
|
|
61 |
$resolver->expects($this->once()) |
|
|
62 |
->method('getArguments') |
|
|
63 |
->with($request, $controller) |
|
|
64 |
->will($this->returnValue(array())); |
|
|
65 |
|
|
|
66 |
$actual = $kernel->handle($request, $type); |
|
|
67 |
|
|
|
68 |
$this->assertSame($expected, $actual, '->handle() returns the response'); |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
/** |
|
|
72 |
* @dataProvider getProviderTypes |
|
|
73 |
*/ |
|
|
74 |
public function testHandleRestoresThePreviousRequestOnException($type) |
|
|
75 |
{ |
|
|
76 |
$request = new Request(); |
|
|
77 |
$expected = new \Exception(); |
|
|
78 |
|
|
|
79 |
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); |
|
|
80 |
$container |
|
|
81 |
->expects($this->once()) |
|
|
82 |
->method('enterScope') |
|
|
83 |
->with($this->equalTo('request')) |
|
|
84 |
; |
|
|
85 |
$container |
|
|
86 |
->expects($this->once()) |
|
|
87 |
->method('leaveScope') |
|
|
88 |
->with($this->equalTo('request')) |
|
|
89 |
; |
|
|
90 |
$container |
|
|
91 |
->expects($this->once()) |
|
|
92 |
->method('set') |
|
|
93 |
->with($this->equalTo('request'), $this->equalTo($request), $this->equalTo('request')) |
|
|
94 |
; |
|
|
95 |
|
|
|
96 |
$dispatcher = new EventDispatcher(); |
|
|
97 |
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface'); |
|
|
98 |
$kernel = new HttpKernel($dispatcher, $container, $resolver); |
|
|
99 |
|
|
|
100 |
$controller = function() use ($expected) |
|
|
101 |
{ |
|
|
102 |
throw $expected; |
|
|
103 |
}; |
|
|
104 |
|
|
|
105 |
$resolver->expects($this->once()) |
|
|
106 |
->method('getController') |
|
|
107 |
->with($request) |
|
|
108 |
->will($this->returnValue($controller)); |
|
|
109 |
$resolver->expects($this->once()) |
|
|
110 |
->method('getArguments') |
|
|
111 |
->with($request, $controller) |
|
|
112 |
->will($this->returnValue(array())); |
|
|
113 |
|
|
|
114 |
try { |
|
|
115 |
$kernel->handle($request, $type); |
|
|
116 |
$this->fail('->handle() suppresses the controller exception'); |
|
|
117 |
} catch (\Exception $actual) { |
|
|
118 |
$this->assertSame($expected, $actual, '->handle() throws the controller exception'); |
|
|
119 |
} |
|
|
120 |
} |
|
|
121 |
|
|
|
122 |
public function testGenerateInternalUriHandlesNullValues() |
|
|
123 |
{ |
|
|
124 |
$request = new Request(); |
|
|
125 |
|
|
|
126 |
$router = $this->getMock('Symfony\\Component\\Routing\\RouterInterface'); |
|
|
127 |
$container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface'); |
|
|
128 |
$container |
|
|
129 |
->expects($this->at(0)) |
|
|
130 |
->method('get') |
|
|
131 |
->with($this->equalTo('router')) |
|
|
132 |
->will($this->returnValue($router)) |
|
|
133 |
; |
|
|
134 |
$container |
|
|
135 |
->expects($this->at('1')) |
|
|
136 |
->method('get') |
|
|
137 |
->with($this->equalTo('request')) |
|
|
138 |
->will($this->returnValue($request)) |
|
|
139 |
; |
|
|
140 |
|
|
|
141 |
$controller = 'AController'; |
|
|
142 |
$attributes = array('anAttribute' => null); |
|
|
143 |
$query = array('aQueryParam' => null); |
|
|
144 |
|
|
|
145 |
$expectedPath = 'none'; |
|
|
146 |
|
|
|
147 |
$routeParameters = array('controller' => $controller, 'path' => $expectedPath, '_format' => 'html'); |
|
|
148 |
$router |
|
|
149 |
->expects($this->once()) |
|
|
150 |
->method('generate') |
|
|
151 |
->with($this->equalTo('_internal'), $this->equalTo($routeParameters)) |
|
|
152 |
->will($this->returnValue('GENERATED_URI')) |
|
|
153 |
; |
|
|
154 |
|
|
|
155 |
$dispatcher = new EventDispatcher(); |
|
|
156 |
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface'); |
|
|
157 |
$kernel = new HttpKernel($dispatcher, $container, $resolver); |
|
|
158 |
|
|
|
159 |
$uri = $kernel->generateInternalUri($controller, $attributes, $query); |
|
|
160 |
$this->assertEquals('GENERATED_URI', $uri); |
|
|
161 |
} |
|
|
162 |
|
|
|
163 |
public function getProviderTypes() |
|
|
164 |
{ |
|
|
165 |
return array( |
|
|
166 |
array(HttpKernelInterface::MASTER_REQUEST), |
|
|
167 |
array(HttpKernelInterface::SUB_REQUEST), |
|
|
168 |
); |
|
|
169 |
} |
|
|
170 |
} |