Merge "MediaSearchWidget: Listen to "change" event to reposition"
[lhc/web/wiklou.git] / tests / phpunit / tests / MediaWikiTestCaseTest.php
1 <?php
2 use MediaWiki\Logger\LoggerFactory;
3 use MediaWiki\MediaWikiServices;
4 use Psr\Log\LoggerInterface;
5
6 /**
7 * @covers MediaWikiTestCase
8 * @author Addshore
9 */
10 class MediaWikiTestCaseTest extends MediaWikiTestCase {
11
12 private static $startGlobals = [
13 'MediaWikiTestCaseTestGLOBAL-ExistingString' => 'foo',
14 'MediaWikiTestCaseTestGLOBAL-ExistingStringEmpty' => '',
15 'MediaWikiTestCaseTestGLOBAL-ExistingArray' => [ 1, 'foo' => 'bar' ],
16 'MediaWikiTestCaseTestGLOBAL-ExistingArrayEmpty' => [],
17 ];
18
19 public static function setUpBeforeClass() {
20 parent::setUpBeforeClass();
21 foreach ( self::$startGlobals as $key => $value ) {
22 $GLOBALS[$key] = $value;
23 }
24 }
25
26 public static function tearDownAfterClass() {
27 parent::tearDownAfterClass();
28 foreach ( self::$startGlobals as $key => $value ) {
29 unset( $GLOBALS[$key] );
30 }
31 }
32
33 public function provideExistingKeysAndNewValues() {
34 $providedArray = [];
35 foreach ( array_keys( self::$startGlobals ) as $key ) {
36 $providedArray[] = [ $key, 'newValue' ];
37 $providedArray[] = [ $key, [ 'newValue' ] ];
38 }
39 return $providedArray;
40 }
41
42 /**
43 * @dataProvider provideExistingKeysAndNewValues
44 *
45 * @covers MediaWikiTestCase::setMwGlobals
46 * @covers MediaWikiTestCase::tearDown
47 */
48 public function testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
49 $this->setMwGlobals( $globalKey, $newValue );
50 $this->assertEquals(
51 $newValue,
52 $GLOBALS[$globalKey],
53 'Global failed to correctly set'
54 );
55
56 $this->tearDown();
57
58 $this->assertEquals(
59 self::$startGlobals[$globalKey],
60 $GLOBALS[$globalKey],
61 'Global failed to be restored on tearDown'
62 );
63 }
64
65 /**
66 * @dataProvider provideExistingKeysAndNewValues
67 *
68 * @covers MediaWikiTestCase::stashMwGlobals
69 * @covers MediaWikiTestCase::tearDown
70 */
71 public function testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
72 $this->stashMwGlobals( $globalKey );
73 $GLOBALS[$globalKey] = $newValue;
74 $this->assertEquals(
75 $newValue,
76 $GLOBALS[$globalKey],
77 'Global failed to correctly set'
78 );
79
80 $this->tearDown();
81
82 $this->assertEquals(
83 self::$startGlobals[$globalKey],
84 $GLOBALS[$globalKey],
85 'Global failed to be restored on tearDown'
86 );
87 }
88
89 /**
90 * @covers MediaWikiTestCase::stashMwGlobals
91 * @covers MediaWikiTestCase::tearDown
92 */
93 public function testSetNonExistentGlobalsAreUnsetOnTearDown() {
94 $globalKey = 'abcdefg1234567';
95 $this->setMwGlobals( $globalKey, true );
96 $this->assertTrue(
97 $GLOBALS[$globalKey],
98 'Global failed to correctly set'
99 );
100
101 $this->tearDown();
102
103 $this->assertFalse(
104 isset( $GLOBALS[$globalKey] ),
105 'Global failed to be correctly unset'
106 );
107 }
108
109 public function testOverrideMwServices() {
110 $initialServices = MediaWikiServices::getInstance();
111
112 $this->overrideMwServices();
113 $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
114
115 $this->tearDown();
116 $this->assertSame( $initialServices, MediaWikiServices::getInstance() );
117 }
118
119 public function testSetService() {
120 $initialServices = MediaWikiServices::getInstance();
121 $initialService = $initialServices->getDBLoadBalancer();
122 $mockService = $this->getMockBuilder( LoadBalancer::class )
123 ->disableOriginalConstructor()->getMock();
124
125 $this->setService( 'DBLoadBalancer', $mockService );
126 $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
127 $this->assertNotSame(
128 $initialService,
129 MediaWikiServices::getInstance()->getDBLoadBalancer()
130 );
131 $this->assertSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
132
133 $this->tearDown();
134 $this->assertSame( $initialServices, MediaWikiServices::getInstance() );
135 $this->assertNotSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
136 $this->assertSame( $initialService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
137 }
138
139 /**
140 * @covers MediaWikiTestCase::setLogger
141 * @covers MediaWikiTestCase::restoreLoggers
142 */
143 public function testLoggersAreRestoredOnTearDown_replacingExistingLogger() {
144 $logger1 = LoggerFactory::getInstance( 'foo' );
145 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
146 $logger2 = LoggerFactory::getInstance( 'foo' );
147 $this->tearDown();
148 $logger3 = LoggerFactory::getInstance( 'foo' );
149
150 $this->assertSame( $logger1, $logger3 );
151 $this->assertNotSame( $logger1, $logger2 );
152 }
153
154 /**
155 * @covers MediaWikiTestCase::setLogger
156 * @covers MediaWikiTestCase::restoreLoggers
157 */
158 public function testLoggersAreRestoredOnTearDown_replacingNonExistingLogger() {
159 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
160 $logger1 = LoggerFactory::getInstance( 'foo' );
161 $this->tearDown();
162 $logger2 = LoggerFactory::getInstance( 'foo' );
163
164 $this->assertNotSame( $logger1, $logger2 );
165 $this->assertInstanceOf( '\Psr\Log\LoggerInterface', $logger2 );
166 }
167
168 /**
169 * @covers MediaWikiTestCase::setLogger
170 * @covers MediaWikiTestCase::restoreLoggers
171 */
172 public function testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice() {
173 $logger1 = LoggerFactory::getInstance( 'baz' );
174 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
175 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
176 $this->tearDown();
177 $logger2 = LoggerFactory::getInstance( 'baz' );
178
179 $this->assertSame( $logger1, $logger2 );
180 }
181 }