DeprecatedGlobal: Support lazy-loading via StubObject
authorKunal Mehta <legoktm@member.fsf.org>
Sun, 11 Jun 2017 01:07:11 +0000 (18:07 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Thu, 6 Jul 2017 02:55:00 +0000 (19:55 -0700)
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
includes/DeprecatedGlobal.php
tests/phpunit/includes/DeprecatedGlobalTest.php [new file with mode: 0644]

index 8fc94c5..c95e799 100644 (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
index 7c592c6..60dde40 100644 (file)
  * 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 (file)
index 0000000..76a4f51
--- /dev/null
@@ -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 );
+       }
+
+}