wiki.techinc.nl/tests/phpunit/includes/config/ConfigFactoryTest.php
daniel d7410db0fd Allow reset of global services (redux).
(This is part of I6ec374ac9 wich was a re-submit of Ie98bf5af5
which got reverted by Ide7ab563)

This change provides a mechanism to reset global service instances
in an orderly manner. There are three use cases for this:

* the installation process
* integration tests (which most of the existing phpunit tests are)

In contrast to I6ec374ac9, this change does not cause singeltons
of legacy services to be reset. It is assumed that legacy services
use global state to access services and configuration, so any
change in confuguration would affect them immediately.

NOTE: the original I6ec374ac9 would cause session information to
get lost if the user session was creatsed before initialization
was complete. This was apparently triggered by the MobileFrontend
extension under some circumstances. Check with Addshore and Catrope.

Change-Id: Ie06782ffb96e675c0aa55dc26fb8f22037e8517d
2016-05-04 19:18:29 +02:00

111 lines
3.1 KiB
PHP

<?php
class ConfigFactoryTest extends MediaWikiTestCase {
/**
* @covers ConfigFactory::register
*/
public function testRegister() {
$factory = new ConfigFactory();
$factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
$this->assertInstanceOf( GlobalVarConfig::class, $factory->makeConfig( 'unittest' ) );
}
/**
* @covers ConfigFactory::register
*/
public function testRegisterInvalid() {
$factory = new ConfigFactory();
$this->setExpectedException( 'InvalidArgumentException' );
$factory->register( 'invalid', 'Invalid callback' );
}
/**
* @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 );
}
/**
* @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() );
}
/**
* @covers ConfigFactory::makeConfig
*/
public function testMakeConfig() {
$factory = new ConfigFactory();
$factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
$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' );
$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();
$factory->register( 'unittest', function () {
return true; // Not a Config object
} );
$this->setExpectedException( 'UnexpectedValueException' );
$factory->makeConfig( 'unittest' );
}
/**
* @covers ConfigFactory::getDefaultInstance
*/
public function testGetDefaultInstance() {
// NOTE: the global config factory returned here has been overwritten
// for operation in test mode. It may not reflect LocalSettings.
$factory = ConfigFactory::getDefaultInstance();
$this->assertInstanceOf( 'Config', $factory->makeConfig( 'main' ) );
}
}