2 use MediaWiki\Services\ServiceContainer
;
5 * @covers MediaWiki\Services\ServiceContainer
9 class ServiceContainerTest
extends PHPUnit_Framework_TestCase
{
11 private function newServiceContainer( $extraArgs = [] ) {
12 return new ServiceContainer( $extraArgs );
15 public function testGetServiceNames() {
16 $services = $this->newServiceContainer();
17 $names = $services->getServiceNames();
19 $this->assertInternalType( 'array', $names );
20 $this->assertEmpty( $names );
22 $name = 'TestService92834576';
23 $services->defineService( $name, function() {
27 $names = $services->getServiceNames();
28 $this->assertContains( $name, $names );
31 public function testHasService() {
32 $services = $this->newServiceContainer();
34 $name = 'TestService92834576';
35 $this->assertFalse( $services->hasService( $name ) );
37 $services->defineService( $name, function() {
41 $this->assertTrue( $services->hasService( $name ) );
44 public function testGetService() {
45 $services = $this->newServiceContainer( [ 'Foo' ] );
47 $theService = new stdClass();
48 $name = 'TestService92834576';
51 $services->defineService(
53 function( $actualLocator, $extra ) use ( $services, $theService, &$count ) {
55 PHPUnit_Framework_Assert
::assertSame( $services, $actualLocator );
56 PHPUnit_Framework_Assert
::assertSame( $extra, 'Foo' );
61 $this->assertSame( $theService, $services->getService( $name ) );
63 $services->getService( $name );
64 $this->assertSame( 1, $count, 'instantiator should be called exactly once!' );
67 public function testGetService_fail_unknown() {
68 $services = $this->newServiceContainer();
70 $name = 'TestService92834576';
72 $this->setExpectedException( 'InvalidArgumentException' );
74 $services->getService( $name );
77 public function testDefineService() {
78 $services = $this->newServiceContainer();
80 $theService = new stdClass();
81 $name = 'TestService92834576';
83 $services->defineService( $name, function( $actualLocator ) use ( $services, $theService ) {
84 PHPUnit_Framework_Assert
::assertSame( $services, $actualLocator );
88 $this->assertTrue( $services->hasService( $name ) );
89 $this->assertSame( $theService, $services->getService( $name ) );
92 public function testDefineService_fail_duplicate() {
93 $services = $this->newServiceContainer();
95 $theService = new stdClass();
96 $name = 'TestService92834576';
98 $services->defineService( $name, function() use ( $theService ) {
102 $this->setExpectedException( 'RuntimeException' );
104 $services->defineService( $name, function() use ( $theService ) {
109 public function testApplyWiring() {
110 $services = $this->newServiceContainer();
113 'Foo' => function() {
116 'Bar' => function() {
121 $services->applyWiring( $wiring );
123 $this->assertSame( 'Foo!', $services->getService( 'Foo' ) );
124 $this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
127 public function testLoadWiringFiles() {
128 $services = $this->newServiceContainer();
131 __DIR__
. '/TestWiring1.php',
132 __DIR__
. '/TestWiring2.php',
135 $services->loadWiringFiles( $wiringFiles );
137 $this->assertSame( 'Foo!', $services->getService( 'Foo' ) );
138 $this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
141 public function testLoadWiringFiles_fail_duplicate() {
142 $services = $this->newServiceContainer();
145 __DIR__
. '/TestWiring1.php',
146 __DIR__
. '/./TestWiring1.php',
149 // loading the same file twice should fail, because
150 $this->setExpectedException( 'RuntimeException' );
152 $services->loadWiringFiles( $wiringFiles );
155 public function testRedefineService() {
156 $services = $this->newServiceContainer( [ 'Foo' ] );
158 $theService1 = new stdClass();
159 $name = 'TestService92834576';
161 $services->defineService( $name, function() {
162 PHPUnit_Framework_Assert
::fail(
163 'The original instantiator function should not get called'
167 // redefine before instantiation
168 $services->redefineService(
170 function( $actualLocator, $extra ) use ( $services, $theService1 ) {
171 PHPUnit_Framework_Assert
::assertSame( $services, $actualLocator );
172 PHPUnit_Framework_Assert
::assertSame( 'Foo', $extra );
177 // force instantiation, check result
178 $this->assertSame( $theService1, $services->getService( $name ) );
181 public function testRedefineService_fail_undefined() {
182 $services = $this->newServiceContainer();
184 $theService = new stdClass();
185 $name = 'TestService92834576';
187 $this->setExpectedException( 'RuntimeException' );
189 $services->redefineService( $name, function() use ( $theService ) {
194 public function testRedefineService_fail_in_use() {
195 $services = $this->newServiceContainer( [ 'Foo' ] );
197 $theService = new stdClass();
198 $name = 'TestService92834576';
200 $services->defineService( $name, function() {
204 // create the service, so it can no longer be redefined
205 $services->getService( $name );
207 $this->setExpectedException( 'RuntimeException' );
209 $services->redefineService( $name, function() use ( $theService ) {