2 use MediaWiki\Logger\LoggerFactory
;
3 use Psr\Log\LoggerInterface
;
6 * @covers MediaWikiTestCase
9 class MediaWikiTestCaseTest
extends MediaWikiTestCase
{
11 const GLOBAL_KEY_NONEXISTING
= 'MediaWikiTestCaseTestGLOBAL-NONExisting';
13 private static $startGlobals = [
14 'MediaWikiTestCaseTestGLOBAL-ExistingString' => 'foo',
15 'MediaWikiTestCaseTestGLOBAL-ExistingStringEmpty' => '',
16 'MediaWikiTestCaseTestGLOBAL-ExistingArray' => [ 1, 'foo' => 'bar' ],
17 'MediaWikiTestCaseTestGLOBAL-ExistingArrayEmpty' => [],
20 public static function setUpBeforeClass() {
21 parent
::setUpBeforeClass();
22 foreach ( self
::$startGlobals as $key => $value ) {
23 $GLOBALS[$key] = $value;
27 public static function tearDownAfterClass() {
28 parent
::tearDownAfterClass();
29 foreach ( self
::$startGlobals as $key => $value ) {
30 unset( $GLOBALS[$key] );
34 public function provideExistingKeysAndNewValues() {
36 foreach ( array_keys( self
::$startGlobals ) as $key ) {
37 $providedArray[] = [ $key, 'newValue' ];
38 $providedArray[] = [ $key, [ 'newValue' ] ];
40 return $providedArray;
44 * @dataProvider provideExistingKeysAndNewValues
46 * @covers MediaWikiTestCase::setMwGlobals
47 * @covers MediaWikiTestCase::tearDown
49 public function testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
50 $this->setMwGlobals( $globalKey, $newValue );
54 'Global failed to correctly set'
60 self
::$startGlobals[$globalKey],
62 'Global failed to be restored on tearDown'
67 * @dataProvider provideExistingKeysAndNewValues
69 * @covers MediaWikiTestCase::stashMwGlobals
70 * @covers MediaWikiTestCase::tearDown
72 public function testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
73 $this->stashMwGlobals( $globalKey );
74 $GLOBALS[$globalKey] = $newValue;
78 'Global failed to correctly set'
84 self
::$startGlobals[$globalKey],
86 'Global failed to be restored on tearDown'
91 * @covers MediaWikiTestCase::stashMwGlobals
93 public function testExceptionThrownWhenStashingNonExistentGlobals() {
94 $this->setExpectedException(
96 'Global with key ' . self
::GLOBAL_KEY_NONEXISTING
. ' doesn\'t exist and cant be stashed'
99 $this->stashMwGlobals( self
::GLOBAL_KEY_NONEXISTING
);
103 * @covers MediaWikiTestCase::setLogger
104 * @covers MediaWikiTestCase::restoreLogger
106 public function testLoggersAreRestoredOnTearDown() {
107 // replacing an existing logger
108 $logger1 = LoggerFactory
::getInstance( 'foo' );
109 $this->setLogger( 'foo', $this->getMock( LoggerInterface
::class ) );
110 $logger2 = LoggerFactory
::getInstance( 'foo' );
112 $logger3 = LoggerFactory
::getInstance( 'foo' );
114 $this->assertSame( $logger1, $logger3 );
115 $this->assertNotSame( $logger1, $logger2 );
117 // replacing a non-existing logger
118 $this->setLogger( 'foo', $this->getMock( LoggerInterface
::class ) );
119 $logger1 = LoggerFactory
::getInstance( 'bar' );
121 $logger2 = LoggerFactory
::getInstance( 'bar' );
123 $this->assertNotSame( $logger1, $logger2 );
124 $this->assertInstanceOf( '\Psr\Log\LoggerInterface', $logger2 );
126 // replacing same logger twice
127 $logger1 = LoggerFactory
::getInstance( 'baz' );
128 $this->setLogger( 'foo', $this->getMock( LoggerInterface
::class ) );
129 $this->setLogger( 'foo', $this->getMock( LoggerInterface
::class ) );
131 $logger2 = LoggerFactory
::getInstance( 'baz' );
133 $this->assertSame( $logger1, $logger2 );