3 use MediaWiki\Logger\LoggerFactory
;
4 use MediaWiki\MediaWikiServices
;
5 use Psr\Log\LoggerInterface
;
6 use Wikimedia\Rdbms\LoadBalancer
;
9 * @covers MediaWikiTestCase
10 * @group MediaWikiTestCaseTest
15 class MediaWikiTestCaseTest
extends MediaWikiTestCase
{
17 private static $startGlobals = [
18 'MediaWikiTestCaseTestGLOBAL-ExistingString' => 'foo',
19 'MediaWikiTestCaseTestGLOBAL-ExistingStringEmpty' => '',
20 'MediaWikiTestCaseTestGLOBAL-ExistingArray' => [ 1, 'foo' => 'bar' ],
21 'MediaWikiTestCaseTestGLOBAL-ExistingArrayEmpty' => [],
24 public static function setUpBeforeClass() {
25 parent
::setUpBeforeClass();
26 foreach ( self
::$startGlobals as $key => $value ) {
27 $GLOBALS[$key] = $value;
31 public static function tearDownAfterClass() {
32 parent
::tearDownAfterClass();
33 foreach ( self
::$startGlobals as $key => $value ) {
34 unset( $GLOBALS[$key] );
38 public function provideExistingKeysAndNewValues() {
40 foreach ( array_keys( self
::$startGlobals ) as $key ) {
41 $providedArray[] = [ $key, 'newValue' ];
42 $providedArray[] = [ $key, [ 'newValue' ] ];
44 return $providedArray;
48 * @dataProvider provideExistingKeysAndNewValues
50 * @covers MediaWikiTestCase::setMwGlobals
51 * @covers MediaWikiTestCase::tearDown
53 public function testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
54 $this->setMwGlobals( $globalKey, $newValue );
58 'Global failed to correctly set'
64 self
::$startGlobals[$globalKey],
66 'Global failed to be restored on tearDown'
71 * @covers MediaWikiTestCase::setMwGlobals
72 * @covers MediaWikiTestCase::tearDown
74 public function testSetNonExistentGlobalsAreUnsetOnTearDown() {
75 $globalKey = 'abcdefg1234567';
76 $this->setMwGlobals( $globalKey, true );
79 'Global failed to correctly set'
85 isset( $GLOBALS[$globalKey] ),
86 'Global failed to be correctly unset'
90 public function testOverrideMwServices() {
91 $initialServices = MediaWikiServices
::getInstance();
93 $this->overrideMwServices();
94 $this->assertNotSame( $initialServices, MediaWikiServices
::getInstance() );
97 public function testSetService() {
98 $initialServices = MediaWikiServices
::getInstance();
99 $initialService = $initialServices->getDBLoadBalancer();
100 $mockService = $this->getMockBuilder( LoadBalancer
::class )
101 ->disableOriginalConstructor()->getMock();
103 $this->setService( 'DBLoadBalancer', $mockService );
104 $this->assertNotSame(
106 MediaWikiServices
::getInstance()->getDBLoadBalancer()
108 $this->assertSame( $mockService, MediaWikiServices
::getInstance()->getDBLoadBalancer() );
112 * @covers MediaWikiTestCase::setLogger
113 * @covers MediaWikiTestCase::restoreLoggers
115 public function testLoggersAreRestoredOnTearDown_replacingExistingLogger() {
116 $logger1 = LoggerFactory
::getInstance( 'foo' );
117 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
118 $logger2 = LoggerFactory
::getInstance( 'foo' );
120 $logger3 = LoggerFactory
::getInstance( 'foo' );
122 $this->assertSame( $logger1, $logger3 );
123 $this->assertNotSame( $logger1, $logger2 );
127 * @covers MediaWikiTestCase::setLogger
128 * @covers MediaWikiTestCase::restoreLoggers
130 public function testLoggersAreRestoredOnTearDown_replacingNonExistingLogger() {
131 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
132 $logger1 = LoggerFactory
::getInstance( 'foo' );
134 $logger2 = LoggerFactory
::getInstance( 'foo' );
136 $this->assertNotSame( $logger1, $logger2 );
137 $this->assertInstanceOf( \Psr\Log\LoggerInterface
::class, $logger2 );
141 * @covers MediaWikiTestCase::setLogger
142 * @covers MediaWikiTestCase::restoreLoggers
144 public function testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice() {
145 $logger1 = LoggerFactory
::getInstance( 'baz' );
146 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
147 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
149 $logger2 = LoggerFactory
::getInstance( 'baz' );
151 $this->assertSame( $logger1, $logger2 );
155 * @covers MediaWikiTestCase::setupDatabaseWithTestPrefix
156 * @covers MediaWikiTestCase::copyTestData
158 public function testCopyTestData() {
159 $this->markTestSkippedIfDbType( 'sqlite' );
161 $this->tablesUsed
[] = 'objectcache';
164 [ 'keyname' => __METHOD__
, 'value' => 'TEST', 'exptime' => $this->db
->timestamp( 11 ) ],
168 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
169 $lb = $lbFactory->newMainLB();
170 $db = $lb->getConnection( DB_REPLICA
);
173 $this->assertNotSame( $this->db
, $db );
175 // Make sure the DB connection has the fake table clones and the fake table prefix
176 MediaWikiTestCase
::setupDatabaseWithTestPrefix( $db, $this->dbPrefix(), false );
178 $this->assertSame( $this->db
->tablePrefix(), $db->tablePrefix(), 'tablePrefix' );
180 // Make sure the DB connection has all the test data
181 $this->copyTestData( $this->db
, $db );
183 $value = $db->selectField( 'objectcache', 'value', [ 'keyname' => __METHOD__
], __METHOD__
);
184 $this->assertSame( 'TEST', $value, 'Copied Data' );
187 public function testResetServices() {
188 $services = MediaWikiServices
::getInstance();
190 // override a service instance
191 $myReadOnlyMode = $this->getMockBuilder( ReadOnlyMode
::class )
192 ->disableOriginalConstructor()
194 $this->setService( 'ReadOnlyMode', $myReadOnlyMode );
197 $this->assertSame( $myReadOnlyMode, $services->getService( 'ReadOnlyMode' ) );
199 // define a custom service
200 $services->defineService(
201 '_TEST_ResetService_Dummy',
202 function ( MediaWikiServices
$services ) {
203 $conf = $services->getMainConfig();
204 return (object)[ 'lang' => $conf->get( 'LanguageCode' ) ];
209 $lang = $services->getMainConfig()->get( 'LanguageCode' );
210 $dummy = $services->getService( '_TEST_ResetService_Dummy' );
211 $this->assertSame( $lang, $dummy->lang
);
213 // the actual test: change config, reset services.
214 $this->setMwGlobals( 'wgLanguageCode', 'qqx' );
215 $this->resetServices();
217 // the overridden service instance should still be there
218 $this->assertSame( $myReadOnlyMode, $services->getService( 'ReadOnlyMode' ) );
220 // our custom service should have been re-created with the new language code
221 $dummy2 = $services->getService( '_TEST_ResetService_Dummy' );
222 $this->assertNotSame( $dummy2, $dummy );
223 $this->assertSame( 'qqx', $dummy2->lang
);