MediaWiki  1.33.0
MediaWikiTestCaseTest.php
Go to the documentation of this file.
1 <?php
4 use Psr\Log\LoggerInterface;
6 
15 
16  private static $startGlobals = [
17  'MediaWikiTestCaseTestGLOBAL-ExistingString' => 'foo',
18  'MediaWikiTestCaseTestGLOBAL-ExistingStringEmpty' => '',
19  'MediaWikiTestCaseTestGLOBAL-ExistingArray' => [ 1, 'foo' => 'bar' ],
20  'MediaWikiTestCaseTestGLOBAL-ExistingArrayEmpty' => [],
21  ];
22 
23  public static function setUpBeforeClass() {
24  parent::setUpBeforeClass();
25  foreach ( self::$startGlobals as $key => $value ) {
26  $GLOBALS[$key] = $value;
27  }
28  }
29 
30  public static function tearDownAfterClass() {
31  parent::tearDownAfterClass();
32  foreach ( self::$startGlobals as $key => $value ) {
33  unset( $GLOBALS[$key] );
34  }
35  }
36 
37  public function provideExistingKeysAndNewValues() {
38  $providedArray = [];
39  foreach ( array_keys( self::$startGlobals ) as $key ) {
40  $providedArray[] = [ $key, 'newValue' ];
41  $providedArray[] = [ $key, [ 'newValue' ] ];
42  }
43  return $providedArray;
44  }
45 
52  public function testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
53  $this->setMwGlobals( $globalKey, $newValue );
54  $this->assertEquals(
55  $newValue,
56  $GLOBALS[$globalKey],
57  'Global failed to correctly set'
58  );
59 
60  $this->tearDown();
61 
62  $this->assertEquals(
63  self::$startGlobals[$globalKey],
64  $GLOBALS[$globalKey],
65  'Global failed to be restored on tearDown'
66  );
67  }
68 
75  public function testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
76  $this->hideDeprecated( 'MediaWikiTestCase::stashMwGlobals' );
77  $this->stashMwGlobals( $globalKey );
78  $GLOBALS[$globalKey] = $newValue;
79  $this->assertEquals(
80  $newValue,
81  $GLOBALS[$globalKey],
82  'Global failed to correctly set'
83  );
84 
85  $this->tearDown();
86 
87  $this->assertEquals(
88  self::$startGlobals[$globalKey],
89  $GLOBALS[$globalKey],
90  'Global failed to be restored on tearDown'
91  );
92  }
93 
99  $globalKey = 'abcdefg1234567';
100  $this->setMwGlobals( $globalKey, true );
101  $this->assertTrue(
102  $GLOBALS[$globalKey],
103  'Global failed to correctly set'
104  );
105 
106  $this->tearDown();
107 
108  $this->assertFalse(
109  isset( $GLOBALS[$globalKey] ),
110  'Global failed to be correctly unset'
111  );
112  }
113 
114  public function testOverrideMwServices() {
115  $initialServices = MediaWikiServices::getInstance();
116 
117  $this->overrideMwServices();
118  $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
119  }
120 
121  public function testSetService() {
122  $initialServices = MediaWikiServices::getInstance();
123  $initialService = $initialServices->getDBLoadBalancer();
124  $mockService = $this->getMockBuilder( LoadBalancer::class )
125  ->disableOriginalConstructor()->getMock();
126 
127  $this->setService( 'DBLoadBalancer', $mockService );
128  $this->assertNotSame(
129  $initialService,
130  MediaWikiServices::getInstance()->getDBLoadBalancer()
131  );
132  $this->assertSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
133  }
134 
140  $logger1 = LoggerFactory::getInstance( 'foo' );
141  $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
142  $logger2 = LoggerFactory::getInstance( 'foo' );
143  $this->tearDown();
144  $logger3 = LoggerFactory::getInstance( 'foo' );
145 
146  $this->assertSame( $logger1, $logger3 );
147  $this->assertNotSame( $logger1, $logger2 );
148  }
149 
155  $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
156  $logger1 = LoggerFactory::getInstance( 'foo' );
157  $this->tearDown();
158  $logger2 = LoggerFactory::getInstance( 'foo' );
159 
160  $this->assertNotSame( $logger1, $logger2 );
161  $this->assertInstanceOf( \Psr\Log\LoggerInterface::class, $logger2 );
162  }
163 
169  $logger1 = LoggerFactory::getInstance( 'baz' );
170  $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
171  $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
172  $this->tearDown();
173  $logger2 = LoggerFactory::getInstance( 'baz' );
174 
175  $this->assertSame( $logger1, $logger2 );
176  }
177 
182  public function testCopyTestData() {
183  $this->markTestSkippedIfDbType( 'sqlite' );
184 
185  $this->tablesUsed[] = 'objectcache';
186  $this->db->insert(
187  'objectcache',
188  [ 'keyname' => __METHOD__, 'value' => 'TEST', 'exptime' => $this->db->timestamp( 11 ) ],
189  __METHOD__
190  );
191 
192  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
193  $lb = $lbFactory->newMainLB();
194  $db = $lb->getConnection( DB_REPLICA, DBO_TRX );
195 
196  // sanity
197  $this->assertNotSame( $this->db, $db );
198 
199  // Make sure the DB connection has the fake table clones and the fake table prefix
201 
202  $this->assertSame( $this->db->tablePrefix(), $db->tablePrefix(), 'tablePrefix' );
203 
204  // Make sure the DB connection has all the test data
205  $this->copyTestData( $this->db, $db );
206 
207  $value = $db->selectField( 'objectcache', 'value', [ 'keyname' => __METHOD__ ], __METHOD__ );
208  $this->assertSame( 'TEST', $value, 'Copied Data' );
209  }
210 
211 }
MediaWikiTestCase\copyTestData
copyTestData(IDatabase $source, IDatabase $target)
Copy test data from one database connection to another.
Definition: MediaWikiTestCase.php:1914
MediaWikiTestCaseTest\testLoggersAreRestoredOnTearDown_replacingExistingLogger
testLoggersAreRestoredOnTearDown_replacingExistingLogger()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
Definition: MediaWikiTestCaseTest.php:139
MediaWikiTestCase\stashMwGlobals
stashMwGlobals( $globalKeys)
Stashes the global, will be restored in tearDown()
Definition: MediaWikiTestCase.php:816
MediaWikiTestCaseTest\testLoggersAreRestoredOnTearDown_replacingNonExistingLogger
testLoggersAreRestoredOnTearDown_replacingNonExistingLogger()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
Definition: MediaWikiTestCaseTest.php:154
MediaWikiTestCaseTest\provideExistingKeysAndNewValues
provideExistingKeysAndNewValues()
Definition: MediaWikiTestCaseTest.php:37
MediaWikiTestCaseTest\testSetNonExistentGlobalsAreUnsetOnTearDown
testSetNonExistentGlobalsAreUnsetOnTearDown()
MediaWikiTestCase::stashMwGlobals MediaWikiTestCase::tearDown.
Definition: MediaWikiTestCaseTest.php:98
MediaWikiTestCaseTest\$startGlobals
static $startGlobals
Definition: MediaWikiTestCaseTest.php:16
MediaWikiTestCase\setupDatabaseWithTestPrefix
static setupDatabaseWithTestPrefix(IMaintainableDatabase $db, $prefix=null)
Setups a database with cloned tables using the given prefix.
Definition: MediaWikiTestCase.php:1388
MediaWikiTestCaseTest\tearDownAfterClass
static tearDownAfterClass()
Definition: MediaWikiTestCaseTest.php:30
DBO_TRX
const DBO_TRX
Definition: defines.php:12
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:937
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:709
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
MediaWikiTestCase\hideDeprecated
hideDeprecated( $function)
Don't throw a warning if $function is deprecated and called later.
Definition: MediaWikiTestCase.php:1974
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
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
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:52
MediaWikiTestCase\dbPrefix
dbPrefix()
Definition: MediaWikiTestCase.php:1173
MediaWikiTestCaseTest
MediaWikiTestCase MediaWikiTestCaseTest Database.
Definition: MediaWikiTestCaseTest.php:14
MediaWikiTestCaseTest\testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice
testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
Definition: MediaWikiTestCaseTest.php:168
MediaWikiTestCase\setLogger
setLogger( $channel, LoggerInterface $logger)
Sets the logger for a specified channel, for the duration of the test.
Definition: MediaWikiTestCase.php:1118
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:114
MediaWikiTestCaseTest\testCopyTestData
testCopyTestData()
MediaWikiTestCase::setupDatabaseWithTestPrefix MediaWikiTestCase::copyTestData.
Definition: MediaWikiTestCaseTest.php:182
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:121
MediaWikiTestCaseTest\setUpBeforeClass
static setUpBeforeClass()
Definition: MediaWikiTestCaseTest.php:23
MediaWikiTestCase\setService
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown.
Definition: MediaWikiTestCase.php:649
MediaWikiTestCase\markTestSkippedIfDbType
markTestSkippedIfDbType( $type)
Skip the test if using the specified database type.
Definition: MediaWikiTestCase.php:2303
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6
MediaWikiTestCase\$db
Database $db
Primary database.
Definition: MediaWikiTestCase.php:61
MediaWikiTestCase\tearDown
tearDown()
Definition: MediaWikiTestCase.php:547
MediaWikiTestCaseTest\testStashedGlobalsAreRestoredOnTearDown
testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue)
provideExistingKeysAndNewValues
Definition: MediaWikiTestCaseTest.php:75