1 | <?php |
||
20 | class ServiceParamConverterTest extends PHPUnit_Framework_TestCase |
||
21 | { |
||
22 | /** |
||
23 | * Dependency injection container. |
||
24 | * |
||
25 | * @var Container |
||
26 | */ |
||
27 | private $container; |
||
28 | |||
29 | /** |
||
30 | * Parameter converter. |
||
31 | * |
||
32 | * @var ServiceParamConverter |
||
33 | */ |
||
34 | private $converter; |
||
35 | |||
36 | public function setUp() |
||
37 | { |
||
38 | $this->container = $this->getMock('\Symfony\Component\DependencyInjection\Container'); |
||
39 | $this->converter = new ServiceParamConverter($this->container); |
||
40 | } |
||
41 | |||
42 | public function testSupports() |
||
43 | { |
||
44 | $this |
||
45 | ->container |
||
46 | ->expects($this->at(0)) |
||
47 | ->method('has') |
||
48 | ->with('dummy') |
||
49 | ->will($this->returnValue(true)); |
||
50 | $this |
||
51 | ->container |
||
52 | ->expects($this->at(1)) |
||
53 | ->method('has') |
||
54 | ->with('dummy2') |
||
55 | ->will($this->returnValue(false)); |
||
56 | |||
57 | |||
58 | $config = $this->createConfiguration(null, null, array( |
||
59 | 'service' => 'dummy', |
||
60 | 'method' => 'dummy', |
||
61 | )); |
||
62 | $this->assertTrue($this->converter->supports($config)); |
||
63 | |||
64 | $config = $this->createConfiguration(null, null, array( |
||
65 | 'service' => 'dummy2', |
||
66 | 'method' => 'dummy', |
||
67 | )); |
||
68 | $this->assertFalse($this->converter->supports($config)); |
||
69 | |||
70 | |||
71 | $config = $this->createConfiguration(__CLASS__); |
||
72 | $this->assertFalse($this->converter->supports($config)); |
||
73 | |||
74 | $config = $this->createConfiguration(); |
||
75 | $this->assertFalse($this->converter->supports($config)); |
||
76 | } |
||
77 | |||
78 | public function testApply() |
||
79 | { |
||
80 | $service = $this->getMock('stdClass', array('dummy')); |
||
81 | $service->expects($this->once()) |
||
82 | ->method('dummy') |
||
83 | ->will($this->returnValue(1)); |
||
84 | |||
85 | $this |
||
86 | ->container |
||
87 | ->expects($this->at(0)) |
||
88 | ->method('get') |
||
89 | ->with('dummy') |
||
90 | ->will($this->returnValue($service)); |
||
91 | |||
92 | |||
93 | $request = new Request(array(), array(), array('dummy' => '1')); |
||
94 | $config = $this->createConfiguration(null, 'dummyparam', array( |
||
95 | 'service' => 'dummy', |
||
96 | 'method' => 'dummy', |
||
97 | )); |
||
98 | |||
99 | $this->converter->apply($request, $config); |
||
100 | |||
101 | $this->assertEquals(1, $request->attributes->get('dummyparam')); |
||
102 | } |
||
103 | |||
104 | public function testApplyArguments() |
||
105 | { |
||
106 | $service = $this->getMock('stdClass', array('dummy')); |
||
107 | $service->expects($this->once()) |
||
108 | ->method('dummy') |
||
109 | ->with(1) |
||
110 | ->will($this->returnArgument(0)); |
||
111 | |||
112 | $this |
||
113 | ->container |
||
114 | ->expects($this->at(0)) |
||
115 | ->method('get') |
||
116 | ->with('dummy') |
||
117 | ->will($this->returnValue($service)); |
||
118 | |||
119 | |||
120 | $request = new Request(array(), array(), array()); |
||
121 | $config = $this->createConfiguration(null, 'dummyparam', array( |
||
122 | 'service' => 'dummy', |
||
123 | 'method' => 'dummy', |
||
124 | 'arguments' => array( |
||
125 | '1' |
||
126 | ) |
||
127 | )); |
||
128 | |||
129 | $this->converter->apply($request, $config); |
||
130 | |||
131 | $this->assertEquals(1, $request->attributes->get('dummyparam')); |
||
132 | } |
||
133 | |||
134 | public function testApplyRequestArguments() |
||
135 | { |
||
136 | $service = $this->getMock('stdClass', array('dummy')); |
||
137 | $service->expects($this->once()) |
||
138 | ->method('dummy') |
||
139 | ->with(1) |
||
140 | ->will($this->returnArgument(0)); |
||
141 | |||
142 | $this |
||
143 | ->container |
||
144 | ->expects($this->at(0)) |
||
145 | ->method('get') |
||
146 | ->with('dummy') |
||
147 | ->will($this->returnValue($service)); |
||
148 | |||
149 | |||
150 | $request = new Request(array('rp' => 1), array(), array()); |
||
151 | $config = $this->createConfiguration(null, 'dummyparam', array( |
||
152 | 'service' => 'dummy', |
||
153 | 'method' => 'dummy', |
||
154 | 'arguments' => array( |
||
155 | '%rp%' |
||
156 | ) |
||
157 | )); |
||
158 | |||
159 | $this->converter->apply($request, $config); |
||
160 | |||
161 | $this->assertEquals(1, $request->attributes->get('dummyparam')); |
||
162 | } |
||
163 | |||
164 | public function testApplyRequestService() |
||
165 | { |
||
166 | $intService = $this->getMock('stdClass', array('dummy')); |
||
167 | $service = $this->getMock('stdClass', array('dummy')); |
||
168 | $service->expects($this->once()) |
||
169 | ->method('dummy') |
||
170 | ->with($intService) |
||
171 | ->will($this->returnValue(1)); |
||
172 | |||
173 | $this |
||
174 | ->container |
||
175 | ->expects($this->at(0)) |
||
176 | ->method('has') |
||
177 | ->with('dummy2') |
||
178 | ->will($this->returnValue(true)); |
||
179 | |||
180 | $this |
||
181 | ->container |
||
182 | ->expects($this->at(1)) |
||
183 | ->method('get') |
||
184 | ->with('dummy2') |
||
185 | ->will($this->returnValue($intService)); |
||
186 | |||
187 | $this |
||
188 | ->container |
||
189 | ->expects($this->at(2)) |
||
190 | ->method('get') |
||
191 | ->with('dummy') |
||
192 | ->will($this->returnValue($service)); |
||
193 | |||
194 | $request = new Request(array(), array(), array()); |
||
195 | $config = $this->createConfiguration(null, 'dummyparam', array( |
||
196 | 'service' => 'dummy', |
||
197 | 'method' => 'dummy', |
||
198 | 'arguments' => array( |
||
199 | '@dummy2' |
||
200 | ) |
||
201 | )); |
||
202 | |||
203 | $this->converter->apply($request, $config); |
||
204 | |||
205 | $this->assertEquals(1, $request->attributes->get('dummyparam')); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @expectedException \InvalidArgumentException |
||
210 | */ |
||
211 | public function testApplyRequestNonExistentService() |
||
212 | { |
||
213 | $this |
||
214 | ->container |
||
215 | ->expects($this->at(0)) |
||
216 | ->method('has') |
||
217 | ->with('dummy2') |
||
218 | ->will($this->returnValue(false)); |
||
219 | |||
220 | $request = new Request(array(), array(), array()); |
||
221 | $config = $this->createConfiguration(null, 'dummyparam', array( |
||
222 | 'service' => 'dummy', |
||
223 | 'method' => 'dummy', |
||
224 | 'arguments' => array( |
||
225 | '@dummy2' |
||
226 | ) |
||
227 | )); |
||
228 | |||
229 | $this->converter->apply($request, $config); |
||
230 | } |
||
231 | |||
232 | |||
233 | public function createConfiguration($class = null, $name = null, array $options = array()) |
||
234 | { |
||
235 | $config = $this |
||
236 | ->getMockBuilder('Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter') |
||
237 | ->setMethods(array('getClass', 'getAliasName', 'getOptions', 'getName', 'allowArray', 'isOptional')) |
||
238 | ->disableOriginalConstructor() |
||
239 | ->getMock(); |
||
240 | |||
241 | if ($name !== null) { |
||
242 | $config->expects($this->any()) |
||
243 | ->method('getName') |
||
244 | ->will($this->returnValue($name)); |
||
245 | } |
||
246 | if ($class !== null) { |
||
247 | $config->expects($this->any()) |
||
248 | ->method('getClass') |
||
249 | ->will($this->returnValue($class)); |
||
250 | } |
||
251 | if ($options !== array()) { |
||
252 | $config->expects($this->any()) |
||
253 | ->method('getOptions') |
||
254 | ->will($this->returnValue($options)); |
||
255 | } |
||
256 | |||
257 | return $config; |
||
258 | } |
||
259 | } |
||
260 |