MediaWiki  1.30.0
MediaWikiTestCaseTest.php
Go to the documentation of this file.
1 <?php
4 use Psr\Log\LoggerInterface;
5 
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 
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 
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 
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 
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 
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 
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 }
MediaWikiTestCaseTest\testLoggersAreRestoredOnTearDown_replacingExistingLogger
testLoggersAreRestoredOnTearDown_replacingExistingLogger()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
Definition: MediaWikiTestCaseTest.php:143
MediaWikiTestCase\stashMwGlobals
stashMwGlobals( $globalKeys)
Stashes the global, will be restored in tearDown()
Definition: MediaWikiTestCase.php:724
MediaWikiTestCaseTest\testLoggersAreRestoredOnTearDown_replacingNonExistingLogger
testLoggersAreRestoredOnTearDown_replacingNonExistingLogger()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
Definition: MediaWikiTestCaseTest.php:158
MediaWikiTestCaseTest\provideExistingKeysAndNewValues
provideExistingKeysAndNewValues()
Definition: MediaWikiTestCaseTest.php:33
MediaWikiTestCaseTest\testSetNonExistentGlobalsAreUnsetOnTearDown
testSetNonExistentGlobalsAreUnsetOnTearDown()
MediaWikiTestCase::stashMwGlobals MediaWikiTestCase::tearDown.
Definition: MediaWikiTestCaseTest.php:93
MediaWikiTestCaseTest\$startGlobals
static $startGlobals
Definition: MediaWikiTestCaseTest.php:12
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MediaWikiTestCaseTest\tearDownAfterClass
static tearDownAfterClass()
Definition: MediaWikiTestCaseTest.php:26
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWikiTestCase\overrideMwServices
overrideMwServices(Config $configOverrides=null, array $services=[])
Stashes the global instance of MediaWikiServices, and installs a new one, allowing test cases to over...
Definition: MediaWikiTestCase.php:839
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:672
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
$GLOBALS
$GLOBALS['wgAutoloadClasses']['LocalisationUpdate']
Definition: Autoload.php:10
$value
$value
Definition: styleTest.css.php:45
MediaWikiTestCaseTest\testSetGlobalsAreRestoredOnTearDown
testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue)
provideExistingKeysAndNewValues
Definition: MediaWikiTestCaseTest.php:48
MediaWikiTestCaseTest
MediaWikiTestCase.
Definition: MediaWikiTestCaseTest.php:10
MediaWikiTestCaseTest\testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice
testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
Definition: MediaWikiTestCaseTest.php:172
MediaWikiTestCase\setLogger
setLogger( $channel, LoggerInterface $logger)
Sets the logger for a specified channel, for the duration of the test.
Definition: MediaWikiTestCase.php:906
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
MediaWikiTestCaseTest\testOverrideMwServices
testOverrideMwServices()
Definition: MediaWikiTestCaseTest.php:109
LoggerFactory
MediaWiki Logger LoggerFactory implements a PSR[0] compatible message logging system Named Psr Log LoggerInterface instances can be obtained from the MediaWiki Logger LoggerFactory::getInstance() static method. MediaWiki\Logger\LoggerFactory expects a class implementing the MediaWiki\Logger\Spi interface to act as a factory for new Psr\Log\LoggerInterface instances. The "Spi" in MediaWiki\Logger\Spi stands for "service provider interface". An SPI is an API intended to be implemented or extended by a third party. This software design pattern is intended to enable framework extension and replaceable components. It is specifically used in the MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging implementations to be easily integrated with MediaWiki. The service provider interface allows the backend logging library to be implemented in multiple ways. The $wgMWLoggerDefaultSpi global provides the classname of the default MediaWiki\Logger\Spi implementation to be loaded at runtime. This can either be the name of a class implementing the MediaWiki\Logger\Spi with a zero argument const ructor or a callable that will return an MediaWiki\Logger\Spi instance. Alternately the MediaWiki\Logger\LoggerFactory MediaWiki Logger LoggerFactory
Definition: logger.txt:5
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
MediaWikiTestCaseTest\testSetService
testSetService()
Definition: MediaWikiTestCaseTest.php:119
MediaWikiTestCaseTest\setUpBeforeClass
static setUpBeforeClass()
Definition: MediaWikiTestCaseTest.php:19
MediaWikiTestCase\setService
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown.
Definition: MediaWikiTestCase.php:622
MediaWikiTestCase\tearDown
tearDown()
Definition: MediaWikiTestCase.php:528
MediaWikiTestCaseTest\testStashedGlobalsAreRestoredOnTearDown
testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue)
provideExistingKeysAndNewValues
Definition: MediaWikiTestCaseTest.php:71