Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / config / ConfigFactoryTest.php
blob8a766187c84601e08b740752b00be78ca2baa34e
1 <?php
3 use MediaWiki\MediaWikiServices;
5 class ConfigFactoryTest extends MediaWikiTestCase {
7 /**
8 * @covers ConfigFactory::register
9 */
10 public function testRegister() {
11 $factory = new ConfigFactory();
12 $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
13 $this->assertInstanceOf( GlobalVarConfig::class, $factory->makeConfig( 'unittest' ) );
16 /**
17 * @covers ConfigFactory::register
19 public function testRegisterInvalid() {
20 $factory = new ConfigFactory();
21 $this->setExpectedException( 'InvalidArgumentException' );
22 $factory->register( 'invalid', 'Invalid callback' );
25 /**
26 * @covers ConfigFactory::register
28 public function testRegisterInstance() {
29 $config = GlobalVarConfig::newInstance();
30 $factory = new ConfigFactory();
31 $factory->register( 'unittest', $config );
32 $this->assertSame( $config, $factory->makeConfig( 'unittest' ) );
35 /**
36 * @covers ConfigFactory::register
38 public function testRegisterAgain() {
39 $factory = new ConfigFactory();
40 $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
41 $config1 = $factory->makeConfig( 'unittest' );
43 $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
44 $config2 = $factory->makeConfig( 'unittest' );
46 $this->assertNotSame( $config1, $config2 );
49 /**
50 * @covers ConfigFactory::register
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' );
58 // instantiate two of the three defined configurations
59 $foo = $oldFactory->makeConfig( 'foo' );
60 $bar = $oldFactory->makeConfig( 'bar' );
61 $quux = $oldFactory->makeConfig( 'quux' );
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 } );
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 );
74 $newFoo = $newFactory->makeConfig( 'foo' );
75 $this->assertSame( $foo, $newFoo, 'existing instance should be salvaged' );
77 $newBar = $newFactory->makeConfig( 'bar' );
78 $this->assertNotSame( $bar, $newBar, 'don\'t salvage if callbacks differ' );
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' );
85 /**
86 * @covers ConfigFactory::register
88 public function testGetConfigNames() {
89 $factory = new ConfigFactory();
90 $factory->register( 'foo', 'GlobalVarConfig::newInstance' );
91 $factory->register( 'bar', new HashConfig() );
93 $this->assertEquals( [ 'foo', 'bar' ], $factory->getConfigNames() );
96 /**
97 * @covers ConfigFactory::makeConfig
99 public function testMakeConfig() {
100 $factory = new ConfigFactory();
101 $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
103 $conf = $factory->makeConfig( 'unittest' );
104 $this->assertInstanceOf( 'Config', $conf );
105 $this->assertSame( $conf, $factory->makeConfig( 'unittest' ) );
109 * @covers ConfigFactory::makeConfig
111 public function testMakeConfigFallback() {
112 $factory = new ConfigFactory();
113 $factory->register( '*', 'GlobalVarConfig::newInstance' );
114 $conf = $factory->makeConfig( 'unittest' );
115 $this->assertInstanceOf( 'Config', $conf );
119 * @covers ConfigFactory::makeConfig
121 public function testMakeConfigWithNoBuilders() {
122 $factory = new ConfigFactory();
123 $this->setExpectedException( 'ConfigException' );
124 $factory->makeConfig( 'nobuilderregistered' );
128 * @covers ConfigFactory::makeConfig
130 public function testMakeConfigWithInvalidCallback() {
131 $factory = new ConfigFactory();
132 $factory->register( 'unittest', function () {
133 return true; // Not a Config object
134 } );
135 $this->setExpectedException( 'UnexpectedValueException' );
136 $factory->makeConfig( 'unittest' );
140 * @covers ConfigFactory::getDefaultInstance
142 public function testGetDefaultInstance() {
143 // NOTE: the global config factory returned here has been overwritten
144 // for operation in test mode. It may not reflect LocalSettings.
145 $factory = MediaWikiServices::getInstance()->getConfigFactory();
146 $this->assertInstanceOf( 'Config', $factory->makeConfig( 'main' ) );