Merge "User: Avoid deprecated Linker::link()"
[lhc/web/wiklou.git] / tests / phpunit / includes / config / ConfigFactoryTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 class ConfigFactoryTest extends MediaWikiTestCase {
6
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' ) );
14 }
15
16 /**
17 * @covers ConfigFactory::register
18 */
19 public function testRegisterInvalid() {
20 $factory = new ConfigFactory();
21 $this->setExpectedException( 'InvalidArgumentException' );
22 $factory->register( 'invalid', 'Invalid callback' );
23 }
24
25 /**
26 * @covers ConfigFactory::register
27 */
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
35 /**
36 * @covers ConfigFactory::register
37 */
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
49 /**
50 * @covers ConfigFactory::register
51 */
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
85 /**
86 * @covers ConfigFactory::register
87 */
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
96 /**
97 * @covers ConfigFactory::makeConfig
98 */
99 public function testMakeConfig() {
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
108 /**
109 * @covers ConfigFactory::makeConfig
110 */
111 public function testMakeConfigFallback() {
112 $factory = new ConfigFactory();
113 $factory->register( '*', 'GlobalVarConfig::newInstance' );
114 $conf = $factory->makeConfig( 'unittest' );
115 $this->assertInstanceOf( 'Config', $conf );
116 }
117
118 /**
119 * @covers ConfigFactory::makeConfig
120 */
121 public function testMakeConfigWithNoBuilders() {
122 $factory = new ConfigFactory();
123 $this->setExpectedException( 'ConfigException' );
124 $factory->makeConfig( 'nobuilderregistered' );
125 }
126
127 /**
128 * @covers ConfigFactory::makeConfig
129 */
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' );
137 }
138
139 /**
140 * @covers ConfigFactory::getDefaultInstance
141 */
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' ) );
147 }
148
149 }