<?php
+use MediaWiki\Logger\LegacySpi;
+use MediaWiki\Logger\LoggerFactory;
+use MediaWiki\Logger\MonologSpi;
+use Psr\Log\LoggerInterface;
/**
* @since 1.18
* This property must be private, we do not want child to override it,
* they should call the appropriate parent method instead.
*/
- private $called = array();
+ private $called = [];
/**
* @var TestUser[]
* @var array
* @since 1.19
*/
- protected $tablesUsed = array(); // tables with data
+ protected $tablesUsed = []; // tables with data
private static $useTemporaryTables = true;
private static $reuseDB = false;
*
* @var array
*/
- private $tmpFiles = array();
+ private $tmpFiles = [];
/**
* Holds original values of MediaWiki configuration settings
* See also setMwGlobals().
* @var array
*/
- private $mwGlobals = array();
+ private $mwGlobals = [];
+
+ /**
+ * Holds original loggers which have been replaced by setLogger()
+ * @var LoggerInterface[]
+ */
+ private $loggers = [];
/**
* Table name prefixes. Oracle likes it shorter.
* @var array
* @since 1.18
*/
- protected $supportedDBs = array(
+ protected $supportedDBs = [
'mysql',
'sqlite',
'postgres',
'oracle'
- );
+ ];
- public function __construct( $name = null, array $data = array(), $dataName = '' ) {
+ public function __construct( $name = null, array $data = [], $dataName = '' ) {
parent::__construct( $name, $data, $dataName );
$this->backupGlobals = false;
// Complain if self::setUp() was called, but not self::tearDown()
// $this->called['setUp'] will be checked by self::testMediaWikiTestCaseParentSetupCalled()
if ( isset( $this->called['setUp'] ) && !isset( $this->called['tearDown'] ) ) {
- throw new MWException( get_called_class() . "::tearDown() must call parent::tearDown()" );
+ throw new MWException( static::class . "::tearDown() must call parent::tearDown()" );
}
}
foreach ( $this->mwGlobals as $key => $value ) {
$GLOBALS[$key] = $value;
}
- $this->mwGlobals = array();
+ $this->mwGlobals = [];
+ $this->restoreLoggers();
RequestContext::resetMain();
MediaHandler::resetCache();
if ( session_id() !== '' ) {
*/
final public function testMediaWikiTestCaseParentSetupCalled() {
$this->assertArrayHasKey( 'setUp', $this->called,
- get_called_class() . "::setUp() must call parent::setUp()"
+ static::class . '::setUp() must call parent::setUp()'
);
}
*/
protected function setMwGlobals( $pairs, $value = null ) {
if ( is_string( $pairs ) ) {
- $pairs = array( $pairs => $value );
+ $pairs = [ $pairs => $value ];
}
$this->stashMwGlobals( array_keys( $pairs ) );
*/
protected function stashMwGlobals( $globalKeys ) {
if ( is_string( $globalKeys ) ) {
- $globalKeys = array( $globalKeys );
+ $globalKeys = [ $globalKeys ];
}
foreach ( $globalKeys as $globalKey ) {
$this->setMwGlobals( $name, $merged );
}
+ /**
+ * Sets the logger for a specified channel, for the duration of the test.
+ * @since 1.27
+ * @param string $channel
+ * @param LoggerInterface $logger
+ */
+ protected function setLogger( $channel, LoggerInterface $logger ) {
+ $provider = LoggerFactory::getProvider();
+ $wrappedProvider = TestingAccessWrapper::newFromObject( $provider );
+ $singletons = $wrappedProvider->singletons;
+ if ( $provider instanceof MonologSpi ) {
+ if ( !isset( $this->loggers[$channel] ) ) {
+ $this->loggers[$channel] = isset( $singletons['loggers'][$channel] )
+ ? $singletons['loggers'][$channel] : null;
+ }
+ $singletons['loggers'][$channel] = $logger;
+ } elseif ( $provider instanceof LegacySpi ) {
+ if ( !isset( $this->loggers[$channel] ) ) {
+ $this->loggers[$channel] = isset( $singletons[$channel] ) ? $singletons[$channel] : null;
+ }
+ $singletons[$channel] = $logger;
+ } else {
+ throw new LogicException( __METHOD__ . ': setting a logger for ' . get_class( $provider )
+ . ' is not implemented' );
+ }
+ $wrappedProvider->singletons = $singletons;
+ }
+
+ /**
+ * Restores loggers replaced by setLogger().
+ * @since 1.27
+ */
+ private function restoreLoggers() {
+ $provider = LoggerFactory::getProvider();
+ $wrappedProvider = TestingAccessWrapper::newFromObject( $provider );
+ $singletons = $wrappedProvider->singletons;
+ foreach ( $this->loggers as $channel => $logger ) {
+ if ( $provider instanceof MonologSpi ) {
+ if ( $logger === null ) {
+ unset( $singletons['loggers'][$channel] );
+ } else {
+ $singletons['loggers'][$channel] = $logger;
+ }
+ } elseif ( $provider instanceof LegacySpi ) {
+ if ( $logger === null ) {
+ unset( $singletons[$channel] );
+ } else {
+ $singletons[$channel] = $logger;
+ }
+ }
+ }
+ $wrappedProvider->singletons = $singletons;
+ $this->loggers = [];
+ }
+
/**
* @return string
* @since 1.18
$page = WikiPage::factory( $title );
$page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
- return array(
+ return [
'title' => $title,
'id' => $page->getId(),
- );
+ ];
}
/**
# Insert 0 user to prevent FK violations
# Anonymous user
- $this->db->insert( 'user', array(
+ $this->db->insert( 'user', [
'user_id' => 0,
- 'user_name' => 'Anonymous' ), __METHOD__, array( 'IGNORE' ) );
+ 'user_name' => 'Anonymous' ], __METHOD__, [ 'IGNORE' ] );
# Insert 0 page to prevent FK violations
# Blank page
- $this->db->insert( 'page', array(
+ $this->db->insert( 'page', [
'page_id' => 0,
'page_namespace' => 0,
'page_title' => ' ',
'page_random' => 0,
'page_touched' => $this->db->timestamp(),
'page_latest' => 0,
- 'page_len' => 0 ), __METHOD__, array( 'IGNORE' ) );
+ 'page_len' => 0 ], __METHOD__, [ 'IGNORE' ] );
}
User::resetIdByNameCache();
* @throws MWException
*/
public function __call( $func, $args ) {
- static $compatibility = array(
+ static $compatibility = [
'assertEmpty' => 'assertEmpty2', // assertEmpty was added in phpunit 3.7.32
- );
+ ];
if ( isset( $compatibility[$func] ) ) {
- return call_user_func_array( array( $this, $compatibility[$func] ), $args );
+ return call_user_func_array( [ $this, $compatibility[$func] ], $args );
} else {
throw new MWException( "Called non-existent $func method on "
. get_class( $this ) );
$views = $db->listViews( $wgDBprefix, __METHOD__ );
$tables = array_diff( $tables, $views );
}
- $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
+ $tables = array_map( [ __CLASS__, 'unprefixTable' ], $tables );
// Don't duplicate test tables from the previous fataled run
- $tables = array_filter( $tables, array( __CLASS__, 'isNotUnittest' ) );
+ $tables = array_filter( $tables, [ __CLASS__, 'isNotUnittest' ] );
if ( $db->getType() == 'sqlite' ) {
$tables = array_flip( $tables );
$db = wfGetDB( DB_SLAVE );
- $res = $db->select( $table, $fields, $condition, wfGetCaller(), array( 'ORDER BY' => $fields ) );
+ $res = $db->select( $table, $fields, $condition, wfGetCaller(), [ 'ORDER BY' => $fields ] );
$this->assertNotEmpty( $res, "query failed: " . $db->lastError() );
$i = 0;
protected function arrayWrap( array $elements ) {
return array_map(
function ( $element ) {
- return array( $element );
+ return [ $element ];
},
$elements
);
}
call_user_func_array(
- array( $this, 'assertEquals' ),
- array_merge( array( $expected, $actual ), array_slice( func_get_args(), 4 ) )
+ [ $this, 'assertEquals' ],
+ array_merge( [ $expected, $actual ], array_slice( func_get_args(), 4 ) )
);
}
// NOTE: prefer content namespaces
$namespaces = array_unique( array_merge(
MWNamespace::getContentNamespaces(),
- array( NS_MAIN, NS_HELP, NS_PROJECT ), // prefer these
+ [ NS_MAIN, NS_HELP, NS_PROJECT ], // prefer these
MWNamespace::getValidNamespaces()
) );
- $namespaces = array_diff( $namespaces, array(
+ $namespaces = array_diff( $namespaces, [
NS_FILE, NS_CATEGORY, NS_MEDIAWIKI, NS_USER // don't mess with magic namespaces
- ) );
+ ] );
$talk = array_filter( $namespaces, function ( $ns ) {
return MWNamespace::isTalk( $ns );