Provide PHPUnit 4 and 6 compatibility layer
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index d542826..13d09ab 100644 (file)
@@ -5,16 +5,19 @@ use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\Logger\MonologSpi;
 use MediaWiki\MediaWikiServices;
 use Psr\Log\LoggerInterface;
+use Wikimedia\Rdbms\IDatabase;
 use Wikimedia\Rdbms\IMaintainableDatabase;
 use Wikimedia\Rdbms\Database;
+use Wikimedia\Rdbms\LBFactory;
 use Wikimedia\TestingAccessWrapper;
 
 /**
  * @since 1.18
  */
-abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
+abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
 
        use MediaWikiCoversValidator;
+       use PHPUnit4And6Compat;
 
        /**
         * The service locator created by prepareServices(). This service locator will
@@ -258,20 +261,19 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                 * which we can't allow, as that would open a new connection for mysql.
                 * Replace with a HashBag. They would not be going to persist anyway.
                 */
-               $hashCache = [ 'class' => 'HashBagOStuff', 'reportDupes' => false ];
+               $hashCache = [ 'class' => HashBagOStuff::class, 'reportDupes' => false ];
                $objectCaches = [
                                CACHE_DB => $hashCache,
                                CACHE_ACCEL => $hashCache,
                                CACHE_MEMCACHED => $hashCache,
                                'apc' => $hashCache,
                                'apcu' => $hashCache,
-                               'xcache' => $hashCache,
                                'wincache' => $hashCache,
                        ] + $baseConfig->get( 'ObjectCaches' );
 
                $defaultOverrides->set( 'ObjectCaches', $objectCaches );
                $defaultOverrides->set( 'MainCacheType', CACHE_NONE );
-               $defaultOverrides->set( 'JobTypeConf', [ 'default' => [ 'class' => 'JobQueueMemory' ] ] );
+               $defaultOverrides->set( 'JobTypeConf', [ 'default' => [ 'class' => JobQueueMemory::class ] ] );
 
                // Use a fast hash algorithm to hash passwords.
                $defaultOverrides->set( 'PasswordDefault', 'A' );
@@ -408,6 +410,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        // is available in subclass's setUpBeforeClass() and setUp() methods.
                        // This would also remove the need for the HACK that is oncePerClass().
                        if ( $this->oncePerClass() ) {
+                               $this->setUpSchema( $this->db );
                                $this->addDBDataOnce();
                        }
 
@@ -1077,6 +1080,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        }
                }
 
+               SiteStatsInit::doPlaceholderInit();
+
                User::resetIdByNameCache();
 
                // Make sysop user
@@ -1152,6 +1157,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $dbClone = new CloneDatabase( $db, $tablesCloned, $prefix );
                $dbClone->useTemporaryTables( self::$useTemporaryTables );
 
+               $db->_originalTablePrefix = $db->tablePrefix();
+
                if ( ( $db->getType() == 'oracle' || !self::$useTemporaryTables ) && self::$reuseDB ) {
                        CloneDatabase::changePrefix( $prefix );
 
@@ -1295,6 +1302,132 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                return false;
        }
 
