MediaWiki REL1_33
MediaWikiTestCaseTest.php
Go to the documentation of this file.
1<?php
4use 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
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}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$GLOBALS['IP']
MediaWikiTestCase MediaWikiTestCaseTest Database.
testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
testLoggersAreRestoredOnTearDown_replacingNonExistingLogger()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
testCopyTestData()
MediaWikiTestCase::setupDatabaseWithTestPrefix MediaWikiTestCase::copyTestData.
testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue)
provideExistingKeysAndNewValues
testLoggersAreRestoredOnTearDown_replacingExistingLogger()
MediaWikiTestCase::setLogger MediaWikiTestCase::restoreLoggers.
testSetNonExistentGlobalsAreUnsetOnTearDown()
MediaWikiTestCase::stashMwGlobals MediaWikiTestCase::tearDown.
testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue)
provideExistingKeysAndNewValues
Database $db
Primary database.
markTestSkippedIfDbType( $type)
Skip the test if using the specified database type.
overrideMwServices(Config $configOverrides=null, array $services=[])
Stashes the global instance of MediaWikiServices, and installs a new one, allowing test cases to over...
setLogger( $channel, LoggerInterface $logger)
Sets the logger for a specified channel, for the duration of the test.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
hideDeprecated( $function)
Don't throw a warning if $function is deprecated and called later.
static setupDatabaseWithTestPrefix(IMaintainableDatabase $db, $prefix=null)
Setups a database with cloned tables using the given prefix.
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown.
copyTestData(IDatabase $source, IDatabase $target)
Copy test data from one database connection to another.
stashMwGlobals( $globalKeys)
Stashes the global, will be restored in tearDown()
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
tablePrefix( $prefix=null)
Get/set the table prefix.
Definition Database.php:607
selectField( $table, $var, $cond='', $fname=__METHOD__, $options=[], $join_conds=[])
A SELECT wrapper which returns a single field from a single result row.
Database connection, tracking, load balancing, and transaction manager for a cluster.
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
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
const DB_REPLICA
Definition defines.php:25
const DBO_TRX
Definition defines.php:12