summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/libs/objectcache/CachedBagOStuffTest.php
blob: d0360a99f2abf0b959b313517a39b7c3997a21dc (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php

use Wikimedia\TestingAccessWrapper;

/**
 * @group BagOStuff
 */
class CachedBagOStuffTest extends PHPUnit\Framework\TestCase {

	use MediaWikiCoversValidator;

	/**
	 * @covers CachedBagOStuff::__construct
	 * @covers CachedBagOStuff::doGet
	 */
	public function testGetFromBackend() {
		$backend = new HashBagOStuff;
		$cache = new CachedBagOStuff( $backend );

		$backend->set( 'foo', 'bar' );
		$this->assertEquals( 'bar', $cache->get( 'foo' ) );

		$backend->set( 'foo', 'baz' );
		$this->assertEquals( 'bar', $cache->get( 'foo' ), 'cached' );
	}

	/**
	 * @covers CachedBagOStuff::set
	 * @covers CachedBagOStuff::delete
	 */
	public function testSetAndDelete() {
		$backend = new HashBagOStuff;
		$cache = new CachedBagOStuff( $backend );

		for ( $i = 0; $i < 10; $i++ ) {
			$cache->set( "key$i", 1 );
			$this->assertEquals( 1, $cache->get( "key$i" ) );
			$this->assertEquals( 1, $backend->get( "key$i" ) );
			$cache->delete( "key$i" );
			$this->assertEquals( false, $cache->get( "key$i" ) );
			$this->assertEquals( false, $backend->get( "key$i" ) );
		}
	}

	/**
	 * @covers CachedBagOStuff::set
	 * @covers CachedBagOStuff::delete
	 */
	public function testWriteCacheOnly() {
		$backend = new HashBagOStuff;
		$cache = new CachedBagOStuff( $backend );

		$cache->set( 'foo', 'bar', 0, CachedBagOStuff::WRITE_CACHE_ONLY );
		$this->assertEquals( 'bar', $cache->get( 'foo' ) );
		$this->assertFalse( $backend->get( 'foo' ) );

		$cache->set( 'foo', 'old' );
		$this->assertEquals( 'old', $cache->get( 'foo' ) );
		$this->assertEquals( 'old', $backend->get( 'foo' ) );

		$cache->set( 'foo', 'new', 0, CachedBagOStuff::WRITE_CACHE_ONLY );
		$this->assertEquals( 'new', $cache->get( 'foo' ) );
		$this->assertEquals( 'old', $backend->get( 'foo' ) );

		$cache->delete( 'foo', CachedBagOStuff::WRITE_CACHE_ONLY );
		$this->assertEquals( 'old', $cache->get( 'foo' ) ); // Reloaded from backend
	}

	/**
	 * @covers CachedBagOStuff::doGet
	 */
	public function testCacheBackendMisses() {
		$backend = new HashBagOStuff;
		$cache = new CachedBagOStuff( $backend );

		// First hit primes the cache with miss from the backend
		$this->assertEquals( false, $cache->get( 'foo' ) );

		// Change the value in the backend
		$backend->set( 'foo', true );

		// Second hit returns the cached miss
		$this->assertEquals( false, $cache->get( 'foo' ) );

		// But a fresh value is read from the backend
		$backend->set( 'bar', true );
		$this->assertEquals( true, $cache->get( 'bar' ) );
	}

	/**
	 * @covers CachedBagOStuff::setDebug
	 */
	public function testSetDebug() {
		$backend = new HashBagOStuff();
		$cache = new CachedBagOStuff( $backend );
		// Access private property 'debugMode'
		$backend = TestingAccessWrapper::newFromObject( $backend );
		$cache = TestingAccessWrapper::newFromObject( $cache );
		$this->assertFalse( $backend->debugMode );
		$this->assertFalse( $cache->debugMode );

		$cache->setDebug( true );
		// Should have set both
		$this->assertTrue( $backend->debugMode, 'sets backend' );
		$this->assertTrue( $cache->debugMode, 'sets self' );
	}

	/**
	 * @covers CachedBagOStuff::deleteObjectsExpiringBefore
	 */
	public function testExpire() {
		$backend = $this->getMockBuilder( HashBagOStuff::class )
			->setMethods( [ 'deleteObjectsExpiringBefore' ] )
			->getMock();
		$backend->expects( $this->once() )
			->method( 'deleteObjectsExpiringBefore' )
			->willReturn( false );

		$cache = new CachedBagOStuff( $backend );
		$cache->deleteObjectsExpiringBefore( '20110401000000' );
	}

	/**
	 * @covers CachedBagOStuff::makeKey
	 */
	public function testMakeKey() {
		$backend = $this->getMockBuilder( HashBagOStuff::class )
			->setMethods( [ 'makeKey' ] )
			->getMock();
		$backend->method( 'makeKey' )
			->willReturn( 'special/logic' );

		// CachedBagOStuff wraps any backend with a process cache
		// using HashBagOStuff. Hash has no special key limitations,
		// but backends often do. Make sure it uses the backend's
		// makeKey() logic, not the one inherited from HashBagOStuff
		$cache = new CachedBagOStuff( $backend );

		$this->assertEquals( 'special/logic', $backend->makeKey( 'special', 'logic' ) );
		$this->assertEquals( 'special/logic', $cache->makeKey( 'special', 'logic' ) );
	}

	/**
	 * @covers CachedBagOStuff::makeGlobalKey
	 */
	public function testMakeGlobalKey() {
		$backend = $this->getMockBuilder( HashBagOStuff::class )
			->setMethods( [ 'makeGlobalKey' ] )
			->getMock();
		$backend->method( 'makeGlobalKey' )
			->willReturn( 'special/logic' );

		$cache = new CachedBagOStuff( $backend );

		$this->assertEquals( 'special/logic', $backend->makeGlobalKey( 'special', 'logic' ) );
		$this->assertEquals( 'special/logic', $cache->makeGlobalKey( 'special', 'logic' ) );
	}
}