+       /**
+        * @throws LogicException if the given database connection is not a set up to use
+        * mock tables.
+        */
+       private function ensureMockDatabaseConnection( IDatabase $db ) {
+               if ( $db->tablePrefix() !== $this->dbPrefix() ) {
+                       throw new LogicException(
+                               'Trying to delete mock tables, but table prefix does not indicate a mock database.'
+                       );
+               }
+       }
+
+       /**
+        * Stub. If a test suite needs to test against a specific database schema, it should
+        * override this method and return the appropriate information from it.
+        *
+        * @return [ $tables, $scripts ] A tuple of two lists, with $tables being a list of tables
+        *         that will be re-created by the scripts, and $scripts being a list of SQL script
+        *         files for creating the tables listed.
+        */
+       protected function getSchemaOverrides() {
+               return [ [], [] ];
+       }
+
+       /**
+        * Applies any schema changes requested by calling setDbSchema().
+        * Called once per test class, just before addDataOnce().
+        */
+       private function setUpSchema( IMaintainableDatabase $db ) {
+               list( $tablesToAlter, $scriptsToRun ) = $this->getSchemaOverrides();
+
+               if ( $tablesToAlter && !$scriptsToRun ) {
+                       throw new InvalidArgumentException(
+                               'No scripts supplied for applying the database schema.'
+                       );
+               }
+
+               if ( !$tablesToAlter && $scriptsToRun ) {
+                       throw new InvalidArgumentException(
+                               'No tables declared to be altered by schema scripts.'
+                       );
+               }
+
+               $this->ensureMockDatabaseConnection( $db );
+
+               $previouslyAlteredTables = isset( $db->_alteredMockTables ) ? $db->_alteredMockTables : [];
+
+               if ( !$tablesToAlter && !$previouslyAlteredTables ) {
+                       return; // nothing to do
+               }
+
+               $tablesToDrop = array_merge( $previouslyAlteredTables, $tablesToAlter );
+               $tablesToRestore = array_diff( $previouslyAlteredTables, $tablesToAlter );
+
+               if ( $tablesToDrop ) {
+                       $this->dropMockTables( $db, $tablesToDrop );
+               }
+
+               if ( $tablesToRestore ) {
+                       $this->recloneMockTables( $db, $tablesToRestore );
+               }
+
+               foreach ( $scriptsToRun as $script ) {
+                       $db->sourceFile(
+                               $script,
+                               null,
+                               null,
+                               __METHOD__,
+                               function ( $cmd ) {
+                                       return $this->mungeSchemaUpdateQuery( $cmd );
+                               }
+                       );
+               }
+
+               $db->_alteredMockTables = $tablesToAlter;
+       }
+
+       private function mungeSchemaUpdateQuery( $cmd ) {
+               return self::$useTemporaryTables
+                       ? preg_replace( '/\bCREATE\s+TABLE\b/i', 'CREATE TEMPORARY TABLE', $cmd )
+                       : $cmd;
+       }
+
+       /**
+        * Drops the given mock tables.
+        *
+        * @param IMaintainableDatabase $db
+        * @param array $tables
+        */
+       private function dropMockTables( IMaintainableDatabase $db, array $tables ) {
+               $this->ensureMockDatabaseConnection( $db );
+
+               foreach ( $tables as $tbl ) {
+                       $tbl = $db->tableName( $tbl );
+                       $db->query( "DROP TABLE IF EXISTS $tbl", __METHOD__ );
+
+                       if ( $tbl === 'page' ) {
+                               // Forget about the pages since they don't
+                               // exist in the DB.
+                               LinkCache::singleton()->clear();
+                       }
+               }
+       }
+
+       /**
+        * Re-clones the given mock tables to restore them based on the live database schema.
+        *
+        * @param IMaintainableDatabase $db
+        * @param array $tables
+        */
+       private function recloneMockTables( IMaintainableDatabase $db, array $tables ) {
+               $this->ensureMockDatabaseConnection( $db );
+
+               if ( !isset( $db->_originalTablePrefix ) ) {
+                       throw new LogicException( 'No original table prefix know, cannot restore tables!' );
+               }
+
+               $originalTables = $db->listTables( $db->_originalTablePrefix, __METHOD__ );
+               $tables = array_intersect( $tables, $originalTables );
+
+               $dbClone = new CloneDatabase( $db, $tables, $db->tablePrefix(), $db->_originalTablePrefix );
+               $dbClone->useTemporaryTables( self::$useTemporaryTables );
+
+               $dbClone->cloneTableStructure();
+       }
+
        /**
         * Empty all tables so they can be repopulated for tests
         *
@@ -1343,50 +1476,12 @@ 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 ) );
        }
 
        private static function isNotUnittest( $table ) {
-               return strpos( $table, 'unittest_' ) !== 0;
+               return strpos( $table, self::DB_PREFIX ) !== 0;
        }
 
        /**
@@ -1465,9 +1560,9 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * @param string $function
         */
        public function hideDeprecated( $function ) {
-               MediaWiki\suppressWarnings();
+               Wikimedia\suppressWarnings();
                wfDeprecated( $function );
-               MediaWiki\restoreWarnings();
+               Wikimedia\restoreWarnings();
        }
 
        /**
@@ -1482,13 +1577,17 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * @param string|array $fields The columns to include in the result (and to sort by)
         * @param string|array $condition "where" condition(s)
         * @param array $expectedRows An array of arrays giving the expected rows.
+        * @param array $options Options for the query
+        * @param array $join_conds Join conditions for the query
         *
         * @throws MWException If this test cases's needsDB() method doesn't return true.
         *         Test cases can use "@group Database" to enable database test support,
         *         or list the tables under testing in $this->tablesUsed, or override the
         *         needsDB() method.
         */
-       protected function assertSelect( $table, $fields, $condition, array $expectedRows ) {
+       protected function assertSelect(
+               $table, $fields, $condition, array $expectedRows, array $options = [], array $join_conds = []
+       ) {
                if ( !$this->needsDB() ) {
                        throw new MWException( 'When testing database state, the test cases\'s needDB()' .
                                ' method should return true. Use @group Database or $this->tablesUsed.' );
@@ -1496,7 +1595,14 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                $db = wfGetDB( DB_REPLICA );
 
-               $res = $db->select( $table, $fields, $condition, wfGetCaller(), [ 'ORDER BY' => $fields ] );
+               $res = $db->select(
+                       $table,
+                       $fields,
+                       $condition,
+                       wfGetCaller(),
+                       $options + [ 'ORDER BY' => $fields ],
+                       $join_conds
+               );
                $this->assertNotEmpty( $res, "query failed: " . $db->lastError() );
 
                $i = 0;
@@ -1750,9 +1856,9 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                # This check may also protect against code injection in
                # case of broken installations.
-               MediaWiki\suppressWarnings();
+               Wikimedia\suppressWarnings();
                $haveDiff3 = $wgDiff3 && file_exists( $wgDiff3 );
-               MediaWiki\restoreWarnings();
+               Wikimedia\restoreWarnings();
 
                if ( !$haveDiff3 ) {
                        $this->markTestSkipped( "Skip test, since diff3 is not configured" );