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 * @covers MediaWikiTestCase::setMwGlobals
71 * @covers MediaWikiTestCase::tearDown
73 public function testSetNonExistentGlobalsAreUnsetOnTearDown() {
74 $globalKey = 'abcdefg1234567';
75 $this->setMwGlobals( $globalKey, true );
78 'Global failed to correctly set'
84 isset( $GLOBALS[$globalKey] ),
85 'Global failed to be correctly unset'
89 public function testOverrideMwServices() {
90 $initialServices = MediaWikiServices
::getInstance();
92 $this->overrideMwServices();
93 $this->assertNotSame( $initialServices, MediaWikiServices
::getInstance() );
96 public function testSetService() {
97 $initialServices = MediaWikiServices
::getInstance();
98 $initialService = $initialServices->getDBLoadBalancer();
99 $mockService = $this->getMockBuilder( LoadBalancer
::class )
100 ->disableOriginalConstructor()->getMock();
102 $this->setService( 'DBLoadBalancer', $mockService );
103 $this->assertNotSame(
105 MediaWikiServices
::getInstance()->getDBLoadBalancer()
107 $this->assertSame( $mockService, MediaWikiServices
::getInstance()->getDBLoadBalancer() );
111 * @covers MediaWikiTestCase::setLogger
112 * @covers MediaWikiTestCase::restoreLoggers
114 public function testLoggersAreRestoredOnTearDown_replacingExistingLogger() {
115 $logger1 = LoggerFactory
::getInstance( 'foo' );
116 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
117 $logger2 = LoggerFactory
::getInstance( 'foo' );
119 $logger3 = LoggerFactory
::getInstance( 'foo' );
121 $this->assertSame( $logger1, $logger3 );
122 $this->assertNotSame( $logger1, $logger2 );
126 * @covers MediaWikiTestCase::setLogger
127 * @covers MediaWikiTestCase::restoreLoggers
129 public function testLoggersAreRestoredOnTearDown_replacingNonExistingLogger() {
130 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
131 $logger1 = LoggerFactory
::getInstance( 'foo' );
133 $logger2 = LoggerFactory
::getInstance( 'foo' );
135 $this->assertNotSame( $logger1, $logger2 );
136 $this->assertInstanceOf( \Psr\Log\LoggerInterface
::class, $logger2 );
140 * @covers MediaWikiTestCase::setLogger
141 * @covers MediaWikiTestCase::restoreLoggers
143 public function testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice() {
144 $logger1 = LoggerFactory
::getInstance( 'baz' );
145 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
146 $this->setLogger( 'foo', $this->createMock( LoggerInterface
::class ) );
148 $logger2 = LoggerFactory
::getInstance( 'baz' );
150 $this->assertSame( $logger1, $logger2 );
154 * @covers MediaWikiTestCase::setupDatabaseWithTestPrefix
155 * @covers MediaWikiTestCase::copyTestData
157 public function testCopyTestData() {
158 $this->markTestSkippedIfDbType( 'sqlite' );
160 $this->tablesUsed
[] = 'objectcache';
163 [ 'keyname' => __METHOD__
, 'value' => 'TEST', 'exptime' => $this->db
->timestamp( 11 ) ],
167 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
168 $lb = $lbFactory->newMainLB();
169 $db = $lb->getConnection( DB_REPLICA
, DBO_TRX
);
172 $this->assertNotSame( $this->db
, $db );
174 // Make sure the DB connection has the fake table clones and the fake table prefix
175 MediaWikiTestCase
::setupDatabaseWithTestPrefix( $db, $this->dbPrefix(), false );
177 $this->assertSame( $this->db
->tablePrefix(), $db->tablePrefix(), 'tablePrefix' );
179 // Make sure the DB connection has all the test data
180 $this->copyTestData( $this->db
, $db );
182 $value = $db->selectField( 'objectcache', 'value', [ 'keyname' => __METHOD__
], __METHOD__
);
183 $this->assertSame( 'TEST', $value, 'Copied Data' );