MediaWiki  1.28.0
ConfigFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
4 
8  public function testRegister() {
9  $factory = new ConfigFactory();
10  $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
11  $this->assertInstanceOf( GlobalVarConfig::class, $factory->makeConfig( 'unittest' ) );
12  }
13 
17  public function testRegisterInvalid() {
18  $factory = new ConfigFactory();
19  $this->setExpectedException( 'InvalidArgumentException' );
20  $factory->register( 'invalid', 'Invalid callback' );
21  }
22 
26  public function testRegisterInstance() {
27  $config = GlobalVarConfig::newInstance();
28  $factory = new ConfigFactory();
29  $factory->register( 'unittest', $config );
30  $this->assertSame( $config, $factory->makeConfig( 'unittest' ) );
31  }
32 
36  public function testRegisterAgain() {
37  $factory = new ConfigFactory();
38  $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
39  $config1 = $factory->makeConfig( 'unittest' );
40 
41  $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
42  $config2 = $factory->makeConfig( 'unittest' );
43 
44  $this->assertNotSame( $config1, $config2 );
45  }
46 
50  public function testSalvage() {
51  $oldFactory = new ConfigFactory();
52  $oldFactory->register( 'foo', 'GlobalVarConfig::newInstance' );
53  $oldFactory->register( 'bar', 'GlobalVarConfig::newInstance' );
54  $oldFactory->register( 'quux', 'GlobalVarConfig::newInstance' );
55 
56  // instantiate two of the three defined configurations
57  $foo = $oldFactory->makeConfig( 'foo' );
58  $bar = $oldFactory->makeConfig( 'bar' );
59  $quux = $oldFactory->makeConfig( 'quux' );
60 
61  // define new config instance
62  $newFactory = new ConfigFactory();
63  $newFactory->register( 'foo', 'GlobalVarConfig::newInstance' );
64  $newFactory->register( 'bar', function() {
65  return new HashConfig();
66  } );
67 
68  // "foo" and "quux" are defined in the old and the new factory.
69  // The old factory has instances for "foo" and "bar", but not "quux".
70  $newFactory->salvage( $oldFactory );
71 
72  $newFoo = $newFactory->makeConfig( 'foo' );
73  $this->assertSame( $foo, $newFoo, 'existing instance should be salvaged' );
74 
75  $newBar = $newFactory->makeConfig( 'bar' );
76  $this->assertNotSame( $bar, $newBar, 'don\'t salvage if callbacks differ' );
77 
78  // the new factory doesn't have quux defined, so the quux instance should not be salvaged
79  $this->setExpectedException( 'ConfigException' );
80  $newFactory->makeConfig( 'quux' );
81  }
82 
86  public function testGetConfigNames() {
87  $factory = new ConfigFactory();
88  $factory->register( 'foo', 'GlobalVarConfig::newInstance' );
89  $factory->register( 'bar', new HashConfig() );
90 
91  $this->assertEquals( [ 'foo', 'bar' ], $factory->getConfigNames() );
92  }
93 
97  public function testMakeConfig() {
98  $factory = new ConfigFactory();
99  $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
100 
101  $conf = $factory->makeConfig( 'unittest' );
102  $this->assertInstanceOf( 'Config', $conf );
103  $this->assertSame( $conf, $factory->makeConfig( 'unittest' ) );
104  }
105 
109  public function testMakeConfigFallback() {
110  $factory = new ConfigFactory();
111  $factory->register( '*', 'GlobalVarConfig::newInstance' );
112  $conf = $factory->makeConfig( 'unittest' );
113  $this->assertInstanceOf( 'Config', $conf );
114  }
115 
119  public function testMakeConfigWithNoBuilders() {
120  $factory = new ConfigFactory();
121  $this->setExpectedException( 'ConfigException' );
122  $factory->makeConfig( 'nobuilderregistered' );
123  }
124 
129  $factory = new ConfigFactory();
130  $factory->register( 'unittest', function () {
131  return true; // Not a Config object
132  } );
133  $this->setExpectedException( 'UnexpectedValueException' );
134  $factory->makeConfig( 'unittest' );
135  }
136 
140  public function testGetDefaultInstance() {
141  // NOTE: the global config factory returned here has been overwritten
142  // for operation in test mode. It may not reflect LocalSettings.
144  $this->assertInstanceOf( 'Config', $factory->makeConfig( 'main' ) );
145  }
146 
147 }
testMakeConfigWithNoBuilders()
ConfigFactory::makeConfig.
testGetConfigNames()
ConfigFactory::register.
testMakeConfigFallback()
ConfigFactory::makeConfig.
testGetDefaultInstance()
ConfigFactory::getDefaultInstance.
testMakeConfig()
ConfigFactory::makeConfig.
testRegister()
ConfigFactory::register.
testMakeConfigWithInvalidCallback()
ConfigFactory::makeConfig.
static newInstance()
Default builder function.
testSalvage()
ConfigFactory::register.
static getDefaultInstance()
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
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
testRegisterAgain()
ConfigFactory::register.
testRegisterInstance()
ConfigFactory::register.
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
testRegisterInvalid()
ConfigFactory::register.