Merge "Move MimeAnalyzer params to ServiceWiring"
[lhc/web/wiklou.git] / tests / phpunit / includes / config / ConfigFactoryTest.php
1 <?php
2
3 class ConfigFactoryTest extends MediaWikiTestCase {
4
5 /**
6 * @covers ConfigFactory::register
7 */
8 public function testRegister() {
9 $factory = new ConfigFactory();
10 $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
11 $this->assertInstanceOf( GlobalVarConfig::class, $factory->makeConfig( 'unittest' ) );
12 }
13
14 /**
15 * @covers ConfigFactory::register
16 */
17 public function testRegisterInvalid() {
18 $factory = new ConfigFactory();
19 $this->setExpectedException( 'InvalidArgumentException' );
20 $factory->register( 'invalid', 'Invalid callback' );
21 }
22
23 /**
24 * @covers ConfigFactory::register
25 */
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
33 /**
34 * @covers ConfigFactory::register
35 */
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
47 /**
48 * @covers ConfigFactory::register
49 */
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
83 /**
84 * @covers ConfigFactory::register
85 */
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
94 /**
95 * @covers ConfigFactory::makeConfig
96 */
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
106 /**
107 * @covers ConfigFactory::makeConfig
108 */
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
116 /**
117 * @covers ConfigFactory::makeConfig
118 */
119 public function testMakeConfigWithNoBuilders() {
120 $factory = new ConfigFactory();
121 $this->setExpectedException( 'ConfigException' );
122 $factory->makeConfig( 'nobuilderregistered' );
123 }
124
125 /**
126 * @covers ConfigFactory::makeConfig
127 */
128 public function testMakeConfigWithInvalidCallback() {
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
137 /**
138 * @covers ConfigFactory::getDefaultInstance
139 */
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.
143 $factory = ConfigFactory::getDefaultInstance();
144 $this->assertInstanceOf( 'Config', $factory->makeConfig( 'main' ) );
145 }
146
147 }