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
94 public function testExceptionThrownWhenStashingNonExistentGlobals() {
95 $this->setExpectedException(
97 'Global with key ' . self
::GLOBAL_KEY_NONEXISTING
. ' doesn\'t exist and cant be stashed'
100 $this->stashMwGlobals( self
::GLOBAL_KEY_NONEXISTING
);
103 public function testOverrideMwServices() {
104 $initialServices = MediaWikiServices
::getInstance();
106 $this->overrideMwServices();
107 $this->assertNotSame( $initialServices, MediaWikiServices
::getInstance() );
110 $this->assertSame( $initialServices, MediaWikiServices
::getInstance() );
113 public function testSetService() {
114 $initialServices = MediaWikiServices
::getInstance();
115 $initialService = $initialServices->getDBLoadBalancer();
116 $mockService = $this->getMockBuilder( LoadBalancer
::class )
117 ->disableOriginalConstructor()->getMock();
119 $this->setService( 'DBLoadBalancer', $mockService );
120 $this->assertNotSame( $initialServices, MediaWikiServices
::getInstance() );
121 $this->assertNotSame(
123 MediaWikiServices
::getInstance()->getDBLoadBalancer()
125 $this->assertSame( $mockService, MediaWikiServices
::getInstance()->getDBLoadBalancer() );
128 $this->assertSame( $initialServices, MediaWikiServices
::getInstance() );
129 $this->assertNotSame( $mockService, MediaWikiServices
::getInstance()->getDBLoadBalancer() );
130 $this->assertSame( $initialService, MediaWikiServices
::getInstance()->getDBLoadBalancer() );
134 * @covers MediaWikiTestCase::setLogger
135 * @covers MediaWikiTestCase::restoreLogger
137 public function testLoggersAreRestoredOnTearDown() {
138 // replacing an existing logger
139 $logger1 = LoggerFactory
::getInstance( 'foo' );
140 $this->setLogger( 'foo', $this->getMock( LoggerInterface
::class ) );
141 $logger2 = LoggerFactory
::getInstance( 'foo' );
143 $logger3 = LoggerFactory
::getInstance( 'foo' );
145 $this->assertSame( $logger1, $logger3 );
146 $this->assertNotSame( $logger1, $logger2 );
148 // replacing a non-existing logger
149 $this->setLogger( 'foo', $this->getMock( LoggerInterface
::class ) );
150 $logger1 = LoggerFactory
::getInstance( 'bar' );
152 $logger2 = LoggerFactory
::getInstance( 'bar' );
154 $this->assertNotSame( $logger1, $logger2 );
155 $this->assertInstanceOf( '\Psr\Log\LoggerInterface', $logger2 );
157 // replacing same logger twice
158 $logger1 = LoggerFactory
::getInstance( 'baz' );
159 $this->setLogger( 'foo', $this->getMock( LoggerInterface
::class ) );
160 $this->setLogger( 'foo', $this->getMock( LoggerInterface
::class ) );
162 $logger2 = LoggerFactory
::getInstance( 'baz' );
164 $this->assertSame( $logger1, $logger2 );