Update tests/phpunit/MediaWikiTestCase.php with support for apcu
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index c952229..37f76ff 100644 (file)
@@ -42,7 +42,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        /**
         * Primary database
         *
-        * @var DatabaseBase
+        * @var Database
         * @since 1.18
         */
        protected $db;
@@ -81,6 +81,13 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         */
        private $mwGlobals = [];
 
+       /**
+        * Holds list of MediaWiki configuration settings to be unset in tearDown().
+        * See also setMwGlobals().
+        * @var array
+        */
+       private $mwGlobalsToUnset = [];
+
        /**
         * Holds original loggers which have been replaced by setLogger()
         * @var LoggerInterface[]
@@ -242,12 +249,13 @@ 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' ];
+               $hashCache = [ 'class' => 'HashBagOStuff', 'reportDupes' => false ];
                $objectCaches = [
                                CACHE_DB => $hashCache,
                                CACHE_ACCEL => $hashCache,
                                CACHE_MEMCACHED => $hashCache,
                                'apc' => $hashCache,
+                               'apcu' => $hashCache,
                                'xcache' => $hashCache,
                                'wincache' => $hashCache,
                        ] + $baseConfig->get( 'ObjectCaches' );
@@ -336,6 +344,10 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                JobQueueGroup::destroySingletons();
                ObjectCache::clear();
+               $services = MediaWikiServices::getInstance();
+               $services->resetServiceForTesting( 'MainObjectStash' );
+               $services->resetServiceForTesting( 'LocalServerObjectCache' );
+               $services->getMainWANObjectCache()->clearProcessCache();
                FileBackendGroup::destroySingleton();
 
                // TODO: move global state into MediaWikiServices
@@ -531,7 +543,11 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                foreach ( $this->mwGlobals as $key => $value ) {
                        $GLOBALS[$key] = $value;
                }
+               foreach ( $this->mwGlobalsToUnset as $value ) {
+                       unset( $GLOBALS[$value] );
+               }
                $this->mwGlobals = [];
+               $this->mwGlobalsToUnset = [];
                $this->restoreLoggers();
 
                if ( self::$serviceLocator && MediaWikiServices::getInstance() !== self::$serviceLocator ) {
@@ -567,6 +583,10 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        /**
         * Make sure MediaWikiTestCase extending classes have called their
         * parent setUp method
+        *
+        * With strict coverage activated in PHP_CodeCoverage, this test would be
+        * marked as risky without the following annotation (T152923).
+        * @coversNothing
         */
        final public function testMediaWikiTestCaseParentSetupCalled() {
                $this->assertArrayHasKey( 'setUp', $this->called,
@@ -680,8 +700,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         *
         * @param array|string $globalKeys Key to the global variable, or an array of keys.
         *
-        * @throws Exception When trying to stash an unset global
-        *
         * @note To allow changes to global variables to take effect on global service instances,
         *       call overrideMwServices().
         *
@@ -696,9 +714,13 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        // NOTE: make sure we only save the global once or a second call to
                        // setMwGlobals() on the same global would override the original
                        // value.
-                       if ( !array_key_exists( $globalKey, $this->mwGlobals ) ) {
+                       if (
+                               !array_key_exists( $globalKey, $this->mwGlobals ) &&
+                               !array_key_exists( $globalKey, $this->mwGlobalsToUnset )
+                       ) {
                                if ( !array_key_exists( $globalKey, $GLOBALS ) ) {
-                                       throw new Exception( "Global with key {$globalKey} doesn't exist and cant be stashed" );
+                                       $this->mwGlobalsToUnset[$globalKey] = $globalKey;
+                                       continue;
                                }
                                // NOTE: we serialize then unserialize the value in case it is an object
                                // this stops any objects being passed by reference. We could use clone
@@ -1070,11 +1092,11 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * Clones all tables in the given database (whatever database that connection has
         * open), to versions with the test prefix.
         *
-        * @param DatabaseBase $db Database to use
+        * @param IMaintainableDatabase $db Database to use
         * @param string $prefix Prefix to use for test tables
         * @return bool True if tables were cloned, false if only the prefix was changed
         */
-       protected static function setupDatabaseWithTestPrefix( DatabaseBase $db, $prefix ) {
+       protected static function setupDatabaseWithTestPrefix( IMaintainableDatabase $db, $prefix ) {
                $tablesCloned = self::listTables( $db );
                $dbClone = new CloneDatabase( $db, $tablesCloned, $prefix );
                $dbClone->useTemporaryTables( self::$useTemporaryTables );
@@ -1123,12 +1145,12 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * @note this method only works when first called. Subsequent calls have no effect,
         * even if using different parameters.
         *
-        * @param DatabaseBase $db The database connection
+        * @param Database $db The database connection
         * @param string $prefix The prefix to use for the new table set (aka schema).
         *
         * @throws MWException If the database table prefix is already $prefix
         */
-       public static function setupTestDB( DatabaseBase $db, $prefix ) {
+       public static function setupTestDB( Database $db, $prefix ) {
                if ( self::$dbSetup ) {
                        return;
                }
@@ -1139,7 +1161,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                }
 
                // TODO: the below should be re-written as soon as LBFactory, LoadBalancer,
-               // and DatabaseBase no longer use global state.
+               // and Database no longer use global state.
 
                self::$dbSetup = true;
 
@@ -1178,7 +1200,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * Gets master database connections for all of the ExternalStoreDB
         * stores configured in $wgDefaultExternalStore.
         *
-        * @return DatabaseBase[] Array of DatabaseBase master connections
+        * @return Database[] Array of Database master connections
         */
 
        protected static function getExternalStoreDatabaseConnections() {
@@ -1186,16 +1208,14 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                /** @var ExternalStoreDB $externalStoreDB */
                $externalStoreDB = ExternalStore::getStoreObject( 'DB' );
-               $defaultArray = (array) $wgDefaultExternalStore;
+               $defaultArray = (array)$wgDefaultExternalStore;
                $dbws = [];
                foreach ( $defaultArray as $url ) {
                        if ( strpos( $url, 'DB://' ) === 0 ) {
                                list( $proto, $cluster ) = explode( '://', $url, 2 );
                                // Avoid getMaster() because setupDatabaseWithTestPrefix()
-                               // requires DatabaseBase instead of plain DBConnRef/IDatabase
-                               $lb = $externalStoreDB->getLoadBalancer( $cluster );
-                               $dbw = $lb->getConnection( DB_MASTER );
-                               $dbws[] = $dbw;
+                               // requires Database instead of plain DBConnRef/IDatabase
+                               $dbws[] = $externalStoreDB->getMaster( $cluster );
                        }
                }
 
@@ -1213,7 +1233,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        return false;
                }
 
-               $defaultArray = (array) $wgDefaultExternalStore;
+               $defaultArray = (array)$wgDefaultExternalStore;
                foreach ( $defaultArray as $url ) {
                        if ( strpos( $url, 'DB://' ) === 0 ) {
                                return true;
@@ -1226,7 +1246,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        /**
         * Empty all tables so they can be repopulated for tests
         *
-        * @param DatabaseBase $db|null Database to reset
+        * @param Database $db|null Database to reset
         * @param array $tablesUsed Tables to reset
         */
        private function resetDB( $db, $tablesUsed ) {
@@ -1309,11 +1329,11 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        /**
         * @since 1.18
         *
-        * @param DatabaseBase $db
+        * @param IMaintainableDatabase $db
         *
         * @return array
         */
-       public static function listTables( DatabaseBase $db ) {
+       public static function listTables( IMaintainableDatabase $db ) {
                $prefix = $db->tablePrefix();
                $tables = $db->listTables( $prefix, __METHOD__ );
 
@@ -1361,6 +1381,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                if ( isset( PHPUnitMaintClass::$additionalOptions[$offset] ) ) {
                        return PHPUnitMaintClass::$additionalOptions[$offset];
                }
+
+               return null;
        }
 
        /**