Merge "Fix wrong @return type hints in Language::tsTo… methods"
[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( 'InvalidArgumentException' );
73
74 $services->getService( $name );
75 }
76
77 public function testDefineService() {
78 $services = $this->newServiceContainer();
79
80 $theService = new stdClass();
81 $name = 'TestService92834576';
82
83 $services->defineService( $name, function( $actualLocator ) use ( $services, $theService ) {
84 PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
85 return $theService;
86 } );
87
88 $this->assertTrue( $services->hasService( $name ) );
89 $this->assertSame( $theService, $services->getService( $name ) );
90 }
91
92 public function testDefineService_fail_duplicate() {
93 $services = $this->newServiceContainer();
94
95 $theService = new stdClass();
96 $name = 'TestService92834576';
97
98 $services->defineService( $name, function() use ( $theService ) {
99 return $theService;
100 } );
101
102 $this->setExpectedException( 'RuntimeException' );
103
104 $services->defineService( $name, function() use ( $theService ) {
105 return $theService;
106 } );
107 }
108
109 public function testApplyWiring() {
110 $services = $this->newServiceContainer();
111
112 $wiring = [
113 'Foo' => function() {
114 return 'Foo!';
115 },
116 'Bar' => function() {
117 return 'Bar!';
118 },
119 ];
120
121 $services->applyWiring( $wiring );
122
123 $this->assertSame( 'Foo!', $services->getService( 'Foo' ) );
124 $this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
125 }
126
127 public function testLoadWiringFiles() {
128 $services = $this->newServiceContainer();
129
130 $wiringFiles = [
131 __DIR__ . '/TestWiring1.php',
132 __DIR__ . '/TestWiring2.php',
133 ];
134
135 $services->loadWiringFiles( $wiringFiles );
136
137 $this->assertSame( 'Foo!', $services->getService( 'Foo' ) );
138 $this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
139 }
140
141 public function testLoadWiringFiles_fail_duplicate() {
142 $services = $this->newServiceContainer();
143
144 $wiringFiles = [
145 __DIR__ . '/TestWiring1.php',
146 __DIR__ . '/./TestWiring1.php',
147 ];
148
149 // loading the same file twice should fail, because
150 $this->setExpectedException( 'RuntimeException' );
151
152 $services->loadWiringFiles( $wiringFiles );
153 }
154
155 public function testRedefineService() {
156 $services = $this->newServiceContainer( [ 'Foo' ] );
157
158 $theService1 = new stdClass();
159 $name = 'TestService92834576';
160
161 $services->defineService( $name, function() {
162 PHPUnit_Framework_Assert::fail(
163 'The original instantiator function should not get called'
164 );
165 } );
166
167 // redefine before instantiation
168 $services->redefineService(
169 $name,
170 function( $actualLocator, $extra ) use ( $services, $theService1 ) {
171 PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
172 PHPUnit_Framework_Assert::assertSame( 'Foo', $extra );
173 return $theService1;
174 }
175 );
176
177 // force instantiation, check result
178 $this->assertSame( $theService1, $services->getService( $name ) );
179 }
180
181 public function testRedefineService_fail_undefined() {
182 $services = $this->newServiceContainer();
183
184 $theService = new stdClass();
185 $name = 'TestService92834576';
186
187 $this->setExpectedException( 'RuntimeException' );
188
189 $services->redefineService( $name, function() use ( $theService ) {
190 return $theService;
191 } );
192 }
193
194 public function testRedefineService_fail_in_use() {
195 $services = $this->newServiceContainer( [ 'Foo' ] );
196
197 $theService = new stdClass();
198 $name = 'TestService92834576';
199
200 $services->defineService( $name, function() {
201 return 'Foo';
202 } );
203
204 // create the service, so it can no longer be redefined
205 $services->getService( $name );
206
207 $this->setExpectedException( 'RuntimeException' );
208
209 $services->redefineService( $name, function() use ( $theService ) {
210 return $theService;
211 } );
212 }
213
214 }