3 class GlobalVarConfigTest
extends MediaWikiTestCase
{
6 * @covers GlobalVarConfig::newInstance
8 public function testNewInstance() {
9 $config = GlobalVarConfig
::newInstance();
10 $this->assertInstanceOf( 'GlobalVarConfig', $config );
11 $this->maybeStashGlobal( 'wgBaz' );
12 $GLOBALS['wgBaz'] = 'somevalue';
13 // Check prefix is set to 'wg'
14 $this->assertEquals( 'somevalue', $config->get( 'Baz' ) );
18 * @covers GlobalVarConfig::__construct
19 * @dataProvider provideConstructor
21 public function testConstructor( $prefix ) {
22 $var = $prefix . 'GlobalVarConfigTest';
23 $rand = wfRandomString();
24 $this->maybeStashGlobal( $var );
25 $GLOBALS[$var] = $rand;
26 $config = new GlobalVarConfig( $prefix );
27 $this->assertInstanceOf( 'GlobalVarConfig', $config );
28 $this->assertEquals( $rand, $config->get( 'GlobalVarConfigTest' ) );
31 public static function provideConstructor() {
36 array( 'blahblahblahblah' ),
41 public function provideGet() {
43 'wgSomething' => 'default1',
44 'wgFoo' => 'default2',
45 'efVariable' => 'default3',
49 foreach ( $set as $var => $value ) {
50 $GLOBALS[$var] = $value;
54 array( 'Something', 'wg', 'default1' ),
55 array( 'Foo', 'wg', 'default2' ),
56 array( 'Variable', 'ef', 'default3' ),
57 array( 'BAR', '', 'default4' ),
58 array( 'ThisGlobalWasNotSetAbove', 'wg', false )
64 * @param string $prefix
65 * @param string $expected
66 * @dataProvider provideGet
67 * @covers GlobalVarConfig::get
68 * @covers GlobalVarConfig::getWithPrefix
70 public function testGet( $name, $prefix, $expected ) {
71 $config = new GlobalVarConfig( $prefix );
72 if ( $expected === false ) {
73 $this->setExpectedException( 'ConfigException', 'GlobalVarConfig::getWithPrefix: undefined variable:' );
75 $this->assertEquals( $config->get( $name ), $expected );
78 public static function provideSet() {
80 array( 'Foo', 'wg', 'wgFoo' ),
81 array( 'SomethingRandom', 'wg', 'wgSomethingRandom' ),
82 array( 'FromAnExtension', 'eg', 'egFromAnExtension' ),
83 array( 'NoPrefixHere', '', 'NoPrefixHere' ),
87 private function maybeStashGlobal( $var ) {
88 if ( array_key_exists( $var, $GLOBALS ) ) {
89 // Will be reset after this test is over
90 $this->stashMwGlobals( $var );
95 * @dataProvider provideSet
96 * @covers GlobalVarConfig::set
97 * @covers GlobalVarConfig::setWithPrefix
99 public function testSet( $name, $prefix, $var ) {
100 $this->maybeStashGlobal( $var );
101 $config = new GlobalVarConfig( $prefix );
102 $random = wfRandomString();
103 $config->set( $name, $random );
104 $this->assertArrayHasKey( $var, $GLOBALS );
105 $this->assertEquals( $random, $GLOBALS[$var] );