MediaWiki REL1_32
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->hideDeprecated( 'MediaWikiTestCase::stashMwGlobals' );
76 $this->stashMwGlobals( $globalKey );
77 $GLOBALS[$globalKey] = $newValue;
78 $this->assertEquals(
79 $newValue,
80 $GLOBALS[$globalKey],
81 'Global failed to correctly set'
82 );
83
84 $this->tearDown();
85
86 $this->assertEquals(
87 self::$startGlobals[$globalKey],
88 $GLOBALS[$globalKey],
89 'Global failed to be restored on tearDown'
90 );
91 }
92
98 $globalKey = 'abcdefg1234567';
99 $this->setMwGlobals( $globalKey, true );
100 $this->assertTrue(
101 $GLOBALS[$globalKey],
102 'Global failed to correctly set'
103 );
104
105 $this->tearDown();
106
107 $this->assertFalse(
108 isset( $GLOBALS[$globalKey] ),
109 'Global failed to be correctly unset'
110 );
111 }
112
113 public function testOverrideMwServices() {
114 $initialServices = MediaWikiServices::getInstance();
115
116 $this->overrideMwServices();
117 $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
118 }
119
120 public function testSetService() {
121 $initialServices = MediaWikiServices::getInstance();
122 $initialService = $initialServices->getDBLoadBalancer();
123 $mockService = $this->getMockBuilder( LoadBalancer::class )
124 ->disableOriginalConstructor()->getMock();
125
126 $this->setService( 'DBLoadBalancer', $mockService );
127 $this->assertNotSame(
128 $initialService,
129 MediaWikiServices::getInstance()->getDBLoadBalancer()
130 );
131 $this->assertSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
132 }
133
139 $logger1 = LoggerFactory::getInstance( 'foo' );
140 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
141 $logger2 = LoggerFactory::getInstance( 'foo' );
142 $this->tearDown();
143 $logger3 = LoggerFactory::getInstance( 'foo' );
144
145 $this->assertSame( $logger1, $logger3 );
146 $this->assertNotSame( $logger1, $logger2 );
147 }
148
154 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
155 $logger1 = LoggerFactory::getInstance( 'foo' );
156 $this->tearDown();
157 $logger2 = LoggerFactory::getInstance( 'foo' );
158
159 $this->assertNotSame( $logger1, $logger2 );
160 $this->assertInstanceOf( \Psr\Log\LoggerInterface::class, $logger2 );
161 }
162
168 $logger1 = LoggerFactory::getInstance( 'baz' );
169 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
170 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
171 $this->tearDown();
172 $logger2 = LoggerFactory::getInstance( 'baz' );
173
174 $this->assertSame( $logger1, $logger2 );
175 }
176}
$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.
hideDeprecated( $function)
Don't throw a warning if $function is deprecated and called later.
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.