b15ffa721039e1929b8d61ea5cdfa87554be5084
[lhc/web/wiklou.git] / tests / phpunit / includes / config / GlobalVarConfigTest.php
1 <?php
2
3 class GlobalVarConfigTest extends MediaWikiTestCase {
4
5 /**
6 * @covers GlobalVarConfig::newInstance
7 */
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' ) );
15 }
16
17 /**
18 * @covers GlobalVarConfig::__construct
19 * @dataProvider provideConstructor
20 */
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' ) );
29 }
30
31 public static function provideConstructor() {
32 return array(
33 array( 'wg' ),
34 array( 'ef' ),
35 array( 'smw' ),
36 array( 'blahblahblahblah' ),
37 array( '' ),
38 );
39 }
40
41 public function provideGet() {
42 $set = array(
43 'wgSomething' => 'default1',
44 'wgFoo' => 'default2',
45 'efVariable' => 'default3',
46 'BAR' => 'default4',
47 );
48
49 foreach ( $set as $var => $value ) {
50 $GLOBALS[$var] = $value;
51 }
52
53 return array(
54 array( 'Something', 'wg', 'default1' ),
55 array( 'Foo', 'wg', 'default2' ),
56 array( 'Variable', 'ef', 'default3' ),
57 array( 'BAR', '', 'default4' ),
58 array( 'ThisGlobalWasNotSetAbove', 'wg', false )
59 );
60 }
61
62 /**
63 * @param string $name
64 * @param string $prefix
65 * @param string $expected
66 * @dataProvider provideGet
67 * @covers GlobalVarConfig::get
68 * @covers GlobalVarConfig::getWithPrefix
69 */
70 public function testGet( $name, $prefix, $expected ) {
71 $config = new GlobalVarConfig( $prefix );
72 if ( $expected === false ) {
73 $this->setExpectedException( 'ConfigException', 'GlobalVarConfig::getWithPrefix: undefined variable:' );
74 }
75 $this->assertEquals( $config->get( $name ), $expected );
76 }
77
78 public static function provideSet() {
79 return array(
80 array( 'Foo', 'wg', 'wgFoo' ),
81 array( 'SomethingRandom', 'wg', 'wgSomethingRandom' ),
82 array( 'FromAnExtension', 'eg', 'egFromAnExtension' ),
83 array( 'NoPrefixHere', '', 'NoPrefixHere' ),
84 );
85 }
86
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 );
91 }
92 }
93
94 /**
95 * @dataProvider provideSet
96 * @covers GlobalVarConfig::set
97 * @covers GlobalVarConfig::setWithPrefix
98 */
99 public function testSet( $name, $prefix, $var ) {
100 $this->hideDeprecated( 'GlobalVarConfig::set' );
101 $this->maybeStashGlobal( $var );
102 $config = new GlobalVarConfig( $prefix );
103 $random = wfRandomString();
104 $config->set( $name, $random );
105 $this->assertArrayHasKey( $var, $GLOBALS );
106 $this->assertEquals( $random, $GLOBALS[$var] );
107 }
108 }