Special:Upload should not crash on failing previews
[mediawiki.git] / tests / phpunit / tests / MediaWikiTestCaseTest.php
blobedc81ff925f6fe7b9b1878a50f22467655fd1765
1 <?php
2 use MediaWiki\Logger\LoggerFactory;
3 use MediaWiki\MediaWikiServices;
4 use Psr\Log\LoggerInterface;
6 /**
7 * @covers MediaWikiTestCase
8 * @author Addshore
9 */
10 class MediaWikiTestCaseTest extends MediaWikiTestCase {
12 private static $startGlobals = [
13 'MediaWikiTestCaseTestGLOBAL-ExistingString' => 'foo',
14 'MediaWikiTestCaseTestGLOBAL-ExistingStringEmpty' => '',
15 'MediaWikiTestCaseTestGLOBAL-ExistingArray' => [ 1, 'foo' => 'bar' ],
16 'MediaWikiTestCaseTestGLOBAL-ExistingArrayEmpty' => [],
19 public static function setUpBeforeClass() {
20 parent::setUpBeforeClass();
21 foreach ( self::$startGlobals as $key => $value ) {
22 $GLOBALS[$key] = $value;
26 public static function tearDownAfterClass() {
27 parent::tearDownAfterClass();
28 foreach ( self::$startGlobals as $key => $value ) {
29 unset( $GLOBALS[$key] );
33 public function provideExistingKeysAndNewValues() {
34 $providedArray = [];
35 foreach ( array_keys( self::$startGlobals ) as $key ) {
36 $providedArray[] = [ $key, 'newValue' ];
37 $providedArray[] = [ $key, [ 'newValue' ] ];
39 return $providedArray;
42 /**
43 * @dataProvider provideExistingKeysAndNewValues
45 * @covers MediaWikiTestCase::setMwGlobals
46 * @covers MediaWikiTestCase::tearDown
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'
56 $this->tearDown();
58 $this->assertEquals(
59 self::$startGlobals[$globalKey],
60 $GLOBALS[$globalKey],
61 'Global failed to be restored on tearDown'
65 /**
66 * @dataProvider provideExistingKeysAndNewValues
68 * @covers MediaWikiTestCase::stashMwGlobals
69 * @covers MediaWikiTestCase::tearDown
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'
80 $this->tearDown();
82 $this->assertEquals(
83 self::$startGlobals[$globalKey],
84 $GLOBALS[$globalKey],
85 'Global failed to be restored on tearDown'
89 /**
90 * @covers MediaWikiTestCase::stashMwGlobals
91 * @covers MediaWikiTestCase::tearDown
93 public function testSetNonExistentGlobalsAreUnsetOnTearDown() {
94 $globalKey = 'abcdefg1234567';
95 $this->setMwGlobals( $globalKey, true );
96 $this->assertTrue(
97 $GLOBALS[$globalKey],
98 'Global failed to correctly set'
101 $this->tearDown();
103 $this->assertFalse(
104 isset( $GLOBALS[$globalKey] ),
105 'Global failed to be correctly unset'
109 public function testOverrideMwServices() {
110 $initialServices = MediaWikiServices::getInstance();
112 $this->overrideMwServices();
113 $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
115 $this->tearDown();
116 $this->assertSame( $initialServices, MediaWikiServices::getInstance() );
119 public function testSetService() {
120 $initialServices = MediaWikiServices::getInstance();
121 $initialService = $initialServices->getDBLoadBalancer();
122 $mockService = $this->getMockBuilder( LoadBalancer::class )
123 ->disableOriginalConstructor()->getMock();
125 $this->setService( 'DBLoadBalancer', $mockService );
126 $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
127 $this->assertNotSame(
128 $initialService,
129 MediaWikiServices::getInstance()->getDBLoadBalancer()
131 $this->assertSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
133 $this->tearDown();
134 $this->assertSame( $initialServices, MediaWikiServices::getInstance() );
135 $this->assertNotSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
136 $this->assertSame( $initialService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
140 * @covers MediaWikiTestCase::setLogger
141 * @covers MediaWikiTestCase::restoreLoggers
143 public function testLoggersAreRestoredOnTearDown_replacingExistingLogger() {
144 $logger1 = LoggerFactory::getInstance( 'foo' );
145 $this->setLogger( 'foo', $this->getMock( LoggerInterface::class ) );
146 $logger2 = LoggerFactory::getInstance( 'foo' );
147 $this->tearDown();
148 $logger3 = LoggerFactory::getInstance( 'foo' );
150 $this->assertSame( $logger1, $logger3 );
151 $this->assertNotSame( $logger1, $logger2 );
155 * @covers MediaWikiTestCase::setLogger
156 * @covers MediaWikiTestCase::restoreLoggers
158 public function testLoggersAreRestoredOnTearDown_replacingNonExistingLogger() {
159 $this->setLogger( 'foo', $this->getMock( LoggerInterface::class ) );
160 $logger1 = LoggerFactory::getInstance( 'foo' );
161 $this->tearDown();
162 $logger2 = LoggerFactory::getInstance( 'foo' );
164 $this->assertNotSame( $logger1, $logger2 );
165 $this->assertInstanceOf( '\Psr\Log\LoggerInterface', $logger2 );
169 * @covers MediaWikiTestCase::setLogger
170 * @covers MediaWikiTestCase::restoreLoggers
172 public function testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice() {
173 $logger1 = LoggerFactory::getInstance( 'baz' );
174 $this->setLogger( 'foo', $this->getMock( LoggerInterface::class ) );
175 $this->setLogger( 'foo', $this->getMock( LoggerInterface::class ) );
176 $this->tearDown();
177 $logger2 = LoggerFactory::getInstance( 'baz' );
179 $this->assertSame( $logger1, $logger2 );