MediaWiki  1.33.0
ObjectCacheTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5  protected function setUp() {
6  // Parent calls ObjectCache::clear() among other things
7  parent::setUp();
8 
9  $this->setCacheConfig();
10  $this->setMwGlobals( [
11  'wgMainCacheType' => CACHE_NONE,
12  'wgMessageCacheType' => CACHE_NONE,
13  'wgParserCacheType' => CACHE_NONE,
14  ] );
15  }
16 
17  private function setCacheConfig( $arr = [] ) {
18  $defaults = [
19  CACHE_NONE => [ 'class' => EmptyBagOStuff::class ],
20  CACHE_DB => [ 'class' => SqlBagOStuff::class ],
21  CACHE_ANYTHING => [ 'factory' => 'ObjectCache::newAnything' ],
22  // Mock ACCEL with 'hash' as being installed.
23  // This makes tests deterministic regardless of APC.
24  CACHE_ACCEL => [ 'class' => HashBagOStuff::class ],
25  'hash' => [ 'class' => HashBagOStuff::class ],
26  ];
27  $this->setMwGlobals( 'wgObjectCaches', $arr + $defaults );
28  }
29 
31  public function testNewAnythingNothing() {
32  $this->assertInstanceOf(
35  'No available types. Fallback to DB'
36  );
37  }
38 
40  public function testNewAnythingHash() {
41  $this->setMwGlobals( [
42  'wgMainCacheType' => 'hash'
43  ] );
44 
45  $this->assertInstanceOf(
48  'Use an available type (hash)'
49  );
50  }
51 
53  public function testNewAnythingAccel() {
54  $this->setMwGlobals( [
55  'wgMainCacheType' => CACHE_ACCEL
56  ] );
57 
58  $this->assertInstanceOf(
61  'Use an available type (CACHE_ACCEL)'
62  );
63  }
64 
66  public function testNewAnythingNoAccel() {
67  $this->setMwGlobals( [
68  'wgMainCacheType' => CACHE_ACCEL
69  ] );
70 
71  $this->setCacheConfig( [
72  // Mock APC not being installed (T160519, T147161)
73  CACHE_ACCEL => [ 'class' => EmptyBagOStuff::class ]
74  ] );
75 
76  $this->assertInstanceOf(
79  'Fallback to DB if available types fall back to Empty'
80  );
81  }
82 
84  public function testNewAnythingNoAccelNoDb() {
85  $this->overrideMwServices(); // Ensures restore on tear down
87 
88  $this->setMwGlobals( [
89  'wgMainCacheType' => CACHE_ACCEL
90  ] );
91 
92  $this->setCacheConfig( [
93  // Mock APC not being installed (T160519, T147161)
94  CACHE_ACCEL => [ 'class' => EmptyBagOStuff::class ]
95  ] );
96 
97  $this->assertInstanceOf(
100  'Fallback to none if available types and DB are unavailable'
101  );
102  }
103 
105  public function testNewAnythingNothingNoDb() {
106  $this->overrideMwServices();
108 
109  $this->assertInstanceOf(
112  'No available types or DB. Fallback to none.'
113  );
114  }
115 }
ObjectCacheTest\testNewAnythingHash
testNewAnythingHash()
ObjectCache::newAnything.
Definition: ObjectCacheTest.php:40
CACHE_NONE
const CACHE_NONE
Definition: Defines.php:102
CACHE_ACCEL
const CACHE_ACCEL
Definition: Defines.php:105
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
ObjectCache\newAnything
static newAnything( $params)
Factory function for CACHE_ANYTHING (referenced from DefaultSettings.php)
Definition: ObjectCache.php:237
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
ObjectCacheTest\setUp
setUp()
Definition: ObjectCacheTest.php:5
ObjectCacheTest\testNewAnythingNoAccelNoDb
testNewAnythingNoAccelNoDb()
ObjectCache::newAnything.
Definition: ObjectCacheTest.php:84
ObjectCacheTest\testNewAnythingAccel
testNewAnythingAccel()
ObjectCache::newAnything.
Definition: ObjectCacheTest.php:53
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
ObjectCacheTest\testNewAnythingNoAccel
testNewAnythingNoAccel()
ObjectCache::newAnything.
Definition: ObjectCacheTest.php:66
MediaWiki\MediaWikiServices\disableStorageBackend
static disableStorageBackend()
Disables all storage layer services.
Definition: MediaWikiServices.php:297
ObjectCacheTest\setCacheConfig
setCacheConfig( $arr=[])
Definition: ObjectCacheTest.php:17
ObjectCacheTest\testNewAnythingNothingNoDb
testNewAnythingNothingNoDb()
ObjectCache::newAnything.
Definition: ObjectCacheTest.php:105
CACHE_ANYTHING
const CACHE_ANYTHING
Definition: Defines.php:101
ObjectCacheTest\testNewAnythingNothing
testNewAnythingNothing()
ObjectCache::newAnything.
Definition: ObjectCacheTest.php:31
ObjectCacheTest
Definition: ObjectCacheTest.php:3
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
CACHE_DB
const CACHE_DB
Definition: Defines.php:103