MediaWiki REL1_31
MediaWikiTestCaseTest.php
Go to the documentation of this file.
1<?php
4use Psr\Log\LoggerInterface;
6
14
15 private static $startGlobals = [
16 'MediaWikiTestCaseTestGLOBAL-ExistingString' => 'foo',
17 'MediaWikiTestCaseTestGLOBAL-ExistingStringEmpty' => '',
18 'MediaWikiTestCaseTestGLOBAL-ExistingArray' => [ 1, 'foo' => 'bar' ],
19 'MediaWikiTestCaseTestGLOBAL-ExistingArrayEmpty' => [],
20 ];
21
22 public static function setUpBeforeClass() {
23 parent::setUpBeforeClass();
24 foreach ( self::$startGlobals as $key => $value ) {
25 $GLOBALS[$key] = $value;
26 }
27 }
28
29 public static function tearDownAfterClass() {
30 parent::tearDownAfterClass();
31 foreach ( self::$startGlobals as $key => $value ) {
32 unset( $GLOBALS[$key] );
33 }
34 }
35
37 $providedArray = [];
38 foreach ( array_keys( self::$startGlobals ) as $key ) {
39 $providedArray[] = [ $key, 'newValue' ];
40 $providedArray[] = [ $key, [ 'newValue' ] ];
41 }
42 return $providedArray;
43 }
44
51 public function testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
52 $this->setMwGlobals( $globalKey, $newValue );
53 $this->assertEquals(
54 $newValue,
55 $GLOBALS[$globalKey],
56 'Global failed to correctly set'
57 );
58
59 $this->tearDown();
60
61 $this->assertEquals(
62 self::$startGlobals[$globalKey],
63 $GLOBALS[$globalKey],
64 'Global failed to be restored on tearDown'
65 );
66 }
67
74 public function testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
75 $this->stashMwGlobals( $globalKey );
76 $GLOBALS[$globalKey] = $newValue;
77 $this->assertEquals(
78 $newValue,
79 $GLOBALS[$globalKey],
80 'Global failed to correctly set'
81 );
82
83 $this->tearDown();
84
85 $this->assertEquals(
86 self::$startGlobals[$globalKey],
87 $GLOBALS[$globalKey],
88 'Global failed to be restored on tearDown'
89 );
90 }
91
97 $globalKey = 'abcdefg1234567';
98 $this->setMwGlobals( $globalKey, true );
99 $this->assertTrue(
100 $GLOBALS[$globalKey],
101 'Global failed to correctly set'
102 );
103
104 $this->tearDown();
105
106 $this->assertFalse(
107 isset( $GLOBALS[$globalKey] ),
108 'Global failed to be correctly unset'
109 );
110 }
111
112 public function testOverrideMwServices() {
113 $initialServices = MediaWikiServices::getInstance();
114
115 $this->overrideMwServices();
116 $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
117
118 $this->tearDown();
119 $this->assertSame( $initialServices, MediaWikiServices::getInstance() );
120 }
121
122 public function testSetService() {
123 $initialServices = MediaWikiServices::getInstance();
124 $initialService = $initialServices->getDBLoadBalancer();
125 $mockService = $this->getMockBuilder( LoadBalancer::class )
126 ->disableOriginalConstructor()->getMock();
127
128 $this->setService( 'DBLoadBalancer', $mockService );
129 $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
130 $this->assertNotSame(
131 $initialService,
132 MediaWikiServices::getInstance()->getDBLoadBalancer()
133 );
134 $this->assertSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
135
136 $this->tearDown();
137 $this->assertSame( $initialServices, MediaWikiServices::getInstance() );
138 $this->assertNotSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
139 $this->assertSame( $initialService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
140 }
141
147 $logger1 = LoggerFactory::getInstance( 'foo' );
148 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
149 $logger2 = LoggerFactory::getInstance( 'foo' );
150 $this->tearDown();
151 $logger3 = LoggerFactory::getInstance( 'foo' );
152
153 $this->assertSame( $logger1, $logger3 );
154 $this->assertNotSame( $logger1, $logger2 );
155 }
156
162 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
163 $logger1 = LoggerFactory::getInstance( 'foo' );
164 $this->tearDown();
165 $logger2 = LoggerFactory::getInstance( 'foo' );
166
167 $this->assertNotSame( $logger1, $logger2 );
168 $this->assertInstanceOf( \Psr\Log\LoggerInterface::class, $logger2 );
169 }
170
176 $logger1 = LoggerFactory::getInstance( 'baz' );
177 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
178 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
179 $this->tearDown();
180 $logger2 = LoggerFactory::getInstance( 'baz' );
181
182 $this->assertSame( $logger1, $logger2 );
183 }
184}
$GLOBALS['IP']
MediaWikiTestCase MediaWikiTestCaseTest.
testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
testLoggersAreRestoredOnTearDown_replacingNonExistingLogger()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue)
provideExistingKeysAndNewValues
testLoggersAreRestoredOnTearDown_replacingExistingLogger()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
testSetNonExistentGlobalsAreUnsetOnTearDown()
MediaWikiTestCase::stashMwGlobals MediaWikiTestCase::tearDown.
testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue)
provideExistingKeysAndNewValues
overrideMwServices(Config $configOverrides=null, array $services=[])
Stashes the global instance of MediaWikiServices, and installs a new one, allowing test cases to over...
setLogger( $channel, LoggerInterface $logger)
Sets the logger for a specified channel, for the duration of the test.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown.
stashMwGlobals( $globalKeys)
Stashes the global, will be restored in tearDown()
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Database connection, tracking, load balancing, and transaction manager for a cluster.