Make abstract Config class truly implementation-agnostic
Follow up to I13baec0b6 ("Config: Add Config and GlobalConfig classes"):
Config:
* Rather than returning Status objects, Config::set will now throw an exception
if an error is encountered
* Config::factory was moved into it's own ConfigFactory class.
* Since there are no more functions in it, Config was turned into an interface.
GlobalConfig:
* Remove $prefix args from Config::set and ::get. The idea of having an
abstract Config class is to abstract some notion of configuration data from
the particular way in which it is currently implemented (global variables).
So the abstract base class has no business dealing with variable name
prefixes.
** Instead GlobalVarConfig's implementations of get and set call getWithPrefix
and setWithPrefix internally, which are now protected
* Rename GlobalConfig to GlobalVarConfig, which makes it clearer that it isn't
referring to the scope of the configuration value, but to the scope of the
variable name which provides it.
ConfigFactory:
* ConfigFactory is where Config objects are registered, and later constructed.
* Config objects are registered with a given name, and a callback factory function.
This allows for implementations to construct the object with the parameters they want,
and avoids the overhead of needing an entire class.
** The name 'main' is the default object returned by RequestContext::getConfig(),
and is intended to be used by core.
* This is a singleton class, the main instance can be obtained with:
ConfigFactory::getDefaultInstance()
In addition to the above:
* $wgConfigClass was removed, and $wgConfigRegistry was introduced, which
stores a name => callback. The name is to be what the Config instance is
registered with, and the callback should return an implementation of Config.
* Tests were written for the new ConfigFactory, and GlobalVarConfig's tests
were improved.
Co-Authored-By: Ori Livneh <ori@wikimedia.org>
Co-Authored-By: Chad Horohoe <chadh@wikimedia.org>
Co-Authored-By: Mattflaschen <mflaschen@wikimedia.org>
Co-Authored-By: Parent5446 <tylerromeo@gmail.com>
Co-Authored-By: Reedy <reedy@wikimedia.org>
Co-Authored-By: Daniel Kinzler <daniel.kinzler@wikimedia.de>
Change-Id: I5a5857fcfa07598ba4ce9ae5bbb4ce54a567d31e
2014-05-10 08:19:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class ConfigFactoryTest extends MediaWikiTestCase {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ConfigFactory::register
|
|
|
|
|
*/
|
|
|
|
|
public function testRegister() {
|
|
|
|
|
$factory = new ConfigFactory();
|
|
|
|
|
$factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
|
2016-05-01 19:29:11 +00:00
|
|
|
$this->assertInstanceOf( GlobalVarConfig::class, $factory->makeConfig( 'unittest' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ConfigFactory::register
|
|
|
|
|
*/
|
|
|
|
|
public function testRegisterInvalid() {
|
|
|
|
|
$factory = new ConfigFactory();
|
Make abstract Config class truly implementation-agnostic
Follow up to I13baec0b6 ("Config: Add Config and GlobalConfig classes"):
Config:
* Rather than returning Status objects, Config::set will now throw an exception
if an error is encountered
* Config::factory was moved into it's own ConfigFactory class.
* Since there are no more functions in it, Config was turned into an interface.
GlobalConfig:
* Remove $prefix args from Config::set and ::get. The idea of having an
abstract Config class is to abstract some notion of configuration data from
the particular way in which it is currently implemented (global variables).
So the abstract base class has no business dealing with variable name
prefixes.
** Instead GlobalVarConfig's implementations of get and set call getWithPrefix
and setWithPrefix internally, which are now protected
* Rename GlobalConfig to GlobalVarConfig, which makes it clearer that it isn't
referring to the scope of the configuration value, but to the scope of the
variable name which provides it.
ConfigFactory:
* ConfigFactory is where Config objects are registered, and later constructed.
* Config objects are registered with a given name, and a callback factory function.
This allows for implementations to construct the object with the parameters they want,
and avoids the overhead of needing an entire class.
** The name 'main' is the default object returned by RequestContext::getConfig(),
and is intended to be used by core.
* This is a singleton class, the main instance can be obtained with:
ConfigFactory::getDefaultInstance()
In addition to the above:
* $wgConfigClass was removed, and $wgConfigRegistry was introduced, which
stores a name => callback. The name is to be what the Config instance is
registered with, and the callback should return an implementation of Config.
* Tests were written for the new ConfigFactory, and GlobalVarConfig's tests
were improved.
Co-Authored-By: Ori Livneh <ori@wikimedia.org>
Co-Authored-By: Chad Horohoe <chadh@wikimedia.org>
Co-Authored-By: Mattflaschen <mflaschen@wikimedia.org>
Co-Authored-By: Parent5446 <tylerromeo@gmail.com>
Co-Authored-By: Reedy <reedy@wikimedia.org>
Co-Authored-By: Daniel Kinzler <daniel.kinzler@wikimedia.de>
Change-Id: I5a5857fcfa07598ba4ce9ae5bbb4ce54a567d31e
2014-05-10 08:19:00 +00:00
|
|
|
$this->setExpectedException( 'InvalidArgumentException' );
|
|
|
|
|
$factory->register( 'invalid', 'Invalid callback' );
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-01 19:29:11 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ConfigFactory::register
|
|
|
|
|
*/
|
|
|
|
|
public function testRegisterInstance() {
|
|
|
|
|
$config = GlobalVarConfig::newInstance();
|
|
|
|
|
$factory = new ConfigFactory();
|
|
|
|
|
$factory->register( 'unittest', $config );
|
|
|
|
|
$this->assertSame( $config, $factory->makeConfig( 'unittest' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ConfigFactory::register
|
|
|
|
|
*/
|
|
|
|
|
public function testRegisterAgain() {
|
|
|
|
|
$factory = new ConfigFactory();
|
|
|
|
|
$factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
|
|
|
|
|
$config1 = $factory->makeConfig( 'unittest' );
|
|
|
|
|
|
|
|
|
|
$factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
|
|
|
|
|
$config2 = $factory->makeConfig( 'unittest' );
|
|
|
|
|
|
|
|
|
|
$this->assertNotSame( $config1, $config2 );
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 19:02:31 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ConfigFactory::register
|
|
|
|
|
*/
|
|
|
|
|
public function testSalvage() {
|
|
|
|
|
$oldFactory = new ConfigFactory();
|
|
|
|
|
$oldFactory->register( 'foo', 'GlobalVarConfig::newInstance' );
|
|
|
|
|
$oldFactory->register( 'bar', 'GlobalVarConfig::newInstance' );
|
|
|
|
|
$oldFactory->register( 'quux', 'GlobalVarConfig::newInstance' );
|
|
|
|
|
|
|
|
|
|
// instantiate two of the three defined configurations
|
|
|
|
|
$foo = $oldFactory->makeConfig( 'foo' );
|
|
|
|
|
$bar = $oldFactory->makeConfig( 'bar' );
|
|
|
|
|
$quux = $oldFactory->makeConfig( 'quux' );
|
|
|
|
|
|
|
|
|
|
// define new config instance
|
|
|
|
|
$newFactory = new ConfigFactory();
|
|
|
|
|
$newFactory->register( 'foo', 'GlobalVarConfig::newInstance' );
|
|
|
|
|
$newFactory->register( 'bar', function() {
|
|
|
|
|
return new HashConfig();
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
// "foo" and "quux" are defined in the old and the new factory.
|
|
|
|
|
// The old factory has instances for "foo" and "bar", but not "quux".
|
|
|
|
|
$newFactory->salvage( $oldFactory );
|
|
|
|
|
|
|
|
|
|
$newFoo = $newFactory->makeConfig( 'foo' );
|
|
|
|
|
$this->assertSame( $foo, $newFoo, 'existing instance should be salvaged' );
|
|
|
|
|
|
|
|
|
|
$newBar = $newFactory->makeConfig( 'bar' );
|
|
|
|
|
$this->assertNotSame( $bar, $newBar, 'don\'t salvage if callbacks differ' );
|
|
|
|
|
|
|
|
|
|
// the new factory doesn't have quux defined, so the quux instance should not be salvaged
|
|
|
|
|
$this->setExpectedException( 'ConfigException' );
|
|
|
|
|
$newFactory->makeConfig( 'quux' );
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-01 19:29:11 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ConfigFactory::register
|
|
|
|
|
*/
|
|
|
|
|
public function testGetConfigNames() {
|
|
|
|
|
$factory = new ConfigFactory();
|
|
|
|
|
$factory->register( 'foo', 'GlobalVarConfig::newInstance' );
|
|
|
|
|
$factory->register( 'bar', new HashConfig() );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( [ 'foo', 'bar' ], $factory->getConfigNames() );
|
|
|
|
|
}
|
|
|
|
|
|
Make abstract Config class truly implementation-agnostic
Follow up to I13baec0b6 ("Config: Add Config and GlobalConfig classes"):
Config:
* Rather than returning Status objects, Config::set will now throw an exception
if an error is encountered
* Config::factory was moved into it's own ConfigFactory class.
* Since there are no more functions in it, Config was turned into an interface.
GlobalConfig:
* Remove $prefix args from Config::set and ::get. The idea of having an
abstract Config class is to abstract some notion of configuration data from
the particular way in which it is currently implemented (global variables).
So the abstract base class has no business dealing with variable name
prefixes.
** Instead GlobalVarConfig's implementations of get and set call getWithPrefix
and setWithPrefix internally, which are now protected
* Rename GlobalConfig to GlobalVarConfig, which makes it clearer that it isn't
referring to the scope of the configuration value, but to the scope of the
variable name which provides it.
ConfigFactory:
* ConfigFactory is where Config objects are registered, and later constructed.
* Config objects are registered with a given name, and a callback factory function.
This allows for implementations to construct the object with the parameters they want,
and avoids the overhead of needing an entire class.
** The name 'main' is the default object returned by RequestContext::getConfig(),
and is intended to be used by core.
* This is a singleton class, the main instance can be obtained with:
ConfigFactory::getDefaultInstance()
In addition to the above:
* $wgConfigClass was removed, and $wgConfigRegistry was introduced, which
stores a name => callback. The name is to be what the Config instance is
registered with, and the callback should return an implementation of Config.
* Tests were written for the new ConfigFactory, and GlobalVarConfig's tests
were improved.
Co-Authored-By: Ori Livneh <ori@wikimedia.org>
Co-Authored-By: Chad Horohoe <chadh@wikimedia.org>
Co-Authored-By: Mattflaschen <mflaschen@wikimedia.org>
Co-Authored-By: Parent5446 <tylerromeo@gmail.com>
Co-Authored-By: Reedy <reedy@wikimedia.org>
Co-Authored-By: Daniel Kinzler <daniel.kinzler@wikimedia.de>
Change-Id: I5a5857fcfa07598ba4ce9ae5bbb4ce54a567d31e
2014-05-10 08:19:00 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ConfigFactory::makeConfig
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeConfig() {
|
|
|
|
|
$factory = new ConfigFactory();
|
|
|
|
|
$factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
|
2016-05-01 19:29:11 +00:00
|
|
|
|
|
|
|
|
$conf = $factory->makeConfig( 'unittest' );
|
|
|
|
|
$this->assertInstanceOf( 'Config', $conf );
|
|
|
|
|
$this->assertSame( $conf, $factory->makeConfig( 'unittest' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ConfigFactory::makeConfig
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeConfigFallback() {
|
|
|
|
|
$factory = new ConfigFactory();
|
|
|
|
|
$factory->register( '*', 'GlobalVarConfig::newInstance' );
|
Make abstract Config class truly implementation-agnostic
Follow up to I13baec0b6 ("Config: Add Config and GlobalConfig classes"):
Config:
* Rather than returning Status objects, Config::set will now throw an exception
if an error is encountered
* Config::factory was moved into it's own ConfigFactory class.
* Since there are no more functions in it, Config was turned into an interface.
GlobalConfig:
* Remove $prefix args from Config::set and ::get. The idea of having an
abstract Config class is to abstract some notion of configuration data from
the particular way in which it is currently implemented (global variables).
So the abstract base class has no business dealing with variable name
prefixes.
** Instead GlobalVarConfig's implementations of get and set call getWithPrefix
and setWithPrefix internally, which are now protected
* Rename GlobalConfig to GlobalVarConfig, which makes it clearer that it isn't
referring to the scope of the configuration value, but to the scope of the
variable name which provides it.
ConfigFactory:
* ConfigFactory is where Config objects are registered, and later constructed.
* Config objects are registered with a given name, and a callback factory function.
This allows for implementations to construct the object with the parameters they want,
and avoids the overhead of needing an entire class.
** The name 'main' is the default object returned by RequestContext::getConfig(),
and is intended to be used by core.
* This is a singleton class, the main instance can be obtained with:
ConfigFactory::getDefaultInstance()
In addition to the above:
* $wgConfigClass was removed, and $wgConfigRegistry was introduced, which
stores a name => callback. The name is to be what the Config instance is
registered with, and the callback should return an implementation of Config.
* Tests were written for the new ConfigFactory, and GlobalVarConfig's tests
were improved.
Co-Authored-By: Ori Livneh <ori@wikimedia.org>
Co-Authored-By: Chad Horohoe <chadh@wikimedia.org>
Co-Authored-By: Mattflaschen <mflaschen@wikimedia.org>
Co-Authored-By: Parent5446 <tylerromeo@gmail.com>
Co-Authored-By: Reedy <reedy@wikimedia.org>
Co-Authored-By: Daniel Kinzler <daniel.kinzler@wikimedia.de>
Change-Id: I5a5857fcfa07598ba4ce9ae5bbb4ce54a567d31e
2014-05-10 08:19:00 +00:00
|
|
|
$conf = $factory->makeConfig( 'unittest' );
|
|
|
|
|
$this->assertInstanceOf( 'Config', $conf );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ConfigFactory::makeConfig
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeConfigWithNoBuilders() {
|
|
|
|
|
$factory = new ConfigFactory();
|
|
|
|
|
$this->setExpectedException( 'ConfigException' );
|
|
|
|
|
$factory->makeConfig( 'nobuilderregistered' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ConfigFactory::makeConfig
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeConfigWithInvalidCallback() {
|
|
|
|
|
$factory = new ConfigFactory();
|
2014-07-20 19:41:41 +00:00
|
|
|
$factory->register( 'unittest', function () {
|
Make abstract Config class truly implementation-agnostic
Follow up to I13baec0b6 ("Config: Add Config and GlobalConfig classes"):
Config:
* Rather than returning Status objects, Config::set will now throw an exception
if an error is encountered
* Config::factory was moved into it's own ConfigFactory class.
* Since there are no more functions in it, Config was turned into an interface.
GlobalConfig:
* Remove $prefix args from Config::set and ::get. The idea of having an
abstract Config class is to abstract some notion of configuration data from
the particular way in which it is currently implemented (global variables).
So the abstract base class has no business dealing with variable name
prefixes.
** Instead GlobalVarConfig's implementations of get and set call getWithPrefix
and setWithPrefix internally, which are now protected
* Rename GlobalConfig to GlobalVarConfig, which makes it clearer that it isn't
referring to the scope of the configuration value, but to the scope of the
variable name which provides it.
ConfigFactory:
* ConfigFactory is where Config objects are registered, and later constructed.
* Config objects are registered with a given name, and a callback factory function.
This allows for implementations to construct the object with the parameters they want,
and avoids the overhead of needing an entire class.
** The name 'main' is the default object returned by RequestContext::getConfig(),
and is intended to be used by core.
* This is a singleton class, the main instance can be obtained with:
ConfigFactory::getDefaultInstance()
In addition to the above:
* $wgConfigClass was removed, and $wgConfigRegistry was introduced, which
stores a name => callback. The name is to be what the Config instance is
registered with, and the callback should return an implementation of Config.
* Tests were written for the new ConfigFactory, and GlobalVarConfig's tests
were improved.
Co-Authored-By: Ori Livneh <ori@wikimedia.org>
Co-Authored-By: Chad Horohoe <chadh@wikimedia.org>
Co-Authored-By: Mattflaschen <mflaschen@wikimedia.org>
Co-Authored-By: Parent5446 <tylerromeo@gmail.com>
Co-Authored-By: Reedy <reedy@wikimedia.org>
Co-Authored-By: Daniel Kinzler <daniel.kinzler@wikimedia.de>
Change-Id: I5a5857fcfa07598ba4ce9ae5bbb4ce54a567d31e
2014-05-10 08:19:00 +00:00
|
|
|
return true; // Not a Config object
|
2014-07-19 21:12:10 +00:00
|
|
|
} );
|
Make abstract Config class truly implementation-agnostic
Follow up to I13baec0b6 ("Config: Add Config and GlobalConfig classes"):
Config:
* Rather than returning Status objects, Config::set will now throw an exception
if an error is encountered
* Config::factory was moved into it's own ConfigFactory class.
* Since there are no more functions in it, Config was turned into an interface.
GlobalConfig:
* Remove $prefix args from Config::set and ::get. The idea of having an
abstract Config class is to abstract some notion of configuration data from
the particular way in which it is currently implemented (global variables).
So the abstract base class has no business dealing with variable name
prefixes.
** Instead GlobalVarConfig's implementations of get and set call getWithPrefix
and setWithPrefix internally, which are now protected
* Rename GlobalConfig to GlobalVarConfig, which makes it clearer that it isn't
referring to the scope of the configuration value, but to the scope of the
variable name which provides it.
ConfigFactory:
* ConfigFactory is where Config objects are registered, and later constructed.
* Config objects are registered with a given name, and a callback factory function.
This allows for implementations to construct the object with the parameters they want,
and avoids the overhead of needing an entire class.
** The name 'main' is the default object returned by RequestContext::getConfig(),
and is intended to be used by core.
* This is a singleton class, the main instance can be obtained with:
ConfigFactory::getDefaultInstance()
In addition to the above:
* $wgConfigClass was removed, and $wgConfigRegistry was introduced, which
stores a name => callback. The name is to be what the Config instance is
registered with, and the callback should return an implementation of Config.
* Tests were written for the new ConfigFactory, and GlobalVarConfig's tests
were improved.
Co-Authored-By: Ori Livneh <ori@wikimedia.org>
Co-Authored-By: Chad Horohoe <chadh@wikimedia.org>
Co-Authored-By: Mattflaschen <mflaschen@wikimedia.org>
Co-Authored-By: Parent5446 <tylerromeo@gmail.com>
Co-Authored-By: Reedy <reedy@wikimedia.org>
Co-Authored-By: Daniel Kinzler <daniel.kinzler@wikimedia.de>
Change-Id: I5a5857fcfa07598ba4ce9ae5bbb4ce54a567d31e
2014-05-10 08:19:00 +00:00
|
|
|
$this->setExpectedException( 'UnexpectedValueException' );
|
|
|
|
|
$factory->makeConfig( 'unittest' );
|
|
|
|
|
}
|
2014-08-03 21:05:50 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ConfigFactory::getDefaultInstance
|
|
|
|
|
*/
|
|
|
|
|
public function testGetDefaultInstance() {
|
2016-05-01 19:29:11 +00:00
|
|
|
// NOTE: the global config factory returned here has been overwritten
|
|
|
|
|
// for operation in test mode. It may not reflect LocalSettings.
|
2014-08-03 21:05:50 +00:00
|
|
|
$factory = ConfigFactory::getDefaultInstance();
|
2015-10-12 08:05:45 +00:00
|
|
|
$this->assertInstanceOf( 'Config', $factory->makeConfig( 'main' ) );
|
2016-04-11 20:37:32 +00:00
|
|
|
}
|
2016-05-01 19:29:11 +00:00
|
|
|
|
Make abstract Config class truly implementation-agnostic
Follow up to I13baec0b6 ("Config: Add Config and GlobalConfig classes"):
Config:
* Rather than returning Status objects, Config::set will now throw an exception
if an error is encountered
* Config::factory was moved into it's own ConfigFactory class.
* Since there are no more functions in it, Config was turned into an interface.
GlobalConfig:
* Remove $prefix args from Config::set and ::get. The idea of having an
abstract Config class is to abstract some notion of configuration data from
the particular way in which it is currently implemented (global variables).
So the abstract base class has no business dealing with variable name
prefixes.
** Instead GlobalVarConfig's implementations of get and set call getWithPrefix
and setWithPrefix internally, which are now protected
* Rename GlobalConfig to GlobalVarConfig, which makes it clearer that it isn't
referring to the scope of the configuration value, but to the scope of the
variable name which provides it.
ConfigFactory:
* ConfigFactory is where Config objects are registered, and later constructed.
* Config objects are registered with a given name, and a callback factory function.
This allows for implementations to construct the object with the parameters they want,
and avoids the overhead of needing an entire class.
** The name 'main' is the default object returned by RequestContext::getConfig(),
and is intended to be used by core.
* This is a singleton class, the main instance can be obtained with:
ConfigFactory::getDefaultInstance()
In addition to the above:
* $wgConfigClass was removed, and $wgConfigRegistry was introduced, which
stores a name => callback. The name is to be what the Config instance is
registered with, and the callback should return an implementation of Config.
* Tests were written for the new ConfigFactory, and GlobalVarConfig's tests
were improved.
Co-Authored-By: Ori Livneh <ori@wikimedia.org>
Co-Authored-By: Chad Horohoe <chadh@wikimedia.org>
Co-Authored-By: Mattflaschen <mflaschen@wikimedia.org>
Co-Authored-By: Parent5446 <tylerromeo@gmail.com>
Co-Authored-By: Reedy <reedy@wikimedia.org>
Co-Authored-By: Daniel Kinzler <daniel.kinzler@wikimedia.de>
Change-Id: I5a5857fcfa07598ba4ce9ae5bbb4ce54a567d31e
2014-05-10 08:19:00 +00:00
|
|
|
}
|