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
;
10 * @covers \MediaWiki\Config\ConfigFactory
12 class ConfigFactoryTest
extends \MediaWikiIntegrationTestCase
{
14 public function testRegister() {
15 $factory = new ConfigFactory();
16 $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
17 $this->assertInstanceOf( GlobalVarConfig
::class, $factory->makeConfig( 'unittest' ) );
20 public function testRegisterInvalid() {
21 $factory = new ConfigFactory();
22 $this->expectException( InvalidArgumentException
::class );
23 $factory->register( 'invalid', 'Invalid callback' );
26 public function testRegisterInvalidInstance() {
27 $factory = new ConfigFactory();
28 $this->expectException( InvalidArgumentException
::class );
29 $factory->register( 'invalidInstance', (object)[] );
32 public function testRegisterInstance() {
33 $config = GlobalVarConfig
::newInstance();
34 $factory = new ConfigFactory();
35 $factory->register( 'unittest', $config );
36 $this->assertSame( $config, $factory->makeConfig( 'unittest' ) );
39 public function testRegisterAgain() {
40 $factory = new ConfigFactory();
41 $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
42 $config1 = $factory->makeConfig( 'unittest' );
44 $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
45 $config2 = $factory->makeConfig( 'unittest' );
47 $this->assertNotSame( $config1, $config2 );
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' );
56 // instantiate two of the three defined configurations
57 $foo = $oldFactory->makeConfig( 'foo' );
58 $bar = $oldFactory->makeConfig( 'bar' );
59 $quux = $oldFactory->makeConfig( 'quux' );
61 // define new config instance
62 $newFactory = new ConfigFactory();
63 $newFactory->register( 'foo', 'GlobalVarConfig::newInstance' );
64 $newFactory->register( 'bar', static function () {
65 return new HashConfig();
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 );
72 $newFoo = $newFactory->makeConfig( 'foo' );
73 $this->assertSame( $foo, $newFoo, 'existing instance should be salvaged' );
75 $newBar = $newFactory->makeConfig( 'bar' );
76 $this->assertNotSame( $bar, $newBar, 'don\'t salvage if callbacks differ' );
78 // the new factory doesn't have quux defined, so the quux instance should not be salvaged
79 $this->expectException( ConfigException
::class );
80 $newFactory->makeConfig( 'quux' );
83 public function testGetConfigNames() {
84 $factory = new ConfigFactory();
85 $factory->register( 'foo', 'GlobalVarConfig::newInstance' );
86 $factory->register( 'bar', new HashConfig() );
88 $this->assertEquals( [ 'foo', 'bar' ], $factory->getConfigNames() );
91 public function testMakeConfigWithCallback() {
92 $factory = new ConfigFactory();
93 $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
95 $conf = $factory->makeConfig( 'unittest' );
96 $this->assertInstanceOf( Config
::class, $conf );
97 $this->assertSame( $conf, $factory->makeConfig( 'unittest' ) );
100 public function testMakeConfigWithObject() {
101 $factory = new ConfigFactory();
102 $conf = new HashConfig();
103 $factory->register( 'test', $conf );
104 $this->assertSame( $conf, $factory->makeConfig( 'test' ) );
107 public function testMakeConfigFallback() {
108 $factory = new ConfigFactory();
109 $factory->register( '*', 'GlobalVarConfig::newInstance' );
110 $conf = $factory->makeConfig( 'unittest' );
111 $this->assertInstanceOf( Config
::class, $conf );
114 public function testMakeConfigWithNoBuilders() {
115 $factory = new ConfigFactory();
116 $this->expectException( ConfigException
::class );
117 $factory->makeConfig( 'nobuilderregistered' );
120 public function testMakeConfigWithInvalidCallback() {
121 $factory = new ConfigFactory();
122 $factory->register( 'unittest', static function () {
123 return true; // Not a Config object
125 $this->expectException( UnexpectedValueException
::class );
126 $factory->makeConfig( 'unittest' );
129 public function testGetDefaultInstance() {
130 // NOTE: the global config factory returned here has been overwritten
131 // for operation in test mode. It may not reflect LocalSettings.
132 $factory = $this->getServiceContainer()->getConfigFactory();
133 $this->assertInstanceOf( Config
::class, $factory->makeConfig( 'main' ) );