From dc604f78a3d39c0957fca9f8e494cbdcab169d8d Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Sun, 3 Aug 2014 14:05:50 -0700 Subject: [PATCH] Add tests for ConfigFactory::getDefaultInstance Change-Id: I6d356458dfe0a66f9af12ab58a6b1dcf47b6325a --- includes/config/ConfigFactory.php | 27 +++++++++++++++---- .../includes/config/ConfigFactoryTest.php | 24 +++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/includes/config/ConfigFactory.php b/includes/config/ConfigFactory.php index ff2f403628..312d4616ac 100644 --- a/includes/config/ConfigFactory.php +++ b/includes/config/ConfigFactory.php @@ -41,16 +41,33 @@ class ConfigFactory { */ protected $configs = array(); + /** + * @var ConfigFactory + */ + private static $self; + public static function getDefaultInstance() { - static $self = null; - if ( !$self ) { - $self = new self; + if ( !self::$self ) { + self::$self = new self; global $wgConfigRegistry; foreach ( $wgConfigRegistry as $name => $callback ) { - $self->register( $name, $callback ); + self::$self->register( $name, $callback ); } } - return $self; + return self::$self; + } + + /** + * Destroy the default instance + * Should only be called inside unit tests + * @throws MWException + */ + public static function destroyDefaultInstance() { + if ( !defined( 'MW_PHPUNIT_TEST' ) ) { + throw new MWException( __METHOD__ . ' was called outside of unit tests' ); + } + + self::$self = null; } /** diff --git a/tests/phpunit/includes/config/ConfigFactoryTest.php b/tests/phpunit/includes/config/ConfigFactoryTest.php index 611d304859..3902858d61 100644 --- a/tests/phpunit/includes/config/ConfigFactoryTest.php +++ b/tests/phpunit/includes/config/ConfigFactoryTest.php @@ -2,6 +2,12 @@ class ConfigFactoryTest extends MediaWikiTestCase { + public function tearDown() { + // Reset this since we mess with it a bit + ConfigFactory::destroyDefaultInstance(); + parent::tearDown(); + } + /** * @covers ConfigFactory::register */ @@ -43,4 +49,22 @@ class ConfigFactoryTest extends MediaWikiTestCase { $this->setExpectedException( 'UnexpectedValueException' ); $factory->makeConfig( 'unittest' ); } + + /** + * @covers ConfigFactory::getDefaultInstance + */ + public function testGetDefaultInstance() { + // Set $wgConfigRegistry, and check the default + // instance read from it + $this->setMwGlobals( 'wgConfigRegistry', array( + 'conf1' => 'GlobalVarConfig::newInstance', + 'conf2' => 'GlobalVarConfig::newInstance', + ) ); + ConfigFactory::destroyDefaultInstance(); + $factory = ConfigFactory::getDefaultInstance(); + $this->assertInstanceOf( 'Config', $factory->makeConfig( 'conf1' ) ); + $this->assertInstanceOf( 'Config', $factory->makeConfig( 'conf2' ) ); + $this->setExpectedException( 'ConfigException' ); + $factory->makeConfig( 'conf3' ); + } } -- 2.20.1