19 $this->assertInternalType(
'array', $names );
20 $this->assertEmpty( $names );
22 $name =
'TestService92834576';
28 $this->assertContains(
$name, $names );
34 $name =
'TestService92834576';
47 $theService =
new stdClass();
48 $name =
'TestService92834576';
55 PHPUnit_Framework_Assert::assertSame(
$services, $actualLocator );
56 PHPUnit_Framework_Assert::assertSame( $extra,
'Foo' );
64 $this->assertSame( 1,
$count,
'instantiator should be called exactly once!' );
70 $name =
'TestService92834576';
72 $this->setExpectedException(
'MediaWiki\Services\NoSuchServiceException' );
83 return new stdClass();
90 return new stdClass();
97 $this->assertInternalType(
100 'Peek should return the service object if it had been accessed before.'
105 'Peek should return null if the service was never accessed.'
112 $name =
'TestService92834576';
114 $this->setExpectedException(
'MediaWiki\Services\NoSuchServiceException' );
122 $theService =
new stdClass();
123 $name =
'TestService92834576';
126 PHPUnit_Framework_Assert::assertSame(
$services, $actualLocator );
137 $theService =
new stdClass();
138 $name =
'TestService92834576';
144 $this->setExpectedException(
'MediaWiki\Services\ServiceAlreadyDefinedException' );
155 'Foo' =>
function() {
158 'Bar' =>
function() {
165 $this->assertSame(
'Foo!',
$services->getService(
'Foo' ) );
166 $this->assertSame(
'Bar!',
$services->getService(
'Bar' ) );
173 'Foo' =>
function() {
176 'Bar' =>
function() {
179 'Car' =>
function() {
190 $newServices->defineService(
'Car',
function() {
195 $newServices->getService(
'Car' );
199 $newServices->defineService(
'Xar',
function() {
204 $newServices->importWiring(
$services, [
'Bar' ] );
206 $this->assertNotContains(
'Bar', $newServices->getServiceNames(),
'Skip `Bar` service' );
207 $this->assertSame(
'Foo!', $newServices->getService(
'Foo' ) );
212 $this->assertContains(
'Bar', $newServices->getServiceNames(),
'Import all services' );
213 $this->assertSame(
'Bar!', $newServices->getService(
'Bar' ) );
214 $this->assertSame(
'Car!', $newServices->getService(
'Car' ),
'Use existing service instance' );
215 $this->assertSame(
'Xar!', $newServices->getService(
'Xar' ),
'Predefined services are kept' );
222 __DIR__ .
'/TestWiring1.php',
223 __DIR__ .
'/TestWiring2.php',
226 $services->loadWiringFiles( $wiringFiles );
228 $this->assertSame(
'Foo!',
$services->getService(
'Foo' ) );
229 $this->assertSame(
'Bar!',
$services->getService(
'Bar' ) );
236 __DIR__ .
'/TestWiring1.php',
237 __DIR__ .
'/./TestWiring1.php',
241 $this->setExpectedException(
'MediaWiki\Services\ServiceAlreadyDefinedException' );
243 $services->loadWiringFiles( $wiringFiles );
249 $theService1 =
new stdClass();
250 $name =
'TestService92834576';
253 PHPUnit_Framework_Assert::fail(
254 'The original instantiator function should not get called'
261 function( $actualLocator, $extra )
use (
$services, $theService1 ) {
262 PHPUnit_Framework_Assert::assertSame(
$services, $actualLocator );
263 PHPUnit_Framework_Assert::assertSame(
'Foo', $extra );
269 $this->assertSame( $theService1,
$services->getService(
$name ) );
275 $theService1 =
new stdClass();
276 $name =
'TestService92834576';
290 $this->assertSame( $theService1,
$services->getService(
$name ) );
296 $theService =
new stdClass();
297 $name =
'TestService92834576';
299 $this->setExpectedException(
'MediaWiki\Services\NoSuchServiceException' );
309 $theService =
new stdClass();
310 $name =
'TestService92834576';
319 $this->setExpectedException(
'MediaWiki\Services\CannotReplaceActiveServiceException' );
329 $destructible = $this->getMock(
'MediaWiki\Services\DestructibleService' );
330 $destructible->expects( $this->once() )
331 ->method(
'destroy' );
333 $services->defineService(
'Foo',
function()
use ( $destructible ) {
334 return $destructible;
336 $services->defineService(
'Bar',
function() {
337 return new stdClass();
339 $services->defineService(
'Qux',
function() {
340 return new stdClass();
351 $this->assertContains(
'Foo',
$services->getServiceNames() );
360 $this->assertNull(
$services->peekService(
'Bar' ) );
361 $this->assertNull(
$services->peekService(
'Qux' ) );
364 $this->assertContains(
'Bar',
$services->getServiceNames() );
365 $this->assertContains(
'Qux',
$services->getServiceNames() );
367 $this->setExpectedException(
'MediaWiki\Services\ServiceDisabledException' );
374 $theService =
new stdClass();
375 $name =
'TestService92834576';
377 $this->setExpectedException(
'MediaWiki\Services\NoSuchServiceException' );
387 $destructible = $this->getMock(
'MediaWiki\Services\DestructibleService' );
388 $destructible->expects( $this->once() )
389 ->method(
'destroy' );
391 $services->defineService(
'Foo',
function()
use ( $destructible ) {
392 return $destructible;
395 $services->defineService(
'Bar',
function() {
396 return new stdClass();
405 $this->setExpectedException(
'MediaWiki\Services\ContainerDisabledException' );
testRedefineService_fail_undefined()
MediaWiki\Services\ServiceContainer.
testPeekService_fail_unknown()
testLoadWiringFiles_fail_duplicate()
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testRedefineService_disabled()
testRedefineService_fail_in_use()
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 services the preferred way to define a new service is the $wgServiceWiringFiles array $services
testDefineService_fail_duplicate()
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
testGetService_fail_unknown()
testDisableService_fail_undefined()
Allows to change the fields on the form that will be generated $name