Merge "Tweak RefreshLinksJob cache logic"
[lhc/web/wiklou.git] / tests / phpunit / tests / MediaWikiTestCaseTest.php
index 64def91..955dd2f 100644 (file)
@@ -1,19 +1,21 @@
 <?php
+use MediaWiki\Logger\LoggerFactory;
+use Psr\Log\LoggerInterface;
 
 /**
  * @covers MediaWikiTestCase
- * @author Adam Shorland
+ * @author Addshore
  */
 class MediaWikiTestCaseTest extends MediaWikiTestCase {
 
        const GLOBAL_KEY_NONEXISTING = 'MediaWikiTestCaseTestGLOBAL-NONExisting';
 
-       private static $startGlobals = array(
+       private static $startGlobals = [
                'MediaWikiTestCaseTestGLOBAL-ExistingString' => 'foo',
                'MediaWikiTestCaseTestGLOBAL-ExistingStringEmpty' => '',
-               'MediaWikiTestCaseTestGLOBAL-ExistingArray' => array( 1, 'foo' => 'bar' ),
-               'MediaWikiTestCaseTestGLOBAL-ExistingArrayEmpty' => array(),
-       );
+               'MediaWikiTestCaseTestGLOBAL-ExistingArray' => [ 1, 'foo' => 'bar' ],
+               'MediaWikiTestCaseTestGLOBAL-ExistingArrayEmpty' => [],
+       ];
 
        public static function setUpBeforeClass() {
                parent::setUpBeforeClass();
@@ -30,10 +32,10 @@ class MediaWikiTestCaseTest extends MediaWikiTestCase {
        }
 
        public function provideExistingKeysAndNewValues() {
-               $providedArray = array();
+               $providedArray = [];
                foreach ( array_keys( self::$startGlobals ) as $key ) {
-                       $providedArray[] = array( $key, 'newValue' );
-                       $providedArray[] = array( $key, array( 'newValue' ) );
+                       $providedArray[] = [ $key, 'newValue' ];
+                       $providedArray[] = [ $key, [ 'newValue' ] ];
                }
                return $providedArray;
        }
@@ -97,4 +99,37 @@ class MediaWikiTestCaseTest extends 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( LoggerInterface::class ) );
+               $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( 'foo', $this->getMock( LoggerInterface::class ) );
+               $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( 'foo', $this->getMock( LoggerInterface::class ) );
+               $this->setLogger( 'foo', $this->getMock( LoggerInterface::class ) );
+               $this->tearDown();
+               $logger2 = LoggerFactory::getInstance( 'baz' );
+
+               $this->assertSame( $logger1, $logger2 );
+       }
 }