MediaWiki  1.32.0
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->hideDeprecated( 'MediaWikiTestCase::stashMwGlobals' );
76  $this->stashMwGlobals( $globalKey );
77  $GLOBALS[$globalKey] = $newValue;
78  $this->assertEquals(
79  $newValue,
80  $GLOBALS[$globalKey],
81  'Global failed to correctly set'
82  );
83 
84  $this->tearDown();
85 
86  $this->assertEquals(
87  self::$startGlobals[$globalKey],
88  $GLOBALS[$globalKey],
89  'Global failed to be restored on tearDown'
90  );
91  }
92 
98  $globalKey = 'abcdefg1234567';
99  $this->setMwGlobals( $globalKey, true );
100  $this->assertTrue(
101  $GLOBALS[$globalKey],
102  'Global failed to correctly set'
103  );
104 
105  $this->tearDown();
106 
107  $this->assertFalse(
108  isset( $GLOBALS[$globalKey] ),
109  'Global failed to be correctly unset'
110  );
111  }
112 
113  public function testOverrideMwServices() {
114  $initialServices = MediaWikiServices::getInstance();
115 
116  $this->overrideMwServices();
117  $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
118  }
119 
120  public function testSetService() {
121  $initialServices = MediaWikiServices::getInstance();
122  $initialService = $initialServices->getDBLoadBalancer();
123  $mockService = $this->getMockBuilder( LoadBalancer::class )
124  ->disableOriginalConstructor()->getMock();
125 
126  $this->setService( 'DBLoadBalancer', $mockService );
127  $this->assertNotSame(
128  $initialService,
129  MediaWikiServices::getInstance()->getDBLoadBalancer()
130  );
131  $this->assertSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
132  }
133 
139  $logger1 = LoggerFactory::getInstance( 'foo' );
140  $this->setLogger( 'foo', $this->createMock( 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 
154  $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
155  $logger1 = LoggerFactory::getInstance( 'foo' );
156  $this->tearDown();
157  $logger2 = LoggerFactory::getInstance( 'foo' );
158 
159  $this->assertNotSame( $logger1, $logger2 );
160  $this->assertInstanceOf( \Psr\Log\LoggerInterface::class, $logger2 );
161  }
162 
168  $logger1 = LoggerFactory::getInstance( 'baz' );
169  $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
170  $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
171  $this->tearDown();
172  $logger2 = LoggerFactory::getInstance( 'baz' );
173 
174  $this->assertSame( $logger1, $logger2 );
175  }
176 }
MediaWikiTestCaseTest\testLoggersAreRestoredOnTearDown_replacingExistingLogger
testLoggersAreRestoredOnTearDown_replacingExistingLogger()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
Definition: MediaWikiTestCaseTest.php:138
MediaWikiTestCase\stashMwGlobals
stashMwGlobals( $globalKeys)
Stashes the global, will be restored in tearDown()
Definition: MediaWikiTestCase.php:813
MediaWikiTestCaseTest\testLoggersAreRestoredOnTearDown_replacingNonExistingLogger
testLoggersAreRestoredOnTearDown_replacingNonExistingLogger()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
Definition: MediaWikiTestCaseTest.php:153
MediaWikiTestCaseTest\provideExistingKeysAndNewValues
provideExistingKeysAndNewValues()
Definition: MediaWikiTestCaseTest.php:36
MediaWikiTestCaseTest\testSetNonExistentGlobalsAreUnsetOnTearDown
testSetNonExistentGlobalsAreUnsetOnTearDown()
MediaWikiTestCase::stashMwGlobals MediaWikiTestCase::tearDown.
Definition: MediaWikiTestCaseTest.php:97
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: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:934
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:706
MediaWikiTestCase
Definition: MediaWikiTestCase.php:16
MediaWikiTestCase\hideDeprecated
hideDeprecated( $function)
Don't throw a warning if $function is deprecated and called later.
Definition: MediaWikiTestCase.php:1940
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
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:41
$value
$value
Definition: styleTest.css.php:49
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:167
MediaWikiTestCase\setLogger
setLogger( $channel, LoggerInterface $logger)
Sets the logger for a specified channel, for the duration of the test.
Definition: MediaWikiTestCase.php:1115
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:113
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:120
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:646
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6
MediaWikiTestCase\tearDown
tearDown()
Definition: MediaWikiTestCase.php:544
MediaWikiTestCaseTest\testStashedGlobalsAreRestoredOnTearDown
testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue)
provideExistingKeysAndNewValues
Definition: MediaWikiTestCaseTest.php:74