MediaWiki REL1_27
ServiceContainerTest.php
Go to the documentation of this file.
1<?php
3
9class ServiceContainerTest extends PHPUnit_Framework_TestCase {
10
11 private function newServiceContainer( $extraArgs = [] ) {
12 return new ServiceContainer( $extraArgs );
13 }
14
15 public function testGetServiceNames() {
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() {
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() {
69
70 $name = 'TestService92834576';
71
72 $this->setExpectedException( 'InvalidArgumentException' );
73
74 $services->getService( $name );
75 }
76
77 public function testDefineService() {
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
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() {
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() {
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
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
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
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}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
ServiceContainer provides a generic service to manage named services using lazy instantiation based o...
MediaWiki\Services\ServiceContainer.
newServiceContainer( $extraArgs=[])
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place or wrap existing services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition hooks.txt:2043
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:314
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37