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
14 class MediaWikiTestCaseTest
extends MediaWikiTestCase
{
16 private static $startGlobals = [
17 'MediaWikiTestCaseTestGLOBAL-ExistingString' => 'foo',
18 'MediaWikiTestCaseTestGLOBAL-ExistingStringEmpty' => '',
19 'MediaWikiTestCaseTestGLOBAL-ExistingArray' => [ 1, 'foo' => 'bar' ],
20 'MediaWikiTestCaseTestGLOBAL-ExistingArrayEmpty' => [],
23 public static function setUpBeforeClass() {
24 parent
::setUpBeforeClass();
25 foreach ( self
::$startGlobals as $key => $value ) {
26 $GLOBALS[$key] = $value;
30 public static function tearDownAfterClass() {
31 parent
::tearDownAfterClass();
32 foreach ( self
::$startGlobals as $key => $value ) {
33 unset( $GLOBALS[$key] );
37 public function provideExistingKeysAndNewValues() {
39 foreach ( array_keys( self
::$startGlobals ) as $key ) {
40 $providedArray[] = [ $key, 'newValue' ];
41 $providedArray[] = [ $key, [ 'newValue' ] ];
43 return $providedArray;
47 * @dataProvider provideExistingKeysAndNewValues
49 * @covers MediaWikiTestCase::setMwGlobals
50 * @covers MediaWikiTestCase::tearDown
52 public function testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
53 $this->setMwGlobals( $globalKey, $newValue );
57 'Global failed to correctly set'
63 self
::$startGlobals[$globalKey],
65 'Global failed to be restored on tearDown'
70 * @dataProvider provideExistingKeysAndNewValues
72 * @covers MediaWikiTestCase::stashMwGlobals
73 * @covers MediaWikiTestCase::tearDown
75 public function testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
76 $this->hideDeprecated( 'MediaWikiTestCase::stashMwGlobals' );
77 $this->stashMwGlobals( $globalKey );
78 $GLOBALS[$globalKey] = $newValue;
82 'Global failed to correctly set'
88 self
::$startGlobals[$globalKey],
90 'Global failed to be restored on tearDown'
95 * @covers MediaWikiTestCase::stashMwGlobals
96 * @covers MediaWikiTestCase::tearDown
98 public function testSetNonExistentGlobalsAreUnsetOnTearDown() {
99 $globalKey = 'abcdefg1234567';
100 $this->setMwGlobals( $globalKey, true );
102 $GLOBALS[$globalKey],
103 'Global failed to correctly set'
109 isset( $GLOBALS[$globalKey] ),
110 'Global failed to be correctly unset'
114 public function testOverrideMwServices() {
115 $initialServices = MediaWikiServices
::getInstance();
117 $this->overrideMwServices();
118 $this->assertNotSame( $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(
130 MediaWikiServices
::getInstance()->getDBLoadBalancer()
132 $this->assertSame( $mockService, MediaWikiServices
::getInstance()->getDBLoadBalancer() );
136 * @covers MediaWikiTestCase::setLogger
137 * @covers MediaWikiTestCase::restoreLoggers
139 public function testLoggersAreRestoredOnTearDown_replacingExistingLogger() {
140 $logger1 = LoggerFactory
::getInstance( 'foo' );
141 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
142 $logger2 = LoggerFactory
::getInstance( 'foo' );
144 $logger3 = LoggerFactory
::getInstance( 'foo' );
146 $this->assertSame( $logger1, $logger3 );
147 $this->assertNotSame( $logger1, $logger2 );
151 * @covers MediaWikiTestCase::setLogger
152 * @covers MediaWikiTestCase::restoreLoggers
154 public function testLoggersAreRestoredOnTearDown_replacingNonExistingLogger() {
155 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
156 $logger1 = LoggerFactory
::getInstance( 'foo' );
158 $logger2 = LoggerFactory
::getInstance( 'foo' );
160 $this->assertNotSame( $logger1, $logger2 );
161 $this->assertInstanceOf( \Psr\Log\LoggerInterface
::class, $logger2 );
165 * @covers MediaWikiTestCase::setLogger
166 * @covers MediaWikiTestCase::restoreLoggers
168 public function testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice() {
169 $logger1 = LoggerFactory
::getInstance( 'baz' );
170 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
171 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
173 $logger2 = LoggerFactory
::getInstance( 'baz' );
175 $this->assertSame( $logger1, $logger2 );
179 * @covers MediaWikiTestCase::setupDatabaseWithTestPrefix
180 * @covers MediaWikiTestCase::copyTestData
182 public function testCopyTestData() {
183 $this->markTestSkippedIfDbType( 'sqlite' );
185 $this->tablesUsed
[] = 'objectcache';
188 [ 'keyname' => __METHOD__
, 'value' => 'TEST', 'exptime' => $this->db
->timestamp( 11 ) ],
192 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
193 $lb = $lbFactory->newMainLB();
194 $db = $lb->getConnection( DB_REPLICA
, DBO_TRX
);
197 $this->assertNotSame( $this->db
, $db );
199 // Make sure the DB connection has the fake table clones and the fake table prefix
200 MediaWikiTestCase
::setupDatabaseWithTestPrefix( $db, $this->dbPrefix(), false );
202 $this->assertSame( $this->db
->tablePrefix(), $db->tablePrefix(), 'tablePrefix' );
204 // Make sure the DB connection has all the test data
205 $this->copyTestData( $this->db
, $db );
207 $value = $db->selectField( 'objectcache', 'value', [ 'keyname' => __METHOD__
], __METHOD__
);
208 $this->assertSame( 'TEST', $value, 'Copied Data' );