| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Config\Config; |
| 4 |
use MediaWiki\Config\ConfigException; |
| 5 |
use MediaWiki\Config\ConfigFactory; |
| 6 |
use MediaWiki\Config\GlobalVarConfig; |
| 7 |
use MediaWiki\Config\HashConfig; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class ConfigFactoryTest extends \MediaWikiIntegrationTestCase { |
| 13 |
|
| 14 |
public function testRegister() { |
| 15 |
$factory = new ConfigFactory(); |
| 16 |
$factory->register( 'unittest', 'GlobalVarConfig::newInstance' ); |
| 17 |
$this->assertInstanceOf( GlobalVarConfig::class, $factory->makeConfig( 'unittest' ) ); |
| 18 |
} |
| 19 |
|
| 20 |
public function testRegisterInvalid() { |
| 21 |
$factory = new ConfigFactory(); |
| 22 |
$this->expectException( InvalidArgumentException::class ); |
| 23 |
$factory->register( 'invalid', 'Invalid callback' ); |
| 24 |
} |
| 25 |
|
| 26 |
public function testRegisterInvalidInstance() { |
| 27 |
$factory = new ConfigFactory(); |
| 28 |
$this->expectException( InvalidArgumentException::class ); |
| 29 |
$factory->register( 'invalidInstance', (object)[] ); |
| 30 |
} |
| 31 |
|
| 32 |
public function testRegisterInstance() { |
| 33 |
$config = GlobalVarConfig::newInstance(); |
| 34 |
$factory = new ConfigFactory(); |
| 35 |
$factory->register( 'unittest', $config ); |
| 36 |
$this->assertSame( $config, $factory->makeConfig( 'unittest' ) ); |
| 37 |
} |
| 38 |
|
| 39 |
public function testRegisterAgain() { |
| 40 |
$factory = new ConfigFactory(); |
| 41 |
$factory->register( 'unittest', 'GlobalVarConfig::newInstance' ); |
| 42 |
$config1 = $factory->makeConfig( 'unittest' ); |
| 43 |
|
| 44 |
$factory->register( 'unittest', 'GlobalVarConfig::newInstance' ); |
| 45 |
$config2 = $factory->makeConfig( 'unittest' ); |
| 46 |
|
| 47 |
$this->assertNotSame( $config1, $config2 ); |
| 48 |
} |
| 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 |
|
| 57 |
$foo = $oldFactory->makeConfig( 'foo' ); |
| 58 |
$bar = $oldFactory->makeConfig( 'bar' ); |
| 59 |
$quux = $oldFactory->makeConfig( 'quux' ); |
| 60 |
|
| 61 |
|
| 62 |
$newFactory = new ConfigFactory(); |
| 63 |
$newFactory->register( 'foo', 'GlobalVarConfig::newInstance' ); |
| 64 |
$newFactory->register( 'bar', static function () { |
| 65 |
return new HashConfig(); |
| 66 |
} ); |
| 67 |
|
| 68 |
|
| 69 |
|
| 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 |
|
| 79 |
$this->expectException( ConfigException::class ); |
| 80 |
$newFactory->makeConfig( 'quux' ); |
| 81 |
} |
| 82 |
|
| 83 |
public function testGetConfigNames() { |
| 84 |
$factory = new ConfigFactory(); |
| 85 |
$factory->register( 'foo', 'GlobalVarConfig::newInstance' ); |
| 86 |
$factory->register( 'bar', new HashConfig() ); |
| 87 |
|
| 88 |
$this->assertEquals( [ 'foo', 'bar' ], $factory->getConfigNames() ); |
| 89 |
} |
| 90 |
|
| 91 |
public function testMakeConfigWithCallback() { |
| 92 |
$factory = new ConfigFactory(); |
| 93 |
$factory->register( 'unittest', 'GlobalVarConfig::newInstance' ); |
| 94 |
|
| 95 |
$conf = $factory->makeConfig( 'unittest' ); |
| 96 |
$this->assertInstanceOf( Config::class, $conf ); |
| 97 |
$this->assertSame( $conf, $factory->makeConfig( 'unittest' ) ); |
| 98 |
} |
| 99 |
|
| 100 |
public function testMakeConfigWithObject() { |
| 101 |
$factory = new ConfigFactory(); |
| 102 |
$conf = new HashConfig(); |
| 103 |
$factory->register( 'test', $conf ); |
| 104 |
$this->assertSame( $conf, $factory->makeConfig( 'test' ) ); |
| 105 |
} |
| 106 |
|
| 107 |
public function testMakeConfigFallback() { |
| 108 |
$factory = new ConfigFactory(); |
| 109 |
$factory->register( '*', 'GlobalVarConfig::newInstance' ); |
| 110 |
$conf = $factory->makeConfig( 'unittest' ); |
| 111 |
$this->assertInstanceOf( Config::class, $conf ); |
| 112 |
} |
| 113 |
|
| 114 |
public function testMakeConfigWithNoBuilders() { |
| 115 |
$factory = new ConfigFactory(); |
| 116 |
$this->expectException( ConfigException::class ); |
| 117 |
$factory->makeConfig( 'nobuilderregistered' ); |
| 118 |
} |
| 119 |
|
| 120 |
public function testMakeConfigWithInvalidCallback() { |
| 121 |
$factory = new ConfigFactory(); |
| 122 |
$factory->register( 'unittest', static function () { |
| 123 |
return true; |
| 124 |
} ); |
| 125 |
$this->expectException( UnexpectedValueException::class ); |
| 126 |
$factory->makeConfig( 'unittest' ); |
| 127 |
} |
| 128 |
|
| 129 |
public function testGetDefaultInstance() { |
| 130 |
|
| 131 |
|
| 132 |
$factory = $this->getServiceContainer()->getConfigFactory(); |
| 133 |
$this->assertInstanceOf( Config::class, $factory->makeConfig( 'main' ) ); |
| 134 |
} |
| 135 |
|
| 136 |
} |