Merge "RCFilters: Change tooltip messages for view buttons"
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index 419ff00..c2eebfa 100644 (file)
@@ -1,9 +1,11 @@
 <?php
+
 use MediaWiki\Logger\LegacySpi;
 use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\Logger\MonologSpi;
 use MediaWiki\MediaWikiServices;
 use Psr\Log\LoggerInterface;
+use Wikimedia\TestingAccessWrapper;
 
 /**
  * @since 1.18
@@ -213,6 +215,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        protected static function resetGlobalServices( Config $bootstrapConfig = null ) {
                $oldServices = MediaWikiServices::getInstance();
                $oldConfigFactory = $oldServices->getConfigFactory();
+               $oldLoadBalancerFactory = $oldServices->getDBLoadBalancerFactory();
 
                $testConfig = self::makeTestConfig( $bootstrapConfig );
 
@@ -221,6 +224,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $serviceLocator = MediaWikiServices::getInstance();
                self::installTestServices(
                        $oldConfigFactory,
+                       $oldLoadBalancerFactory,
                        $serviceLocator
                );
                return $serviceLocator;
@@ -276,12 +280,14 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
        /**
         * @param ConfigFactory $oldConfigFactory
+        * @param LBFactory $oldLoadBalancerFactory
         * @param MediaWikiServices $newServices
         *
         * @throws MWException
         */
        private static function installTestServices(
                ConfigFactory $oldConfigFactory,
+               LBFactory $oldLoadBalancerFactory,
                MediaWikiServices $newServices
        ) {
                // Use bootstrap config for all configuration.
@@ -295,6 +301,13 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                                [ 'main' =>  $bootstrapConfig ]
                        )
                );
+               $newServices->resetServiceForTesting( 'DBLoadBalancerFactory' );
+               $newServices->redefineService(
+                       'DBLoadBalancerFactory',
+                       function ( MediaWikiServices $services ) use ( $oldLoadBalancerFactory ) {
+                               return $oldLoadBalancerFactory;
+                       }
+               );
        }
 
        /**
@@ -307,7 +320,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                ConfigFactory $oldFactory,
                array $configurations
        ) {
-               return function( MediaWikiServices $services ) use ( $oldFactory, $configurations ) {
+               return function ( MediaWikiServices $services ) use ( $oldFactory, $configurations ) {
                        $factory = new ConfigFactory();
 
                        // clone configurations from $oldFactory that are not overwritten by $configurations
@@ -735,6 +748,11 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                                        $GLOBALS[$globalKey] instanceof FauxRequest
                                ) {
                                        $this->mwGlobals[$globalKey] = clone $GLOBALS[$globalKey];
+                               } elseif ( $this->containsClosure( $GLOBALS[$globalKey] ) ) {
+                                       // Serializing Closure only gives a warning on HHVM while
+                                       // it throws an Exception on Zend.
+                                       // Workaround for https://github.com/facebook/hhvm/issues/6206
+                                       $this->mwGlobals[$globalKey] = $GLOBALS[$globalKey];
                                } else {
                                        try {
                                                $this->mwGlobals[$globalKey] = unserialize( serialize( $GLOBALS[$globalKey] ) );
@@ -746,6 +764,28 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                }
        }
 
+       /**
+        * @param mixed $var
+        * @param int $maxDepth
+        *
+        * @return bool
+        */
+       private function containsClosure( $var, $maxDepth = 15 ) {
+               if ( $var instanceof Closure ) {
+                       return true;
+               }
+               if ( !is_array( $var ) || $maxDepth === 0 ) {
+                       return false;
+               }
+
+               foreach ( $var as $value ) {
+                       if ( $this->containsClosure( $value, $maxDepth - 1 ) ) {
+                               return true;
+                       }
+               }
+               return false;
+       }
+
        /**
         * Merges the given values into a MW global array variable.
         * Useful for setting some entries in a configuration array, instead of
@@ -800,6 +840,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                $oldInstance = MediaWikiServices::getInstance();
                $oldConfigFactory = $oldInstance->getConfigFactory();
+               $oldLoadBalancerFactory = $oldInstance->getDBLoadBalancerFactory();
 
                $testConfig = self::makeTestConfig( null, $configOverrides );
                $newInstance = new MediaWikiServices( $testConfig );
@@ -818,6 +859,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                self::installTestServices(
                        $oldConfigFactory,
+                       $oldLoadBalancerFactory,
                        $newInstance
                );
                MediaWikiServices::forceGlobalInstance( $newInstance );
@@ -1298,7 +1340,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         */
        public function __call( $func, $args ) {
                static $compatibility = [
-                       'assertEmpty' => 'assertEmpty2', // assertEmpty was added in phpunit 3.7.32
+                       'createMock' => 'createMock2',
                ];
 
                if ( isset( $compatibility[$func] ) ) {
@@ -1309,12 +1351,20 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        }
 
        /**
-        * Used as a compatibility method for phpunit < 3.7.32
-        * @param string $value
-        * @param string $msg
+        * Return a test double for the specified class.
+        *
+        * @param string $originalClassName
+        * @return PHPUnit_Framework_MockObject_MockObject
+        * @throws Exception
         */
-       private function assertEmpty2( $value, $msg ) {
-               $this->assertTrue( $value == '', $msg );
+       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 ) {
@@ -1768,51 +1818,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $this->assertEmpty( $errors, implode( "\n", $errors ) );
        }
 
-       /**
-        * @param array $matcher
-        * @param string $actual
-        * @param bool $isHtml
-        *
-        * @return bool
-        */
-       private static function tagMatch( $matcher, $actual, $isHtml = true ) {
-               $dom = PHPUnit_Util_XML::load( $actual, $isHtml );
-               $tags = PHPUnit_Util_XML::findNodes( $dom, $matcher, $isHtml );
-               return count( $tags ) > 0 && $tags[0] instanceof DOMNode;
-       }
-
-       /**
-        * Note: we are overriding this method to remove the deprecated error
-        * @see https://phabricator.wikimedia.org/T71505
-        * @see https://github.com/sebastianbergmann/phpunit/issues/1292
-        * @deprecated
-        *
-        * @param array $matcher
-        * @param string $actual
-        * @param string $message
-        * @param bool $isHtml
-        */
-       public static function assertTag( $matcher, $actual, $message = '', $isHtml = true ) {
-               // trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
-
-               self::assertTrue( self::tagMatch( $matcher, $actual, $isHtml ), $message );
-       }
-
-       /**
-        * @see MediaWikiTestCase::assertTag
-        * @deprecated
-        *
-        * @param array $matcher
-        * @param string $actual
-        * @param string $message
-        * @param bool $isHtml
-        */
-       public static function assertNotTag( $matcher, $actual, $message = '', $isHtml = true ) {
-               // trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
-
-               self::assertFalse( self::tagMatch( $matcher, $actual, $isHtml ), $message );
-       }
-
        /**
         * Used as a marker to prevent wfResetOutputBuffers from breaking PHPUnit.
         * @return string