DeprecatedGlobal: Support lazy-loading via StubObject

Either a factory function or the class name should be passed in.
Providing the actual value is no longer supported.

And provide tests for DeprecatedGlobal.

Change-Id: I7180cc99c3a01e34f39a9abe54bd1d08137117ed
This commit is contained in:
Kunal Mehta 2017-06-10 18:07:11 -07:00
parent a13e081132
commit 1d77070f19
3 changed files with 81 additions and 6 deletions

View file

@ -127,6 +127,8 @@ changes to languages because of Phabricator reports.
WikiPage::makeParserOptions() to create the ParserOptions object and only
change options that affect the parser cache key.
* Article::viewRedirect() is deprecated.
* DeprecatedGlobal no longer supports passing in a direct value, it requires a
callable factory function or a class name.
== Compatibility ==
MediaWiki 1.30 requires PHP 5.5.9 or later. There is experimental support for

View file

@ -24,13 +24,16 @@
* Class to allow throwing wfDeprecated warnings
* when people use globals that we do not want them to.
*/
class DeprecatedGlobal extends StubObject {
protected $realValue, $version;
protected $version;
function __construct( $name, $realValue, $version = false ) {
parent::__construct( $name );
$this->realValue = $realValue;
/**
* @param string $name Global name
* @param callable|string $callback Factory function or class name to construct
* @param bool|string $version Version global was deprecated in
*/
function __construct( $name, $callback, $version = false ) {
parent::__construct( $name, $callback );
$this->version = $version;
}
@ -51,7 +54,7 @@ class DeprecatedGlobal extends StubObject {
* rather unlikely.
*/
wfDeprecated( '$' . $this->global, $this->version, false, 6 );
return $this->realValue;
return parent::_newObject();
}
// @codingStandardsIgnoreEnd
}

View file

@ -0,0 +1,70 @@
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
/**
* @covers DeprecatedGlobal
*/
class DeprecatedGlobalTest extends MediaWikiTestCase {
public function testObjectDeStub() {
global $wgDummy;
$wgDummy = new DeprecatedGlobal( 'wgDummy', new HashBagOStuff(), '1.30' );
$this->assertInstanceOf( DeprecatedGlobal::class, $wgDummy );
$this->hideDeprecated( '$wgDummy' );
// Trigger de-stubification
$wgDummy->get( 'foo' );
$this->assertInstanceOf( HashBagOStuff::class, $wgDummy );
}
public function testLazyLoad() {
global $wgDummyLazy;
$called = false;
$factory = function() use ( &$called ) {
$called = true;
return new HashBagOStuff();
};
$wgDummyLazy = new DeprecatedGlobal( 'wgDummyLazy', $factory, '1.30' );
$this->assertInstanceOf( DeprecatedGlobal::class, $wgDummyLazy );
$this->hideDeprecated( '$wgDummyLazy' );
$this->assertFalse( $called );
// Trigger de-stubification
$wgDummyLazy->get( 'foo' );
$this->assertTrue( $called );
$this->assertInstanceOf( HashBagOStuff::class, $wgDummyLazy );
}
/**
* @expectedException PHPUnit_Framework_Error
* @expectedExceptionMessage Use of $wgDummy1 was deprecated in MediaWiki 1.30
*/
public function testWarning() {
global $wgDummy1;
$wgDummy1 = new DeprecatedGlobal( 'wgDummy1', new HashBagOStuff(), '1.30' );
$wgDummy1->get( 'foo' );
$this->assertInstanceOf( HashBagOStuff::class, $wgDummy1 );
}
}