]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - tests/phpunit/includes/objectcache/ObjectCacheTest.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.git] / tests / phpunit / includes / objectcache / ObjectCacheTest.php
1 <?php
2
3 class ObjectCacheTest extends MediaWikiTestCase {
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' ],
20                         CACHE_DB => [ 'class' => 'SqlBagOStuff' ],
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' ],
25                         'hash' => [ 'class' => 'HashBagOStuff' ],
26                 ];
27                 $this->setMwGlobals( 'wgObjectCaches', $arr + $defaults );
28         }
29
30         /** @covers ObjectCache::newAnything */
31         public function testNewAnythingNothing() {
32                 $this->assertInstanceOf(
33                         SqlBagOStuff::class,
34                         ObjectCache::newAnything( [] ),
35                         'No available types. Fallback to DB'
36                 );
37         }
38
39         /** @covers ObjectCache::newAnything */
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
52         /** @covers ObjectCache::newAnything */
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
65         /** @covers ObjectCache::newAnything */
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' ]
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
83         /** @covers ObjectCache::newAnything */
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' ]
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
104         /** @covers ObjectCache::newAnything */
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 }