MediaWiki  1.30.0
ConfigFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
10  public function testRegister() {
11  $factory = new ConfigFactory();
12  $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
13  $this->assertInstanceOf( GlobalVarConfig::class, $factory->makeConfig( 'unittest' ) );
14  }
15 
19  public function testRegisterInvalid() {
20  $factory = new ConfigFactory();
21  $this->setExpectedException( 'InvalidArgumentException' );
22  $factory->register( 'invalid', 'Invalid callback' );
23  }
24 
28  public function testRegisterInstance() {
29  $config = GlobalVarConfig::newInstance();
30  $factory = new ConfigFactory();
31  $factory->register( 'unittest', $config );
32  $this->assertSame( $config, $factory->makeConfig( 'unittest' ) );
33  }
34 
38  public function testRegisterAgain() {
39  $factory = new ConfigFactory();
40  $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
41  $config1 = $factory->makeConfig( 'unittest' );
42 
43  $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
44  $config2 = $factory->makeConfig( 'unittest' );
45 
46  $this->assertNotSame( $config1, $config2 );
47  }
48 
52  public function testSalvage() {
53  $oldFactory = new ConfigFactory();
54  $oldFactory->register( 'foo', 'GlobalVarConfig::newInstance' );
55  $oldFactory->register( 'bar', 'GlobalVarConfig::newInstance' );
56  $oldFactory->register( 'quux', 'GlobalVarConfig::newInstance' );
57 
58  // instantiate two of the three defined configurations
59  $foo = $oldFactory->makeConfig( 'foo' );
60  $bar = $oldFactory->makeConfig( 'bar' );
61  $quux = $oldFactory->makeConfig( 'quux' );
62 
63  // define new config instance
64  $newFactory = new ConfigFactory();
65  $newFactory->register( 'foo', 'GlobalVarConfig::newInstance' );
66  $newFactory->register( 'bar', function () {
67  return new HashConfig();
68  } );
69 
70  // "foo" and "quux" are defined in the old and the new factory.
71  // The old factory has instances for "foo" and "bar", but not "quux".
72  $newFactory->salvage( $oldFactory );
73 
74  $newFoo = $newFactory->makeConfig( 'foo' );
75  $this->assertSame( $foo, $newFoo, 'existing instance should be salvaged' );
76 
77  $newBar = $newFactory->makeConfig( 'bar' );
78  $this->assertNotSame( $bar, $newBar, 'don\'t salvage if callbacks differ' );
79 
80  // the new factory doesn't have quux defined, so the quux instance should not be salvaged
81  $this->setExpectedException( 'ConfigException' );
82  $newFactory->makeConfig( 'quux' );
83  }
84 
88  public function testGetConfigNames() {
89  $factory = new ConfigFactory();
90  $factory->register( 'foo', 'GlobalVarConfig::newInstance' );
91  $factory->register( 'bar', new HashConfig() );
92 
93  $this->assertEquals( [ 'foo', 'bar' ], $factory->getConfigNames() );
94  }
95 
99  public function testMakeConfigWithCallback() {
100  $factory = new ConfigFactory();
101  $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
102 
103  $conf = $factory->makeConfig( 'unittest' );
104  $this->assertInstanceOf( 'Config', $conf );
105  $this->assertSame( $conf, $factory->makeConfig( 'unittest' ) );
106  }
107 
111  public function testMakeConfigWithObject() {
112  $factory = new ConfigFactory();
113  $conf = new HashConfig();
114  $factory->register( 'test', $conf );
115  $this->assertSame( $conf, $factory->makeConfig( 'test' ) );
116  }
117 
121  public function testMakeConfigFallback() {
122  $factory = new ConfigFactory();
123  $factory->register( '*', 'GlobalVarConfig::newInstance' );
124  $conf = $factory->makeConfig( 'unittest' );
125  $this->assertInstanceOf( 'Config', $conf );
126  }
127 
131  public function testMakeConfigWithNoBuilders() {
132  $factory = new ConfigFactory();
133  $this->setExpectedException( 'ConfigException' );
134  $factory->makeConfig( 'nobuilderregistered' );
135  }
136 
141  $factory = new ConfigFactory();
142  $factory->register( 'unittest', function () {
143  return true; // Not a Config object
144  } );
145  $this->setExpectedException( 'UnexpectedValueException' );
146  $factory->makeConfig( 'unittest' );
147  }
148 
152  public function testGetDefaultInstance() {
153  // NOTE: the global config factory returned here has been overwritten
154  // for operation in test mode. It may not reflect LocalSettings.
155  $factory = MediaWikiServices::getInstance()->getConfigFactory();
156  $this->assertInstanceOf( 'Config', $factory->makeConfig( 'main' ) );
157  }
158 
159 }
ConfigFactoryTest\testRegisterInstance
testRegisterInstance()
ConfigFactory::register.
Definition: ConfigFactoryTest.php:28
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ConfigFactoryTest\testMakeConfigWithObject
testMakeConfigWithObject()
ConfigFactory::makeConfig.
Definition: ConfigFactoryTest.php:111
php
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:35
ConfigFactoryTest\testRegister
testRegister()
ConfigFactory::register.
Definition: ConfigFactoryTest.php:10
ConfigFactoryTest\testSalvage
testSalvage()
ConfigFactory::salvage.
Definition: ConfigFactoryTest.php:52
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
ConfigFactoryTest\testMakeConfigFallback
testMakeConfigFallback()
ConfigFactory::makeConfig.
Definition: ConfigFactoryTest.php:121
ConfigFactoryTest\testRegisterAgain
testRegisterAgain()
ConfigFactory::register.
Definition: ConfigFactoryTest.php:38
ConfigFactoryTest
Definition: ConfigFactoryTest.php:5
ConfigFactoryTest\testGetDefaultInstance
testGetDefaultInstance()
ConfigFactory::getDefaultInstance.
Definition: ConfigFactoryTest.php:152
ConfigFactoryTest\testRegisterInvalid
testRegisterInvalid()
ConfigFactory::register.
Definition: ConfigFactoryTest.php:19
ConfigFactoryTest\testMakeConfigWithInvalidCallback
testMakeConfigWithInvalidCallback()
ConfigFactory::makeConfig.
Definition: ConfigFactoryTest.php:140
ConfigFactoryTest\testMakeConfigWithCallback
testMakeConfigWithCallback()
ConfigFactory::makeConfig.
Definition: ConfigFactoryTest.php:99
GlobalVarConfig\newInstance
static newInstance()
Default builder function.
Definition: GlobalVarConfig.php:40
ConfigFactory
Factory class to create Config objects.
Definition: ConfigFactory.php:31
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWikiServices
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 MediaWikiServices
Definition: injection.txt:23
ConfigFactoryTest\testMakeConfigWithNoBuilders
testMakeConfigWithNoBuilders()
ConfigFactory::makeConfig.
Definition: ConfigFactoryTest.php:131
ConfigFactoryTest\testGetConfigNames
testGetConfigNames()
ConfigFactory::getConfigNames.
Definition: ConfigFactoryTest.php:88