<?php
+use MediaWiki\Logger\LegacySpi;
+use MediaWiki\Logger\LoggerFactory;
+use MediaWiki\Logger\MonologSpi;
+use Psr\Log\LoggerInterface;
/**
* @since 1.18
*/
private $mwGlobals = array();
+ /**
+ * Holds original loggers which have been replaced by setLogger()
+ * @var LoggerInterface[]
+ */
+ private $loggers = array();
+
/**
* Table name prefixes. Oracle likes it shorter.
*/
$GLOBALS[$key] = $value;
}
$this->mwGlobals = array();
+ $this->restoreLoggers();
RequestContext::resetMain();
MediaHandler::resetCache();
if ( session_id() !== '' ) {
$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 = array();
+ }
+
/**
* @return string
* @since 1.18
<?php
+use MediaWiki\Logger\LoggerFactory;
/**
* @covers MediaWikiTestCase
$this->stashMwGlobals( self::GLOBAL_KEY_NONEXISTING );
}
+ /**
+ * @covers MediaWikiTestCase::setLogger
+ * @covers MediaWikiTestCase::restoreLogger
+ */
+ public function testLoggersAreRestoredOnTearDown() {
+ // replacing an existing logger
+ $logger1 = LoggerFactory::getInstance( 'foo' );
+ $this->setLogger( 'foo', $this->getMock( '\Psr\Log\LoggerInterface' ) );
+ $logger2 = LoggerFactory::getInstance( 'foo' );
+ $this->tearDown();
+ $logger3 = LoggerFactory::getInstance( 'foo' );
+
+ $this->assertSame( $logger1, $logger3 );
+ $this->assertNotSame( $logger1, $logger2 );
+
+ // replacing a non-existing logger
+ $this->setLogger( 'bar', $this->getMock( '\Psr\Log\LoggerInterface' ) );
+ $logger1 = LoggerFactory::getInstance( 'bar' );
+ $this->tearDown();
+ $logger2 = LoggerFactory::getInstance( 'bar' );
+
+ $this->assertNotSame( $logger1, $logger2 );
+ $this->assertInstanceOf( '\Psr\Log\LoggerInterface', $logger2 );
+
+ // replacing same logger twice
+ $logger1 = LoggerFactory::getInstance( 'baz' );
+ $this->setLogger( 'baz', $this->getMock( '\Psr\Log\LoggerInterface' ) );
+ $this->setLogger( 'baz', $this->getMock( '\Psr\Log\LoggerInterface' ) );
+ $this->tearDown();
+ $logger2 = LoggerFactory::getInstance( 'baz' );
+
+ $this->assertSame( $logger1, $logger2 );
+ }
}