3 use MediaWiki\MediaWikiServices
;
5 class ConfigFactoryTest
extends \MediaWikiUnitTestCase
{
8 * @covers ConfigFactory::register
10 public function testRegister() {
11 $factory = new ConfigFactory();
12 $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
13 $this->assertInstanceOf( GlobalVarConfig
::class, $factory->makeConfig( 'unittest' ) );
17 * @covers ConfigFactory::register
19 public function testRegisterInvalid() {
20 $factory = new ConfigFactory();
21 $this->setExpectedException( InvalidArgumentException
::class );
22 $factory->register( 'invalid', 'Invalid callback' );
26 * @covers ConfigFactory::register
28 public function testRegisterInvalidInstance() {
29 $factory = new ConfigFactory();
30 $this->setExpectedException( InvalidArgumentException
::class );
31 $factory->register( 'invalidInstance', new stdClass
);
35 * @covers ConfigFactory::register
37 public function testRegisterInstance() {
38 $config = GlobalVarConfig
::newInstance();
39 $factory = new ConfigFactory();
40 $factory->register( 'unittest', $config );
41 $this->assertSame( $config, $factory->makeConfig( 'unittest' ) );
45 * @covers ConfigFactory::register
47 public function testRegisterAgain() {
48 $factory = new ConfigFactory();
49 $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
50 $config1 = $factory->makeConfig( 'unittest' );
52 $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
53 $config2 = $factory->makeConfig( 'unittest' );
55 $this->assertNotSame( $config1, $config2 );
59 * @covers ConfigFactory::salvage
61 public function testSalvage() {
62 $oldFactory = new ConfigFactory();
63 $oldFactory->register( 'foo', 'GlobalVarConfig::newInstance' );
64 $oldFactory->register( 'bar', 'GlobalVarConfig::newInstance' );
65 $oldFactory->register( 'quux', 'GlobalVarConfig::newInstance' );
67 // instantiate two of the three defined configurations
68 $foo = $oldFactory->makeConfig( 'foo' );
69 $bar = $oldFactory->makeConfig( 'bar' );
70 $quux = $oldFactory->makeConfig( 'quux' );
72 // define new config instance
73 $newFactory = new ConfigFactory();
74 $newFactory->register( 'foo', 'GlobalVarConfig::newInstance' );
75 $newFactory->register( 'bar', function () {
76 return new HashConfig();
79 // "foo" and "quux" are defined in the old and the new factory.
80 // The old factory has instances for "foo" and "bar", but not "quux".
81 $newFactory->salvage( $oldFactory );
83 $newFoo = $newFactory->makeConfig( 'foo' );
84 $this->assertSame( $foo, $newFoo, 'existing instance should be salvaged' );
86 $newBar = $newFactory->makeConfig( 'bar' );
87 $this->assertNotSame( $bar, $newBar, 'don\'t salvage if callbacks differ' );
89 // the new factory doesn't have quux defined, so the quux instance should not be salvaged
90 $this->setExpectedException( ConfigException
::class );
91 $newFactory->makeConfig( 'quux' );
95 * @covers ConfigFactory::getConfigNames
97 public function testGetConfigNames() {
98 $factory = new ConfigFactory();
99 $factory->register( 'foo', 'GlobalVarConfig::newInstance' );
100 $factory->register( 'bar', new HashConfig() );
102 $this->assertEquals( [ 'foo', 'bar' ], $factory->getConfigNames() );
106 * @covers ConfigFactory::makeConfig
108 public function testMakeConfigWithCallback() {
109 $factory = new ConfigFactory();
110 $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
112 $conf = $factory->makeConfig( 'unittest' );
113 $this->assertInstanceOf( Config
::class, $conf );
114 $this->assertSame( $conf, $factory->makeConfig( 'unittest' ) );
118 * @covers ConfigFactory::makeConfig
120 public function testMakeConfigWithObject() {
121 $factory = new ConfigFactory();
122 $conf = new HashConfig();
123 $factory->register( 'test', $conf );
124 $this->assertSame( $conf, $factory->makeConfig( 'test' ) );
128 * @covers ConfigFactory::makeConfig
130 public function testMakeConfigFallback() {
131 $factory = new ConfigFactory();
132 $factory->register( '*', 'GlobalVarConfig::newInstance' );
133 $conf = $factory->makeConfig( 'unittest' );
134 $this->assertInstanceOf( Config
::class, $conf );
138 * @covers ConfigFactory::makeConfig
140 public function testMakeConfigWithNoBuilders() {
141 $factory = new ConfigFactory();
142 $this->setExpectedException( ConfigException
::class );
143 $factory->makeConfig( 'nobuilderregistered' );
147 * @covers ConfigFactory::makeConfig
149 public function testMakeConfigWithInvalidCallback() {
150 $factory = new ConfigFactory();
151 $factory->register( 'unittest', function () {
152 return true; // Not a Config object
154 $this->setExpectedException( UnexpectedValueException
::class );
155 $factory->makeConfig( 'unittest' );
159 * @covers ConfigFactory::getDefaultInstance
161 public function testGetDefaultInstance() {
162 // NOTE: the global config factory returned here has been overwritten
163 // for operation in test mode. It may not reflect LocalSettings.
164 $factory = MediaWikiServices
::getInstance()->getConfigFactory();
165 $this->assertInstanceOf( Config
::class, $factory->makeConfig( 'main' ) );