Merge "Gallery: Use intrinsic width for gallery to center caption"
[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 const GLOBAL_KEY_NONEXISTING = 'MediaWikiTestCaseTestGLOBAL-NONExisting';
13
14 private static $startGlobals = [
15 'MediaWikiTestCaseTestGLOBAL-ExistingString' => 'foo',
16 'MediaWikiTestCaseTestGLOBAL-ExistingStringEmpty' => '',
17 'MediaWikiTestCaseTestGLOBAL-ExistingArray' => [ 1, 'foo' => 'bar' ],
18 'MediaWikiTestCaseTestGLOBAL-ExistingArrayEmpty' => [],
19 ];
20
21 public static function setUpBeforeClass() {
22 parent::setUpBeforeClass();
23 foreach ( self::$startGlobals as $key => $value ) {
24 $GLOBALS[$key] = $value;
25 }
26 }
27
28 public static function tearDownAfterClass() {
29 parent::tearDownAfterClass();
30 foreach ( self::$startGlobals as $key => $value ) {
31 unset( $GLOBALS[$key] );
32 }
33 }
34
35 public function provideExistingKeysAndNewValues() {
36 $providedArray = [];
37 foreach ( array_keys( self::$startGlobals ) as $key ) {
38 $providedArray[] = [ $key, 'newValue' ];
39 $providedArray[] = [ $key, [ 'newValue' ] ];
40 }
41 return $providedArray;
42 }
43
44 /**
45 * @dataProvider provideExistingKeysAndNewValues
46 *
47 * @covers MediaWikiTestCase::setMwGlobals
48 * @covers MediaWikiTestCase::tearDown
49 */
50 public function testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
51 $this->setMwGlobals( $globalKey, $newValue );
52 $this->assertEquals(
53 $newValue,
54 $GLOBALS[$globalKey],
55 'Global failed to correctly set'
56 );
57
58 $this->tearDown();
59
60 $this->assertEquals(
61 self::$startGlobals[$globalKey],
62 $GLOBALS[$globalKey],
63 'Global failed to be restored on tearDown'
64 );
65 }
66
67 /**
68 * @dataProvider provideExistingKeysAndNewValues
69 *
70 * @covers MediaWikiTestCase::stashMwGlobals
71 * @covers MediaWikiTestCase::tearDown
72 */
73 public function testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
74 $this->stashMwGlobals( $globalKey );
75 $GLOBALS[$globalKey] = $newValue;
76 $this->assertEquals(
77 $newValue,
78 $GLOBALS[$globalKey],
79 'Global failed to correctly set'
80 );
81
82 $this->tearDown();
83
84 $this->assertEquals(
85 self::$startGlobals[$globalKey],
86 $GLOBALS[$globalKey],
87 'Global failed to be restored on tearDown'
88 );
89 }
90
91 /**
92 * @covers MediaWikiTestCase::stashMwGlobals
93 */
94 public function testExceptionThrownWhenStashingNonExistentGlobals() {
95 $this->setExpectedException(
96 'Exception',
97 'Global with key ' . self::GLOBAL_KEY_NONEXISTING . ' doesn\'t exist and cant be stashed'
98 );
99
100 $this->stashMwGlobals( self::GLOBAL_KEY_NONEXISTING );
101 }
102
103 public function testOverrideMwServices() {
104 $initialServices = MediaWikiServices::getInstance();
105
106 $this->overrideMwServices();
107 $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
108
109 $this->tearDown();
110 $this->assertSame( $initialServices, MediaWikiServices::getInstance() );
111 }
112
113 public function testSetService() {
114 $initialServices = MediaWikiServices::getInstance();
115 $initialService = $initialServices->getDBLoadBalancer();
116 $mockService = $this->getMockBuilder( LoadBalancer::class )
117 ->disableOriginalConstructor()->getMock();
118
119 $this->setService( 'DBLoadBalancer', $mockService );
120 $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
121 $this->assertNotSame(
122 $initialService,
123 MediaWikiServices::getInstance()->getDBLoadBalancer()
124 );
125 $this->assertSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
126
127 $this->tearDown();
128 $this->assertSame( $initialServices, MediaWikiServices::getInstance() );
129 $this->assertNotSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
130 $this->assertSame( $initialService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
131 }
132
133 /**
134 * @covers MediaWikiTestCase::setLogger
135 * @covers MediaWikiTestCase::restoreLogger
136 */
137 public function testLoggersAreRestoredOnTearDown() {
138 // replacing an existing logger
139 $logger1 = LoggerFactory::getInstance( 'foo' );
140 $this->setLogger( 'foo', $this->getMock( 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 // replacing a non-existing logger
149 $this->setLogger( 'foo', $this->getMock( LoggerInterface::class ) );
150 $logger1 = LoggerFactory::getInstance( 'bar' );
151 $this->tearDown();
152 $logger2 = LoggerFactory::getInstance( 'bar' );
153
154 $this->assertNotSame( $logger1, $logger2 );
155 $this->assertInstanceOf( '\Psr\Log\LoggerInterface', $logger2 );
156
157 // replacing same logger twice
158 $logger1 = LoggerFactory::getInstance( 'baz' );
159 $this->setLogger( 'foo', $this->getMock( LoggerInterface::class ) );
160 $this->setLogger( 'foo', $this->getMock( LoggerInterface::class ) );
161 $this->tearDown();
162 $logger2 = LoggerFactory::getInstance( 'baz' );
163
164 $this->assertSame( $logger1, $logger2 );
165 }
166 }