DeprecatedGlobal: Support lazy-loading via StubObject
[lhc/web/wiklou.git] / tests / phpunit / includes / DeprecatedGlobalTest.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 /**
22 * @covers DeprecatedGlobal
23 */
24 class DeprecatedGlobalTest extends MediaWikiTestCase {
25 public function testObjectDeStub() {
26 global $wgDummy;
27
28 $wgDummy = new DeprecatedGlobal( 'wgDummy', new HashBagOStuff(), '1.30' );
29 $this->assertInstanceOf( DeprecatedGlobal::class, $wgDummy );
30
31 $this->hideDeprecated( '$wgDummy' );
32 // Trigger de-stubification
33 $wgDummy->get( 'foo' );
34
35 $this->assertInstanceOf( HashBagOStuff::class, $wgDummy );
36 }
37
38 public function testLazyLoad() {
39 global $wgDummyLazy;
40
41 $called = false;
42 $factory = function() use ( &$called ) {
43 $called = true;
44 return new HashBagOStuff();
45 };
46
47 $wgDummyLazy = new DeprecatedGlobal( 'wgDummyLazy', $factory, '1.30' );
48 $this->assertInstanceOf( DeprecatedGlobal::class, $wgDummyLazy );
49
50 $this->hideDeprecated( '$wgDummyLazy' );
51 $this->assertFalse( $called );
52 // Trigger de-stubification
53 $wgDummyLazy->get( 'foo' );
54 $this->assertTrue( $called );
55 $this->assertInstanceOf( HashBagOStuff::class, $wgDummyLazy );
56 }
57
58 /**
59 * @expectedException PHPUnit_Framework_Error
60 * @expectedExceptionMessage Use of $wgDummy1 was deprecated in MediaWiki 1.30
61 */
62 public function testWarning() {
63 global $wgDummy1;
64
65 $wgDummy1 = new DeprecatedGlobal( 'wgDummy1', new HashBagOStuff(), '1.30' );
66 $wgDummy1->get( 'foo' );
67 $this->assertInstanceOf( HashBagOStuff::class, $wgDummy1 );
68 }
69
70 }