From 1d77070f19e37efd80bf9038d5259e19719c6d53 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Sat, 10 Jun 2017 18:07:11 -0700 Subject: [PATCH] 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 --- RELEASE-NOTES-1.30 | 2 + includes/DeprecatedGlobal.php | 15 ++-- .../phpunit/includes/DeprecatedGlobalTest.php | 70 +++++++++++++++++++ 3 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 tests/phpunit/includes/DeprecatedGlobalTest.php diff --git a/RELEASE-NOTES-1.30 b/RELEASE-NOTES-1.30 index 8fc94c5b1a..c95e79906d 100644 --- a/RELEASE-NOTES-1.30 +++ b/RELEASE-NOTES-1.30 @@ -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 diff --git a/includes/DeprecatedGlobal.php b/includes/DeprecatedGlobal.php index 7c592c6e59..60dde401ec 100644 --- a/includes/DeprecatedGlobal.php +++ b/includes/DeprecatedGlobal.php @@ -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 } diff --git a/tests/phpunit/includes/DeprecatedGlobalTest.php b/tests/phpunit/includes/DeprecatedGlobalTest.php new file mode 100644 index 0000000000..76a4f515dd --- /dev/null +++ b/tests/phpunit/includes/DeprecatedGlobalTest.php @@ -0,0 +1,70 @@ +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 ); + } + +} -- 2.20.1