2 use MediaWiki\Logger\LoggerFactory
;
3 use MediaWiki\MediaWikiServices
;
4 use Psr\Log\LoggerInterface
;
7 * @covers MediaWikiTestCase
10 class MediaWikiTestCaseTest
extends MediaWikiTestCase
{
12 const GLOBAL_KEY_NONEXISTING
= 'MediaWikiTestCaseTestGLOBAL-NONExisting';
14 private static $startGlobals = [
15 'MediaWikiTestCaseTestGLOBAL-ExistingString' => 'foo',
16 'MediaWikiTestCaseTestGLOBAL-ExistingStringEmpty' => '',
17 'MediaWikiTestCaseTestGLOBAL-ExistingArray' => [ 1, 'foo' => 'bar' ],
18 'MediaWikiTestCaseTestGLOBAL-ExistingArrayEmpty' => [],
21 public static function setUpBeforeClass() {
22 parent
::setUpBeforeClass();
23 foreach ( self
::$startGlobals as $key => $value ) {
24 $GLOBALS[$key] = $value;
28 public static function tearDownAfterClass() {
29 parent
::tearDownAfterClass();
30 foreach ( self
::$startGlobals as $key => $value ) {
31 unset( $GLOBALS[$key] );
35 public function provideExistingKeysAndNewValues() {
37 foreach ( array_keys( self
::$startGlobals ) as $key ) {
38 $providedArray[] = [ $key, 'newValue' ];
39 $providedArray[] = [ $key, [ 'newValue' ] ];
41 return $providedArray;
45 * @dataProvider provideExistingKeysAndNewValues
47 * @covers MediaWikiTestCase::setMwGlobals
48 * @covers MediaWikiTestCase::tearDown
50 public function testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
51 $this->setMwGlobals( $globalKey, $newValue );
55 'Global failed to correctly set'
61 self
::$startGlobals[$globalKey],
63 'Global failed to be restored on tearDown'
68 * @dataProvider provideExistingKeysAndNewValues
70 * @covers MediaWikiTestCase::stashMwGlobals
71 * @covers MediaWikiTestCase::tearDown
73 public function testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
74 $this->stashMwGlobals( $globalKey );
75 $GLOBALS[$globalKey] = $newValue;
79 'Global failed to correctly set'
85 self
::$startGlobals[$globalKey],
87 'Global failed to be restored on tearDown'
92 * @covers MediaWikiTestCase::stashMwGlobals
93 * @covers MediaWikiTestCase::tearDown
95 public function testSetNonExistentGlobalsAreUnsetOnTearDown() {
96 $globalKey = 'abcdefg1234567';
97 $this->setMwGlobals( $globalKey, true );
100 'Global failed to correctly set'
106 isset( $GLOBALS[$globalKey] ),
107 'Global failed to be correctly unset'
111 public function testOverrideMwServices() {
112 $initialServices = MediaWikiServices
::getInstance();
114 $this->overrideMwServices();
115 $this->assertNotSame( $initialServices, MediaWikiServices
::getInstance() );
118 $this->assertSame( $initialServices, MediaWikiServices
::getInstance() );
121 public function testSetService() {
122 $initialServices = MediaWikiServices
::getInstance();
123 $initialService = $initialServices->getDBLoadBalancer();
124 $mockService = $this->getMockBuilder( LoadBalancer
::class )
125 ->disableOriginalConstructor()->getMock();
127 $this->setService( 'DBLoadBalancer', $mockService );
128 $this->assertNotSame( $initialServices, MediaWikiServices
::getInstance() );
129 $this->assertNotSame(
131 MediaWikiServices
::getInstance()->getDBLoadBalancer()
133 $this->assertSame( $mockService, MediaWikiServices
::getInstance()->getDBLoadBalancer() );
136 $this->assertSame( $initialServices, MediaWikiServices
::getInstance() );
137 $this->assertNotSame( $mockService, MediaWikiServices
::getInstance()->getDBLoadBalancer() );
138 $this->assertSame( $initialService, MediaWikiServices
::getInstance()->getDBLoadBalancer() );
142 * @covers MediaWikiTestCase::setLogger
143 * @covers MediaWikiTestCase::restoreLogger
145 public function testLoggersAreRestoredOnTearDown_replacingExistingLogger() {
146 $logger1 = LoggerFactory
::getInstance( 'foo' );
147 $this->setLogger( 'foo', $this->getMock( LoggerInterface
::class ) );
148 $logger2 = LoggerFactory
::getInstance( 'foo' );
150 $logger3 = LoggerFactory
::getInstance( 'foo' );
152 $this->assertSame( $logger1, $logger3 );
153 $this->assertNotSame( $logger1, $logger2 );
157 * @covers MediaWikiTestCase::setLogger
158 * @covers MediaWikiTestCase::restoreLogger
160 public function testLoggersAreRestoredOnTearDown_replacingNonExistingLogger() {
161 $this->setLogger( 'foo', $this->getMock( LoggerInterface
::class ) );
162 $logger1 = LoggerFactory
::getInstance( 'foo' );
164 $logger2 = LoggerFactory
::getInstance( 'foo' );
166 $this->assertNotSame( $logger1, $logger2 );
167 $this->assertInstanceOf( '\Psr\Log\LoggerInterface', $logger2 );
171 * @covers MediaWikiTestCase::setLogger
172 * @covers MediaWikiTestCase::restoreLogger
174 public function testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice() {
175 $logger1 = LoggerFactory
::getInstance( 'baz' );
176 $this->setLogger( 'foo', $this->getMock( LoggerInterface
::class ) );
177 $this->setLogger( 'foo', $this->getMock( LoggerInterface
::class ) );
179 $logger2 = LoggerFactory
::getInstance( 'baz' );
181 $this->assertSame( $logger1, $logger2 );