X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2FMediaWikiTestCase.php;h=fd02a3eeb46744a7cd227f4c51add82283385efd;hb=5c8e8ebb1e868ad0f72ea9343b04b9a9d5c5f140;hp=959901653d5348ee95a95e26052429d27a55dc9d;hpb=4465a9fb5a8763a394b3e1795506f939fc0a9005;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index 959901653d..fd02a3eeb4 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -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[] @@ -535,7 +542,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 ) { @@ -571,6 +582,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, @@ -684,8 +699,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(). * @@ -700,9 +713,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 @@ -1074,11 +1091,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 Database $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( Database $db, $prefix ) { + protected static function setupDatabaseWithTestPrefix( IMaintainableDatabase $db, $prefix ) { $tablesCloned = self::listTables( $db ); $dbClone = new CloneDatabase( $db, $tablesCloned, $prefix ); $dbClone->useTemporaryTables( self::$useTemporaryTables ); @@ -1190,16 +1207,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 Database instead of plain DBConnRef/IDatabase - $lb = $externalStoreDB->getLoadBalancer( $cluster ); - $dbw = $lb->getConnection( DB_MASTER ); - $dbws[] = $dbw; + $dbws[] = $externalStoreDB->getMaster( $cluster ); } } @@ -1217,7 +1232,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; @@ -1313,11 +1328,11 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { /** * @since 1.18 * - * @param Database $db + * @param IMaintainableDatabase $db * * @return array */ - public static function listTables( Database $db ) { + public static function listTables( IMaintainableDatabase $db ) { $prefix = $db->tablePrefix(); $tables = $db->listTables( $prefix, __METHOD__ ); @@ -1365,6 +1380,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { if ( isset( PHPUnitMaintClass::$additionalOptions[$offset] ) ) { return PHPUnitMaintClass::$additionalOptions[$offset]; } + + return null; } /**