MediaWiki  REL1_31
MediaWikiTestCaseTest.php
Go to the documentation of this file.
1 <?php
4 use 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 
36  public function provideExistingKeysAndNewValues() {
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->stashMwGlobals( $globalKey );
76  $GLOBALS[$globalKey] = $newValue;
77  $this->assertEquals(
78  $newValue,
79  $GLOBALS[$globalKey],
80  'Global failed to correctly set'
81  );
82 
83  $this->tearDown();
84 
85  $this->assertEquals(
86  self::$startGlobals[$globalKey],
87  $GLOBALS[$globalKey],
88  'Global failed to be restored on tearDown'
89  );
90  }
91 
97  $globalKey = 'abcdefg1234567';
98  $this->setMwGlobals( $globalKey, true );
99  $this->assertTrue(
100  $GLOBALS[$globalKey],
101  'Global failed to correctly set'
102  );
103 
104  $this->tearDown();
105 
106  $this->assertFalse(
107  isset( $GLOBALS[$globalKey] ),
108  'Global failed to be correctly unset'
109  );
110  }
111 
112  public function testOverrideMwServices() {
113  $initialServices = MediaWikiServices::getInstance();
114 
115  $this->overrideMwServices();
116  $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
117 
118  $this->tearDown();
119  $this->assertSame( $initialServices, MediaWikiServices::getInstance() );
120  }
121 
122  public function testSetService() {
123  $initialServices = MediaWikiServices::getInstance();
124  $initialService = $initialServices->getDBLoadBalancer();
125  $mockService = $this->getMockBuilder( LoadBalancer::class )
126  ->disableOriginalConstructor()->getMock();
127 
128  $this->setService( 'DBLoadBalancer', $mockService );
129  $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
130  $this->assertNotSame(
131  $initialService,
132  MediaWikiServices::getInstance()->getDBLoadBalancer()
133  );
134  $this->assertSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
135 
136  $this->tearDown();
137  $this->assertSame( $initialServices, MediaWikiServices::getInstance() );
138  $this->assertNotSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
139  $this->assertSame( $initialService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
140  }
141 
147  $logger1 = LoggerFactory::getInstance( 'foo' );
148  $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
149  $logger2 = LoggerFactory::getInstance( 'foo' );
150  $this->tearDown();
151  $logger3 = LoggerFactory::getInstance( 'foo' );
152 
153  $this->assertSame( $logger1, $logger3 );
154  $this->assertNotSame( $logger1, $logger2 );
155  }
156 
162  $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
163  $logger1 = LoggerFactory::getInstance( 'foo' );
164  $this->tearDown();
165  $logger2 = LoggerFactory::getInstance( 'foo' );
166 
167  $this->assertNotSame( $logger1, $logger2 );
168  $this->assertInstanceOf( \Psr\Log\LoggerInterface::class, $logger2 );
169  }
170 
176  $logger1 = LoggerFactory::getInstance( 'baz' );
177  $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
178  $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
179  $this->tearDown();
180  $logger2 = LoggerFactory::getInstance( 'baz' );
181 
182  $this->assertSame( $logger1, $logger2 );
183  }
184 }
MediaWikiTestCaseTest\testLoggersAreRestoredOnTearDown_replacingExistingLogger
testLoggersAreRestoredOnTearDown_replacingExistingLogger()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
Definition: MediaWikiTestCaseTest.php:146
MediaWikiTestCase\stashMwGlobals
stashMwGlobals( $globalKeys)
Stashes the global, will be restored in tearDown()
Definition: MediaWikiTestCase.php:730
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
MediaWikiTestCaseTest\testLoggersAreRestoredOnTearDown_replacingNonExistingLogger
testLoggersAreRestoredOnTearDown_replacingNonExistingLogger()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
Definition: MediaWikiTestCaseTest.php:161
MediaWikiTestCaseTest\provideExistingKeysAndNewValues
provideExistingKeysAndNewValues()
Definition: MediaWikiTestCaseTest.php:36
MediaWikiTestCaseTest\testSetNonExistentGlobalsAreUnsetOnTearDown
testSetNonExistentGlobalsAreUnsetOnTearDown()
MediaWikiTestCase::stashMwGlobals MediaWikiTestCase::tearDown.
Definition: MediaWikiTestCaseTest.php:96
MediaWikiTestCaseTest\$startGlobals
static $startGlobals
Definition: MediaWikiTestCaseTest.php:15
MediaWikiTestCaseTest\tearDownAfterClass
static tearDownAfterClass()
Definition: MediaWikiTestCaseTest.php:29
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:37
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:845
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:678
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:41
$value
$value
Definition: styleTest.css.php:45
MediaWikiTestCaseTest\testSetGlobalsAreRestoredOnTearDown
testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue)
provideExistingKeysAndNewValues
Definition: MediaWikiTestCaseTest.php:51
MediaWikiTestCaseTest
MediaWikiTestCase MediaWikiTestCaseTest.
Definition: MediaWikiTestCaseTest.php:13
MediaWikiTestCaseTest\testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice
testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
Definition: MediaWikiTestCaseTest.php:175
MediaWikiTestCase\setLogger
setLogger( $channel, LoggerInterface $logger)
Sets the logger for a specified channel, for the duration of the test.
Definition: MediaWikiTestCase.php:942
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:22
MediaWikiTestCaseTest\testOverrideMwServices
testOverrideMwServices()
Definition: MediaWikiTestCaseTest.php:112
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:56
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:25
MediaWikiTestCaseTest\testSetService
testSetService()
Definition: MediaWikiTestCaseTest.php:122
MediaWikiTestCaseTest\setUpBeforeClass
static setUpBeforeClass()
Definition: MediaWikiTestCaseTest.php:22
MediaWikiTestCase\setService
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown.
Definition: MediaWikiTestCase.php:628
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6
MediaWikiTestCase\tearDown
tearDown()
Definition: MediaWikiTestCase.php:534
MediaWikiTestCaseTest\testStashedGlobalsAreRestoredOnTearDown
testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue)
provideExistingKeysAndNewValues
Definition: MediaWikiTestCaseTest.php:74