Merge "Introduce mediawiki.router for handling hash fragment navigation"
[lhc/web/wiklou.git] / tests / phpunit / includes / Services / ServiceContainerTest.php
1 <?php
2 use MediaWiki\Services\ServiceContainer;
3
4 /**
5 * @covers MediaWiki\Services\ServiceContainer
6 *
7 * @group MediaWiki
8 */
9 class ServiceContainerTest extends PHPUnit_Framework_TestCase {
10
11 private function newServiceContainer( $extraArgs = [] ) {
12 return new ServiceContainer( $extraArgs );
13 }
14
15 public function testGetServiceNames() {
16 $services = $this->newServiceContainer();
17 $names = $services->getServiceNames();
18
19 $this->assertInternalType( 'array', $names );
20 $this->assertEmpty( $names );
21
22 $name = 'TestService92834576';
23 $services->defineService( $name, function() {
24 return null;
25 } );
26
27 $names = $services->getServiceNames();
28 $this->assertContains( $name, $names );
29 }
30
31 public function testHasService() {
32 $services = $this->newServiceContainer();
33
34 $name = 'TestService92834576';
35 $this->assertFalse( $services->hasService( $name ) );
36
37 $services->defineService( $name, function() {
38 return null;
39 } );
40
41 $this->assertTrue( $services->hasService( $name ) );
42 }
43
44 public function testGetService() {
45 $services = $this->newServiceContainer( [ 'Foo' ] );
46
47 $theService = new stdClass();
48 $name = 'TestService92834576';
49 $count = 0;
50
51 $services->defineService(
52 $name,
53 function( $actualLocator, $extra ) use ( $services, $theService, &$count ) {
54 $count++;
55 PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
56 PHPUnit_Framework_Assert::assertSame( $extra, 'Foo' );
57 return $theService;
58 }
59 );
60
61 $this->assertSame( $theService, $services->getService( $name ) );
62
63 $services->getService( $name );
64 $this->assertSame( 1, $count, 'instantiator should be called exactly once!' );
65 }
66
67 public function testGetService_fail_unknown() {
68 $services = $this->newServiceContainer();
69
70 $name = 'TestService92834576';
71
72 $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
73
74 $services->getService( $name );
75 }
76
77 public function testPeekService() {
78 $services = $this->newServiceContainer();
79
80 $services->defineService(
81 'Foo',
82 function() {
83 return new stdClass();
84 }
85 );
86
87 $services->defineService(
88 'Bar',
89 function() {
90 return new stdClass();
91 }
92 );
93
94 // trigger instantiation of Foo
95 $services->getService( 'Foo' );
96
97 $this->assertInternalType(
98 'object',
99 $services->peekService( 'Foo' ),
100 'Peek should return the service object if it had been accessed before.'
101 );
102
103 $this->assertNull(
104 $services->peekService( 'Bar' ),
105 'Peek should return null if the service was never accessed.'
106 );
107 }
108
109 public function testPeekService_fail_unknown() {
110 $services = $this->newServiceContainer();
111
112 $name = 'TestService92834576';
113
114 $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
115
116 $services->peekService( $name );
117 }
118
119 public function testDefineService() {
120 $services = $this->newServiceContainer();
121
122 $theService = new stdClass();
123 $name = 'TestService92834576';
124
125 $services->defineService( $name, function( $actualLocator ) use ( $services, $theService ) {
126 PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
127 return $theService;
128 } );
129
130 $this->assertTrue( $services->hasService( $name ) );
131 $this->assertSame( $theService, $services->getService( $name ) );
132 }
133
134 public function testDefineService_fail_duplicate() {
135 $services = $this->newServiceContainer();
136
137 $theService = new stdClass();
138 $name = 'TestService92834576';
139
140 $services->defineService( $name, function() use ( $theService ) {
141 return $theService;
142 } );
143
144 $this->setExpectedException( 'MediaWiki\Services\ServiceAlreadyDefinedException' );
145
146 $services->defineService( $name, function() use ( $theService ) {
147 return $theService;
148 } );
149 }
150
151 public function testApplyWiring() {
152 $services = $this->newServiceContainer();
153
154 $wiring = [
155 'Foo' => function() {
156 return 'Foo!';
157 },
158 'Bar' => function() {
159 return 'Bar!';
160 },
161 ];
162
163 $services->applyWiring( $wiring );
164
165 $this->assertSame( 'Foo!', $services->getService( 'Foo' ) );
166 $this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
167 }
168
169 public function testLoadWiringFiles() {
170 $services = $this->newServiceContainer();
171
172 $wiringFiles = [
173 __DIR__ . '/TestWiring1.php',
174 __DIR__ . '/TestWiring2.php',
175 ];
176
177 $services->loadWiringFiles( $wiringFiles );
178
179 $this->assertSame( 'Foo!', $services->getService( 'Foo' ) );
180 $this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
181 }
182
183 public function testLoadWiringFiles_fail_duplicate() {
184 $services = $this->newServiceContainer();
185
186 $wiringFiles = [
187 __DIR__ . '/TestWiring1.php',
188 __DIR__ . '/./TestWiring1.php',
189 ];
190
191 // loading the same file twice should fail, because
192 $this->setExpectedException( 'MediaWiki\Services\ServiceAlreadyDefinedException' );
193
194 $services->loadWiringFiles( $wiringFiles );
195 }
196
197 public function testRedefineService() {
198 $services = $this->newServiceContainer( [ 'Foo' ] );
199
200 $theService1 = new stdClass();
201 $name = 'TestService92834576';
202
203 $services->defineService( $name, function() {
204 PHPUnit_Framework_Assert::fail(
205 'The original instantiator function should not get called'
206 );
207 } );
208
209 // redefine before instantiation
210 $services->redefineService(
211 $name,
212 function( $actualLocator, $extra ) use ( $services, $theService1 ) {
213 PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
214 PHPUnit_Framework_Assert::assertSame( 'Foo', $extra );
215 return $theService1;
216 }
217 );
218
219 // force instantiation, check result
220 $this->assertSame( $theService1, $services->getService( $name ) );
221 }
222
223 public function testRedefineService_fail_undefined() {
224 $services = $this->newServiceContainer();
225
226 $theService = new stdClass();
227 $name = 'TestService92834576';
228
229 $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
230
231 $services->redefineService( $name, function() use ( $theService ) {
232 return $theService;
233 } );
234 }
235
236 public function testRedefineService_fail_in_use() {
237 $services = $this->newServiceContainer( [ 'Foo' ] );
238
239 $theService = new stdClass();
240 $name = 'TestService92834576';
241
242 $services->defineService( $name, function() {
243 return 'Foo';
244 } );
245
246 // create the service, so it can no longer be redefined
247 $services->getService( $name );
248
249 $this->setExpectedException( 'MediaWiki\Services\CannotReplaceActiveServiceException' );
250
251 $services->redefineService( $name, function() use ( $theService ) {
252 return $theService;
253 } );
254 }
255
256 public function testDisableService() {
257 $services = $this->newServiceContainer( [ 'Foo' ] );
258
259 $destructible = $this->getMock( 'MediaWiki\Services\DestructibleService' );
260 $destructible->expects( $this->once() )
261 ->method( 'destroy' );
262
263 $services->defineService( 'Foo', function() use ( $destructible ) {
264 return $destructible;
265 } );
266 $services->defineService( 'Bar', function() {
267 return new stdClass();
268 } );
269 $services->defineService( 'Qux', function() {
270 return new stdClass();
271 } );
272
273 // instantiate Foo and Bar services
274 $services->getService( 'Foo' );
275 $services->getService( 'Bar' );
276
277 // disable service, should call destroy() once.
278 $services->disableService( 'Foo' );
279
280 // disabled service should still be listed
281 $this->assertContains( 'Foo', $services->getServiceNames() );
282
283 // getting other services should still work
284 $services->getService( 'Bar' );
285
286 // disable non-destructible service, and not-yet-instantiated service
287 $services->disableService( 'Bar' );
288 $services->disableService( 'Qux' );
289
290 $this->assertNull( $services->peekService( 'Bar' ) );
291 $this->assertNull( $services->peekService( 'Qux' ) );
292
293 // disabled service should still be listed
294 $this->assertContains( 'Bar', $services->getServiceNames() );
295 $this->assertContains( 'Qux', $services->getServiceNames() );
296
297 // re-enable Bar service
298 $services->redefineService( 'Bar', function() {
299 return new stdClass();
300 } );
301
302 $services->getService( 'Bar' );
303
304 $this->setExpectedException( 'MediaWiki\Services\ServiceDisabledException' );
305 $services->getService( 'Qux' );
306 }
307
308 public function testDisableService_fail_undefined() {
309 $services = $this->newServiceContainer();
310
311 $theService = new stdClass();
312 $name = 'TestService92834576';
313
314 $this->setExpectedException( 'MediaWiki\Services\NoSuchServiceException' );
315
316 $services->redefineService( $name, function() use ( $theService ) {
317 return $theService;
318 } );
319 }
320
321 public function testDestroy() {
322 $services = $this->newServiceContainer();
323
324 $destructible = $this->getMock( 'MediaWiki\Services\DestructibleService' );
325 $destructible->expects( $this->once() )
326 ->method( 'destroy' );
327
328 $services->defineService( 'Foo', function() use ( $destructible ) {
329 return $destructible;
330 } );
331
332 $services->defineService( 'Bar', function() {
333 return new stdClass();
334 } );
335
336 // create the service
337 $services->getService( 'Foo' );
338
339 // destroy the container
340 $services->destroy();
341
342 $this->setExpectedException( 'MediaWiki\Services\ContainerDisabledException' );
343 $services->getService( 'Bar' );
344 }
345
346 }