MediaWiki  1.28.0
MediaWikiTestCaseTest.php
Go to the documentation of this file.
1 <?php
5 
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 
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 
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 
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 
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 }
static static setUpBeforeClass()
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testLoggersAreRestoredOnTearDown()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLogger.
$value
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
testStashedGlobalsAreRestoredOnTearDown($globalKey, $newValue)
provideExistingKeysAndNewValues
setService($name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown...
testExceptionThrownWhenStashingNonExistentGlobals()
MediaWikiTestCase::stashMwGlobals.
$GLOBALS['IP']
testSetGlobalsAreRestoredOnTearDown($globalKey, $newValue)
provideExistingKeysAndNewValues
overrideMwServices(Config $configOverrides=null, array $services=[])
Stashes the global instance of MediaWikiServices, and installs a new one, allowing test cases to over...
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
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
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
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
setMwGlobals($pairs, $value=null)
setLogger($channel, LoggerInterface $logger)
Sets the logger for a specified channel, for the duration of the test.
stashMwGlobals($globalKeys)
Stashes the global, will be restored in tearDown()