wiki.techinc.nl/tests/phpunit/includes/config/GlobalVarConfigTest.php
Aryeh Gregor 09eee138e1 Deprecate MediaWikiTestCase::stashMwGlobals
This method encourages directly editing configuration variables.  It's a
better idea to use setMwGlobals() (or other set wrappers) so that we can
be intelligent in the future, for instance resetting services after the
config change.  Plus, a lot of the callers come out cleaner this way
anyway.

Depends-On: I8a1e81acc5c42a8d7f30938a72cface0acea4a70
Depends-On: I4105dbcf9c5399fe7239478c460ec57c015a98d4
Depends-On: I1b220996acf2f66cf7b0f092b341584663df32f9
Depends-On: Ie2d1ea65c0cb334bbde1666d00781474b7ac4dab
Change-Id: I23d77398e401f4986b1d5bd1c9e11a8a40da16f8
2018-10-07 19:39:47 +03:00

86 lines
2.2 KiB
PHP

<?php
class GlobalVarConfigTest extends MediaWikiTestCase {
/**
* @covers GlobalVarConfig::newInstance
*/
public function testNewInstance() {
$config = GlobalVarConfig::newInstance();
$this->assertInstanceOf( GlobalVarConfig::class, $config );
$this->setMwGlobals( '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->setMwGlobals( $var, $rand );
$config = new GlobalVarConfig( $prefix );
$this->assertInstanceOf( GlobalVarConfig::class, $config );
$this->assertEquals( $rand, $config->get( 'GlobalVarConfigTest' ) );
}
public static function provideConstructor() {
return [
[ 'wg' ],
[ 'ef' ],
[ 'smw' ],
[ 'blahblahblahblah' ],
[ '' ],
];
}
/**
* @covers GlobalVarConfig::has
* @covers GlobalVarConfig::hasWithPrefix
*/
public function testHas() {
$this->setMwGlobals( 'wgGlobalVarConfigTestHas', wfRandomString() );
$config = new GlobalVarConfig();
$this->assertTrue( $config->has( 'GlobalVarConfigTestHas' ) );
$this->assertFalse( $config->has( 'GlobalVarConfigTestNotHas' ) );
}
public static function provideGet() {
$set = [
'wgSomething' => 'default1',
'wgFoo' => 'default2',
'efVariable' => 'default3',
'BAR' => 'default4',
];
foreach ( $set as $var => $value ) {
$GLOBALS[$var] = $value;
}
return [
[ 'Something', 'wg', 'default1' ],
[ 'Foo', 'wg', 'default2' ],
[ 'Variable', 'ef', 'default3' ],
[ 'BAR', '', 'default4' ],
[ 'ThisGlobalWasNotSetAbove', 'wg', false ]
];
}
/**
* @dataProvider provideGet
* @covers GlobalVarConfig::get
* @covers GlobalVarConfig::getWithPrefix
* @param string $name
* @param string $prefix
* @param string $expected
*/
public function testGet( $name, $prefix, $expected ) {
$config = new GlobalVarConfig( $prefix );
if ( $expected === false ) {
$this->setExpectedException( ConfigException::class, 'GlobalVarConfig::get: undefined option:' );
}
$this->assertEquals( $config->get( $name ), $expected );
}
}