summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/objectcache/ObjectCacheTest.php
blob: 4318852867ef2d51a4e3b25251ec9cc462eedebb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

class ObjectCacheTest extends MediaWikiTestCase {

	protected function setUp() {
		// Parent calls ObjectCache::clear() among other things
		parent::setUp();

		$this->setCacheConfig();
		$this->setMwGlobals( [
			'wgMainCacheType' => CACHE_NONE,
			'wgMessageCacheType' => CACHE_NONE,
			'wgParserCacheType' => CACHE_NONE,
		] );
	}

	private function setCacheConfig( $arr = [] ) {
		$defaults = [
			CACHE_NONE => [ 'class' => EmptyBagOStuff::class ],
			CACHE_DB => [ 'class' => SqlBagOStuff::class ],
			CACHE_ANYTHING => [ 'factory' => 'ObjectCache::newAnything' ],
			// Mock ACCEL with 'hash' as being installed.
			// This makes tests deterministic regardless of APC.
			CACHE_ACCEL => [ 'class' => HashBagOStuff::class ],
			'hash' => [ 'class' => HashBagOStuff::class ],
		];
		$this->setMwGlobals( 'wgObjectCaches', $arr + $defaults );
	}

	/** @covers ObjectCache::newAnything */
	public function testNewAnythingNothing() {
		$this->assertInstanceOf(
			SqlBagOStuff::class,
			ObjectCache::newAnything( [] ),
			'No available types. Fallback to DB'
		);
	}

	/** @covers ObjectCache::newAnything */
	public function testNewAnythingHash() {
		$this->setMwGlobals( [
			'wgMainCacheType' => 'hash'
		] );

		$this->assertInstanceOf(
			HashBagOStuff::class,
			ObjectCache::newAnything( [] ),
			'Use an available type (hash)'
		);
	}

	/** @covers ObjectCache::newAnything */
	public function testNewAnythingAccel() {
		$this->setMwGlobals( [
			'wgMainCacheType' => CACHE_ACCEL
		] );

		$this->assertInstanceOf(
			HashBagOStuff::class,
			ObjectCache::newAnything( [] ),
			'Use an available type (CACHE_ACCEL)'
		);
	}

	/** @covers ObjectCache::newAnything */
	public function testNewAnythingNoAccel() {
		$this->setMwGlobals( [
			'wgMainCacheType' => CACHE_ACCEL
		] );

		$this->setCacheConfig( [
			// Mock APC not being installed (T160519, T147161)
			CACHE_ACCEL => [ 'class' => EmptyBagOStuff::class ]
		] );

		$this->assertInstanceOf(
			SqlBagOStuff::class,
			ObjectCache::newAnything( [] ),
			'Fallback to DB if available types fall back to Empty'
		);
	}

	/** @covers ObjectCache::newAnything */
	public function testNewAnythingNoAccelNoDb() {
		$this->overrideMwServices(); // Ensures restore on tear down
		MediaWiki\MediaWikiServices::disableStorageBackend();

		$this->setMwGlobals( [
			'wgMainCacheType' => CACHE_ACCEL
		] );

		$this->setCacheConfig( [
			// Mock APC not being installed (T160519, T147161)
			CACHE_ACCEL => [ 'class' => EmptyBagOStuff::class ]
		] );

		$this->assertInstanceOf(
			EmptyBagOStuff::class,
			ObjectCache::newAnything( [] ),
			'Fallback to none if available types and DB are unavailable'
		);
	}

	/** @covers ObjectCache::newAnything */
	public function testNewAnythingNothingNoDb() {
		$this->overrideMwServices();
		MediaWiki\MediaWikiServices::disableStorageBackend();

		$this->assertInstanceOf(
			EmptyBagOStuff::class,
			ObjectCache::newAnything( [] ),
			'No available types or DB. Fallback to none.'
		);
	}
}