2 use MediaWiki\Logger\LoggerFactory
;
3 use MediaWiki\MediaWikiServices
;
4 use Psr\Log\LoggerInterface
;
5 use Wikimedia\Rdbms\LoadBalancer
;
8 * @covers MediaWikiTestCase
9 * @group MediaWikiTestCaseTest
13 class MediaWikiTestCaseTest
extends MediaWikiTestCase
{
15 private static $startGlobals = [
16 'MediaWikiTestCaseTestGLOBAL-ExistingString' => 'foo',
17 'MediaWikiTestCaseTestGLOBAL-ExistingStringEmpty' => '',
18 'MediaWikiTestCaseTestGLOBAL-ExistingArray' => [ 1, 'foo' => 'bar' ],
19 'MediaWikiTestCaseTestGLOBAL-ExistingArrayEmpty' => [],
22 public static function setUpBeforeClass() {
23 parent
::setUpBeforeClass();
24 foreach ( self
::$startGlobals as $key => $value ) {
25 $GLOBALS[$key] = $value;
29 public static function tearDownAfterClass() {
30 parent
::tearDownAfterClass();
31 foreach ( self
::$startGlobals as $key => $value ) {
32 unset( $GLOBALS[$key] );
36 public function provideExistingKeysAndNewValues() {
38 foreach ( array_keys( self
::$startGlobals ) as $key ) {
39 $providedArray[] = [ $key, 'newValue' ];
40 $providedArray[] = [ $key, [ 'newValue' ] ];
42 return $providedArray;
46 * @dataProvider provideExistingKeysAndNewValues
48 * @covers MediaWikiTestCase::setMwGlobals
49 * @covers MediaWikiTestCase::tearDown
51 public function testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
52 $this->setMwGlobals( $globalKey, $newValue );
56 'Global failed to correctly set'
62 self
::$startGlobals[$globalKey],
64 'Global failed to be restored on tearDown'
69 * @dataProvider provideExistingKeysAndNewValues
71 * @covers MediaWikiTestCase::stashMwGlobals
72 * @covers MediaWikiTestCase::tearDown
74 public function testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
75 $this->stashMwGlobals( $globalKey );
76 $GLOBALS[$globalKey] = $newValue;
80 'Global failed to correctly set'
86 self
::$startGlobals[$globalKey],
88 'Global failed to be restored on tearDown'
93 * @covers MediaWikiTestCase::stashMwGlobals
94 * @covers MediaWikiTestCase::tearDown
96 public function testSetNonExistentGlobalsAreUnsetOnTearDown() {
97 $globalKey = 'abcdefg1234567';
98 $this->setMwGlobals( $globalKey, true );
100 $GLOBALS[$globalKey],
101 'Global failed to correctly set'
107 isset( $GLOBALS[$globalKey] ),
108 'Global failed to be correctly unset'
112 public function testOverrideMwServices() {
113 $initialServices = MediaWikiServices
::getInstance();
115 $this->overrideMwServices();
116 $this->assertNotSame( $initialServices, MediaWikiServices
::getInstance() );
119 $this->assertSame( $initialServices, MediaWikiServices
::getInstance() );
122 public function testSetService() {
123 $initialServices = MediaWikiServices
::getInstance();
124 $initialService = $initialServices->getDBLoadBalancer();
125 $mockService = $this->getMockBuilder( LoadBalancer
::class )
126 ->disableOriginalConstructor()->getMock();
128 $this->setService( 'DBLoadBalancer', $mockService );
129 $this->assertNotSame( $initialServices, MediaWikiServices
::getInstance() );
130 $this->assertNotSame(
132 MediaWikiServices
::getInstance()->getDBLoadBalancer()
134 $this->assertSame( $mockService, MediaWikiServices
::getInstance()->getDBLoadBalancer() );
137 $this->assertSame( $initialServices, MediaWikiServices
::getInstance() );
138 $this->assertNotSame( $mockService, MediaWikiServices
::getInstance()->getDBLoadBalancer() );
139 $this->assertSame( $initialService, MediaWikiServices
::getInstance()->getDBLoadBalancer() );
143 * @covers MediaWikiTestCase::setLogger
144 * @covers MediaWikiTestCase::restoreLoggers
146 public function testLoggersAreRestoredOnTearDown_replacingExistingLogger() {
147 $logger1 = LoggerFactory
::getInstance( 'foo' );
148 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
149 $logger2 = LoggerFactory
::getInstance( 'foo' );
151 $logger3 = LoggerFactory
::getInstance( 'foo' );
153 $this->assertSame( $logger1, $logger3 );
154 $this->assertNotSame( $logger1, $logger2 );
158 * @covers MediaWikiTestCase::setLogger
159 * @covers MediaWikiTestCase::restoreLoggers
161 public function testLoggersAreRestoredOnTearDown_replacingNonExistingLogger() {
162 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
163 $logger1 = LoggerFactory
::getInstance( 'foo' );
165 $logger2 = LoggerFactory
::getInstance( 'foo' );
167 $this->assertNotSame( $logger1, $logger2 );
168 $this->assertInstanceOf( '\Psr\Log\LoggerInterface', $logger2 );
172 * @covers MediaWikiTestCase::setLogger
173 * @covers MediaWikiTestCase::restoreLoggers
175 public function testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice() {
176 $logger1 = LoggerFactory
::getInstance( 'baz' );
177 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
178 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
180 $logger2 = LoggerFactory
::getInstance( 'baz' );
182 $this->assertSame( $logger1, $logger2 );