Merge "Provide PHPUnit 4 and 6 compatibility layer"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 6 Apr 2018 23:04:48 +0000 (23:04 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 6 Apr 2018 23:04:48 +0000 (23:04 +0000)
tests/common/TestsAutoLoader.php
tests/phpunit/MediaWikiTestCase.php
tests/phpunit/PHPUnit4And6Compat.php [new file with mode: 0644]

index 1173e1c..b626063 100644 (file)
@@ -63,6 +63,7 @@ $wgAutoloadClasses += [
        'TestUserRegistry' => "$testDir/phpunit/includes/TestUserRegistry.php",
        'LessFileCompilationTest' => "$testDir/phpunit/LessFileCompilationTest.php",
        'MediaWikiCoversValidator' => "$testDir/phpunit/MediaWikiCoversValidator.php",
+       'PHPUnit4And6Compat' => "$testDir/phpunit/PHPUnit4And6Compat.php",
 
        # tests/phpunit/includes
        'RevisionDbTestBase' => "$testDir/phpunit/includes/RevisionDbTestBase.php",
@@ -184,3 +185,35 @@ $wgAutoloadClasses += [
        'ParserTestTopLevelSuite' => "$testDir/phpunit/suites/ParserTestTopLevelSuite.php",
 ];
 // phpcs:enable
+
+/**
+ * Alias any PHPUnit 4 era PHPUnit_... class
+ * to it's PHPUnit 6 replacement. For most classes
+ * this is a direct _ -> \ replacement, but for
+ * some others we might need to maintain a manual
+ * mapping. Once we drop support for PHPUnit 4 this
+ * should be considered deprecated and eventually removed.
+ */
+spl_autoload_register( function ( $class ) {
+       if ( strpos( $class, 'PHPUnit_' ) !== 0 ) {
+               // Skip if it doesn't start with the old prefix
+               return;
+       }
+
+       // Classes that don't map 100%
+       $map = [
+               'PHPUnit_Framework_TestSuite_DataProvider' => 'PHPUnit\Framework\DataProviderTestSuite'
+       ];
+
+       if ( isset( $map[$class] ) ) {
+               $newForm = $map[$class];
+       } else {
+               $newForm = str_replace( '_', '\\', $class );
+       }
+
+       if ( class_exists( $newForm ) ) {
+               // If the new class name exists, alias
+               // the old name to it.
+               class_alias( $newForm, $class );
+       }
+} );
index ffba861..47b6218 100644 (file)
@@ -17,6 +17,7 @@ use Wikimedia\TestingAccessWrapper;
 abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
 
        use MediaWikiCoversValidator;
+       use PHPUnit4And6Compat;
 
        /**
         * The service locator created by prepareServices(). This service locator will
@@ -1558,44 +1559,6 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                }
        }
 
-       /**
-        * @since 1.18
-        *
-        * @param string $func
-        * @param array $args
-        *
-        * @return mixed
-        * @throws MWException
-        */
-       public function __call( $func, $args ) {
-               static $compatibility = [
-                       'createMock' => 'createMock2',
-               ];
-
-               if ( isset( $compatibility[$func] ) ) {
-                       return call_user_func_array( [ $this, $compatibility[$func] ], $args );
-               } else {
-                       throw new MWException( "Called non-existent $func method on " . static::class );
-               }
-       }
-
-       /**
-        * Return a test double for the specified class.
-        *
-        * @param string $originalClassName
-        * @return PHPUnit_Framework_MockObject_MockObject
-        * @throws Exception
-        */
-       private function createMock2( $originalClassName ) {
-               return $this->getMockBuilder( $originalClassName )
-                       ->disableOriginalConstructor()
-                       ->disableOriginalClone()
-                       ->disableArgumentCloning()
-                       // New in phpunit-mock-objects 3.2 (phpunit 5.4.0)
-                       // ->disallowMockingUnknownTypes()
-                       ->getMock();
-       }
-
        private static function unprefixTable( &$tableName, $ind, $prefix ) {
                $tableName = substr( $tableName, strlen( $prefix ) );
        }
diff --git a/tests/phpunit/PHPUnit4And6Compat.php b/tests/phpunit/PHPUnit4And6Compat.php
new file mode 100644 (file)
index 0000000..ac2c4f5
--- /dev/null
@@ -0,0 +1,119 @@
+<?php
+/**
+ * Copyright (C) 2018 Kunal Mehta <legoktm@member.fsf.org>
+ *
+ * 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.
+ *
+ */
+
+/**
+ * @since 1.31
+ */
+trait PHPUnit4And6Compat {
+       /**
+        * @see PHPUnit_Framework_TestCase::setExpectedException
+        *
+        * This function was renamed to expectException() in PHPUnit 6, so this
+        * is a temporary backwards-compatibility layer while we transition.
+        */
+       public function setExpectedException( $name, $message = '', $code = null ) {
+               if ( is_callable( [ $this, 'expectException' ] ) ) {
+                       $this->expectException( $name );
+                       if ( $message !== '' ) {
+                               $this->expectExceptionMessage( $message );
+                       }
+                       if ( $code !== null ) {
+                               $this->expectExceptionCode( $code );
+                       }
+               } else {
+                       parent::setExpectedException( $name, $message, $code );
+               }
+       }
+
+       /**
+        * @see PHPUnit_Framework_TestCase::getMock
+        *
+        * @return PHPUnit_Framework_MockObject_MockObject
+        */
+       public function getMock( $originalClassName, $methods = [], array $arguments = [],
+               $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true,
+               $callAutoload = true, $cloneArguments = false, $callOriginalMethods = false,
+               $proxyTarget = null
+       ) {
+               if ( is_callable( 'parent::getMock' ) ) {
+                       return parent::getMock(
+                               $originalClassName, $methods, $arguments, $mockClassName,
+                               $callOriginalConstructor, $callOriginalClone, $callAutoload,
+                               $cloneArguments, $callOriginalMethods, $proxyTarget
+                       );
+               } else {
+                       $builder = $this->getMockBuilder( $originalClassName )
+                               ->setMethods( $methods )
+                               ->setConstructorArgs( $arguments )
+                               ->setMockClassName( $mockClassName )
+                               ->setProxyTarget( $proxyTarget );
+                       if ( $callOriginalConstructor ) {
+                               $builder->enableOriginalConstructor();
+                       } else {
+                               $builder->disableOriginalConstructor();
+                       }
+                       if ( $callOriginalClone ) {
+                               $builder->enableOriginalClone();
+                       } else {
+                               $builder->disableOriginalClone();
+                       }
+                       if ( $callAutoload ) {
+                               $builder->enableAutoload();
+                       } else {
+                               $builder->disableAutoload();
+                       }
+                       if ( $cloneArguments ) {
+                               $builder->enableArgumentCloning();
+                       } else {
+                               $builder->disableArgumentCloning();
+                       }
+                       if ( $callOriginalMethods ) {
+                               $builder->enableProxyingToOriginalMethods();
+                       } else {
+                               $builder->disableProxyingToOriginalMethods();
+                       }
+
+                       return $builder->getMock();
+               }
+       }
+
+       /**
+        * Return a test double for the specified class. This
+        * is a forward port of the createMock function that
+        * was introduced in PHPUnit 5.4.
+        *
+        * @param string $originalClassName
+        * @return PHPUnit_Framework_MockObject_MockObject
+        * @throws Exception
+        */
+       public function createMock( $originalClassName ) {
+               if ( is_callable( 'parent::createMock' ) ) {
+                       return parent::createMock( $originalClassName );
+               }
+               // Compat for PHPUnit <= 5.4
+               return $this->getMockBuilder( $originalClassName )
+                       ->disableOriginalConstructor()
+                       ->disableOriginalClone()
+                       ->disableArgumentCloning()
+                       // New in phpunit-mock-objects 3.2 (phpunit 5.4.0)
+                       // ->disallowMockingUnknownTypes()
+                       ->getMock();
+       }
+}