MediaWiki REL1_33
CachedBagOStuffTest.php
Go to the documentation of this file.
1<?php
2
4
8class CachedBagOStuffTest extends PHPUnit\Framework\TestCase {
9
10 use MediaWikiCoversValidator;
11
16 public function testGetFromBackend() {
17 $backend = new HashBagOStuff;
18 $cache = new CachedBagOStuff( $backend );
19
20 $backend->set( 'foo', 'bar' );
21 $this->assertEquals( 'bar', $cache->get( 'foo' ) );
22
23 $backend->set( 'foo', 'baz' );
24 $this->assertEquals( 'bar', $cache->get( 'foo' ), 'cached' );
25 }
26
31 public function testSetAndDelete() {
32 $backend = new HashBagOStuff;
33 $cache = new CachedBagOStuff( $backend );
34
35 for ( $i = 0; $i < 10; $i++ ) {
36 $cache->set( "key$i", 1 );
37 $this->assertEquals( 1, $cache->get( "key$i" ) );
38 $this->assertEquals( 1, $backend->get( "key$i" ) );
39
40 $cache->delete( "key$i" );
41 $this->assertEquals( false, $cache->get( "key$i" ) );
42 $this->assertEquals( false, $backend->get( "key$i" ) );
43 }
44 }
45
50 public function testWriteCacheOnly() {
51 $backend = new HashBagOStuff;
52 $cache = new CachedBagOStuff( $backend );
53
54 $cache->set( 'foo', 'bar', 0, CachedBagOStuff::WRITE_CACHE_ONLY );
55 $this->assertEquals( 'bar', $cache->get( 'foo' ) );
56 $this->assertFalse( $backend->get( 'foo' ) );
57
58 $cache->set( 'foo', 'old' );
59 $this->assertEquals( 'old', $cache->get( 'foo' ) );
60 $this->assertEquals( 'old', $backend->get( 'foo' ) );
61
62 $cache->set( 'foo', 'new', 0, CachedBagOStuff::WRITE_CACHE_ONLY );
63 $this->assertEquals( 'new', $cache->get( 'foo' ) );
64 $this->assertEquals( 'old', $backend->get( 'foo' ) );
65
66 $cache->delete( 'foo', CachedBagOStuff::WRITE_CACHE_ONLY );
67 $this->assertEquals( 'old', $cache->get( 'foo' ) ); // Reloaded from backend
68 }
69
73 public function testCacheBackendMisses() {
74 $backend = new HashBagOStuff;
75 $cache = new CachedBagOStuff( $backend );
76
77 // First hit primes the cache with miss from the backend
78 $this->assertEquals( false, $cache->get( 'foo' ) );
79
80 // Change the value in the backend
81 $backend->set( 'foo', true );
82
83 // Second hit returns the cached miss
84 $this->assertEquals( false, $cache->get( 'foo' ) );
85
86 // But a fresh value is read from the backend
87 $backend->set( 'bar', true );
88 $this->assertEquals( true, $cache->get( 'bar' ) );
89 }
90
94 public function testSetDebug() {
95 $backend = new HashBagOStuff();
96 $cache = new CachedBagOStuff( $backend );
97 // Access private property 'debugMode'
98 $backend = TestingAccessWrapper::newFromObject( $backend );
99 $cache = TestingAccessWrapper::newFromObject( $cache );
100 $this->assertFalse( $backend->debugMode );
101 $this->assertFalse( $cache->debugMode );
102
103 $cache->setDebug( true );
104 // Should have set both
105 $this->assertTrue( $backend->debugMode, 'sets backend' );
106 $this->assertTrue( $cache->debugMode, 'sets self' );
107 }
108
112 public function testExpire() {
113 $backend = $this->getMockBuilder( HashBagOStuff::class )
114 ->setMethods( [ 'deleteObjectsExpiringBefore' ] )
115 ->getMock();
116 $backend->expects( $this->once() )
117 ->method( 'deleteObjectsExpiringBefore' )
118 ->willReturn( false );
119
120 $cache = new CachedBagOStuff( $backend );
121 $cache->deleteObjectsExpiringBefore( '20110401000000' );
122 }
123
127 public function testMakeKey() {
128 $backend = $this->getMockBuilder( HashBagOStuff::class )
129 ->setMethods( [ 'makeKey' ] )
130 ->getMock();
131 $backend->method( 'makeKey' )
132 ->willReturn( 'special/logic' );
133
134 // CachedBagOStuff wraps any backend with a process cache
135 // using HashBagOStuff. Hash has no special key limitations,
136 // but backends often do. Make sure it uses the backend's
137 // makeKey() logic, not the one inherited from HashBagOStuff
138 $cache = new CachedBagOStuff( $backend );
139
140 $this->assertEquals( 'special/logic', $backend->makeKey( 'special', 'logic' ) );
141 $this->assertEquals( 'special/logic', $cache->makeKey( 'special', 'logic' ) );
142 }
143
147 public function testMakeGlobalKey() {
148 $backend = $this->getMockBuilder( HashBagOStuff::class )
149 ->setMethods( [ 'makeGlobalKey' ] )
150 ->getMock();
151 $backend->method( 'makeGlobalKey' )
152 ->willReturn( 'special/logic' );
153
154 $cache = new CachedBagOStuff( $backend );
155
156 $this->assertEquals( 'special/logic', $backend->makeGlobalKey( 'special', 'logic' ) );
157 $this->assertEquals( 'special/logic', $cache->makeGlobalKey( 'special', 'logic' ) );
158 }
159}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
testMakeKey()
CachedBagOStuff::makeKey.
testExpire()
CachedBagOStuff::deleteObjectsExpiringBefore.
testSetDebug()
CachedBagOStuff::setDebug.
testCacheBackendMisses()
CachedBagOStuff::get.
testMakeGlobalKey()
CachedBagOStuff::makeGlobalKey.
testSetAndDelete()
CachedBagOStuff::set CachedBagOStuff::delete.
testWriteCacheOnly()
CachedBagOStuff::set CachedBagOStuff::delete.
testGetFromBackend()
CachedBagOStuff::__construct CachedBagOStuff::get.
Wrapper around a BagOStuff that caches data in memory.
Simple store for keeping values in an associative array for the current process.
$cache
Definition mcc.php:33