MediaWiki REL1_31
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(
33 SqlBagOStuff::class,
34 ObjectCache::newAnything( [] ),
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(
46 HashBagOStuff::class,
47 ObjectCache::newAnything( [] ),
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(
59 HashBagOStuff::class,
60 ObjectCache::newAnything( [] ),
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(
77 SqlBagOStuff::class,
78 ObjectCache::newAnything( [] ),
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
86 MediaWiki\MediaWikiServices::disableStorageBackend();
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(
98 EmptyBagOStuff::class,
99 ObjectCache::newAnything( [] ),
100 'Fallback to none if available types and DB are unavailable'
101 );
102 }
103
105 public function testNewAnythingNothingNoDb() {
106 $this->overrideMwServices();
107 MediaWiki\MediaWikiServices::disableStorageBackend();
108
109 $this->assertInstanceOf(
110 EmptyBagOStuff::class,
111 ObjectCache::newAnything( [] ),
112 'No available types or DB. Fallback to none.'
113 );
114 }
115}
overrideMwServices(Config $configOverrides=null, array $services=[])
Stashes the global instance of MediaWikiServices, and installs a new one, allowing test cases to over...
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
testNewAnythingNoAccelNoDb()
ObjectCache::newAnything.
testNewAnythingHash()
ObjectCache::newAnything.
testNewAnythingNothing()
ObjectCache::newAnything.
setCacheConfig( $arr=[])
testNewAnythingAccel()
ObjectCache::newAnything.
testNewAnythingNothingNoDb()
ObjectCache::newAnything.
testNewAnythingNoAccel()
ObjectCache::newAnything.
const CACHE_NONE
Definition Defines.php:112
const CACHE_ANYTHING
Definition Defines.php:111
const CACHE_ACCEL
Definition Defines.php:115
const CACHE_DB
Definition Defines.php:113