summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/config
diff options
context:
space:
mode:
Diffstat (limited to 'tests/phpunit/includes/config')
-rw-r--r--tests/phpunit/includes/config/ConfigFactoryTest.php70
-rw-r--r--tests/phpunit/includes/config/GlobalVarConfigTest.php120
-rw-r--r--tests/phpunit/includes/config/HashConfigTest.php63
-rw-r--r--tests/phpunit/includes/config/MultiConfigTest.php38
4 files changed, 291 insertions, 0 deletions
diff --git a/tests/phpunit/includes/config/ConfigFactoryTest.php b/tests/phpunit/includes/config/ConfigFactoryTest.php
new file mode 100644
index 00000000..3902858d
--- /dev/null
+++ b/tests/phpunit/includes/config/ConfigFactoryTest.php
@@ -0,0 +1,70 @@
+<?php
+
+class ConfigFactoryTest extends MediaWikiTestCase {
+
+ public function tearDown() {
+ // Reset this since we mess with it a bit
+ ConfigFactory::destroyDefaultInstance();
+ parent::tearDown();
+ }
+
+ /**
+ * @covers ConfigFactory::register
+ */
+ public function testRegister() {
+ $factory = new ConfigFactory();
+ $factory->register( 'unittest', 'GlobalVarConfig::newInstance' );
+ $this->assertTrue( true ); // No exception thrown
+ $this->setExpectedException( 'InvalidArgumentException' );
+ $factory->register( 'invalid', 'Invalid callback' );
+ }
+
+ /**
+ * @covers ConfigFactory::makeConfig
+ */
+ public function testMakeConfig() {
+ $factory = new ConfigFactory();
+ $factory->register( 'unittest', '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() {
+ // 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' );
+ }
+}
diff --git a/tests/phpunit/includes/config/GlobalVarConfigTest.php b/tests/phpunit/includes/config/GlobalVarConfigTest.php
new file mode 100644
index 00000000..70b9e684
--- /dev/null
+++ b/tests/phpunit/includes/config/GlobalVarConfigTest.php
@@ -0,0 +1,120 @@
+<?php
+
+class GlobalVarConfigTest extends MediaWikiTestCase {
+
+ /**
+ * @covers GlobalVarConfig::newInstance
+ */
+ public function testNewInstance() {
+ $config = GlobalVarConfig::newInstance();
+ $this->assertInstanceOf( 'GlobalVarConfig', $config );
+ $this->maybeStashGlobal( 'wgBaz' );
+ $GLOBALS['wgBaz'] = 'somevalue';
+ // Check prefix is set to 'wg'
+ $this->assertEquals( 'somevalue', $config->get( 'Baz' ) );
+ }
+
+ /**
+ * @covers GlobalVarConfig::__construct
+ * @dataProvider provideConstructor
+ */
+ public function testConstructor( $prefix ) {
+ $var = $prefix . 'GlobalVarConfigTest';
+ $rand = wfRandomString();
+ $this->maybeStashGlobal( $var );
+ $GLOBALS[$var] = $rand;
+ $config = new GlobalVarConfig( $prefix );
+ $this->assertInstanceOf( 'GlobalVarConfig', $config );
+ $this->assertEquals( $rand, $config->get( 'GlobalVarConfigTest' ) );
+ }
+
+ public static function provideConstructor() {
+ return array(
+ array( 'wg' ),
+ array( 'ef' ),
+ array( 'smw' ),
+ array( 'blahblahblahblah' ),
+ array( '' ),
+ );
+ }
+
+ /**
+ * @covers GlobalVarConfig::has
+ */
+ public function testHas() {
+ $this->maybeStashGlobal( 'wgGlobalVarConfigTestHas' );
+ $GLOBALS['wgGlobalVarConfigTestHas'] = wfRandomString();
+ $this->maybeStashGlobal( 'wgGlobalVarConfigTestNotHas' );
+ $config = new GlobalVarConfig();
+ $this->assertTrue( $config->has( 'GlobalVarConfigTestHas' ) );
+ $this->assertFalse( $config->has( 'GlobalVarConfigTestNotHas' ) );
+ }
+
+ public static function provideGet() {
+ $set = array(
+ 'wgSomething' => 'default1',
+ 'wgFoo' => 'default2',
+ 'efVariable' => 'default3',
+ 'BAR' => 'default4',
+ );
+
+ foreach ( $set as $var => $value ) {
+ $GLOBALS[$var] = $value;
+ }
+
+ return array(
+ array( 'Something', 'wg', 'default1' ),
+ array( 'Foo', 'wg', 'default2' ),
+ array( 'Variable', 'ef', 'default3' ),
+ array( 'BAR', '', 'default4' ),
+ array( 'ThisGlobalWasNotSetAbove', 'wg', false )
+ );
+ }
+
+ /**
+ * @param string $name
+ * @param string $prefix
+ * @param string $expected
+ * @dataProvider provideGet
+ * @covers GlobalVarConfig::get
+ * @covers GlobalVarConfig::getWithPrefix
+ */
+ public function testGet( $name, $prefix, $expected ) {
+ $config = new GlobalVarConfig( $prefix );
+ if ( $expected === false ) {
+ $this->setExpectedException( 'ConfigException', 'GlobalVarConfig::get: undefined option:' );
+ }
+ $this->assertEquals( $config->get( $name ), $expected );
+ }
+
+ public static function provideSet() {
+ return array(
+ array( 'Foo', 'wg', 'wgFoo' ),
+ array( 'SomethingRandom', 'wg', 'wgSomethingRandom' ),
+ array( 'FromAnExtension', 'eg', 'egFromAnExtension' ),
+ array( 'NoPrefixHere', '', 'NoPrefixHere' ),
+ );
+ }
+
+ private function maybeStashGlobal( $var ) {
+ if ( array_key_exists( $var, $GLOBALS ) ) {
+ // Will be reset after this test is over
+ $this->stashMwGlobals( $var );
+ }
+ }
+
+ /**
+ * @dataProvider provideSet
+ * @covers GlobalVarConfig::set
+ * @covers GlobalVarConfig::setWithPrefix
+ */
+ public function testSet( $name, $prefix, $var ) {
+ $this->hideDeprecated( 'GlobalVarConfig::set' );
+ $this->maybeStashGlobal( $var );
+ $config = new GlobalVarConfig( $prefix );
+ $random = wfRandomString();
+ $config->set( $name, $random );
+ $this->assertArrayHasKey( $var, $GLOBALS );
+ $this->assertEquals( $random, $GLOBALS[$var] );
+ }
+}
diff --git a/tests/phpunit/includes/config/HashConfigTest.php b/tests/phpunit/includes/config/HashConfigTest.php
new file mode 100644
index 00000000..3ad3bfbd
--- /dev/null
+++ b/tests/phpunit/includes/config/HashConfigTest.php
@@ -0,0 +1,63 @@
+<?php
+
+class HashConfigTest extends MediaWikiTestCase {
+
+ /**
+ * @covers HashConfig::newInstance
+ */
+ public function testNewInstance() {
+ $conf = HashConfig::newInstance();
+ $this->assertInstanceOf( 'HashConfig', $conf );
+ }
+
+ /**
+ * @covers HashConfig::__construct
+ */
+ public function testConstructor() {
+ $conf = new HashConfig();
+ $this->assertInstanceOf( 'HashConfig', $conf );
+
+ // Test passing arguments to the constructor
+ $conf2 = new HashConfig( array(
+ 'one' => '1',
+ ) );
+ $this->assertEquals( '1', $conf2->get( 'one' ) );
+ }
+
+ /**
+ * @covers HashConfig::get
+ */
+ public function testGet() {
+ $conf = new HashConfig( array(
+ 'one' => '1',
+ ));
+ $this->assertEquals( '1', $conf->get( 'one' ) );
+ $this->setExpectedException( 'ConfigException', 'HashConfig::get: undefined option' );
+ $conf->get( 'two' );
+ }
+
+ /**
+ * @covers HashConfig::has
+ */
+ public function testHas() {
+ $conf = new HashConfig( array(
+ 'one' => '1',
+ ) );
+ $this->assertTrue( $conf->has( 'one' ) );
+ $this->assertFalse( $conf->has( 'two' ) );
+ }
+
+ /**
+ * @covers HashConfig::set
+ */
+ public function testSet() {
+ $conf = new HashConfig( array(
+ 'one' => '1',
+ ) );
+ $conf->set( 'two', '2' );
+ $this->assertEquals( '2', $conf->get( 'two' ) );
+ // Check that set overwrites
+ $conf->set( 'one', '3' );
+ $this->assertEquals( '3', $conf->get( 'one' ) );
+ }
+} \ No newline at end of file
diff --git a/tests/phpunit/includes/config/MultiConfigTest.php b/tests/phpunit/includes/config/MultiConfigTest.php
new file mode 100644
index 00000000..158da466
--- /dev/null
+++ b/tests/phpunit/includes/config/MultiConfigTest.php
@@ -0,0 +1,38 @@
+<?php
+
+class MultiConfigTest extends MediaWikiTestCase {
+
+ /**
+ * Tests that settings are fetched in the right order
+ *
+ * @covers MultiConfig::get
+ */
+ public function testGet() {
+ $multi = new MultiConfig( array(
+ new HashConfig( array( 'foo' => 'bar' ) ),
+ new HashConfig( array( 'foo' => 'baz', 'bar' => 'foo' ) ),
+ new HashConfig( array( 'bar' => 'baz' ) ),
+ ) );
+
+ $this->assertEquals( 'bar', $multi->get( 'foo' ) );
+ $this->assertEquals( 'foo', $multi->get( 'bar' ) );
+ $this->setExpectedException( 'ConfigException', 'MultiConfig::get: undefined option:' );
+ $multi->get( 'notset' );
+ }
+
+ /**
+ * @covers MultiConfig::has
+ */
+ public function testHas() {
+ $conf = new MultiConfig( array(
+ new HashConfig( array( 'foo' => 'foo' ) ),
+ new HashConfig( array( 'something' => 'bleh' ) ),
+ new HashConfig( array( 'meh' => 'eh' ) ),
+ ) );
+
+ $this->assertTrue( $conf->has( 'foo' ) );
+ $this->assertTrue( $conf->has( 'something' ) );
+ $this->assertTrue( $conf->has( 'meh' ) );
+ $this->assertFalse( $conf->has( 'what' ) );
+ }
+}