]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - tests/phpunit/includes/libs/objectcache/CachedBagOStuffTest.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / tests / phpunit / includes / libs / objectcache / CachedBagOStuffTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6  * @group BagOStuff
7  */
8 class CachedBagOStuffTest extends PHPUnit_Framework_TestCase {
9
10         /**
11          * @covers CachedBagOStuff::__construct
12          * @covers CachedBagOStuff::doGet
13          */
14         public function testGetFromBackend() {
15                 $backend = new HashBagOStuff;
16                 $cache = new CachedBagOStuff( $backend );
17
18                 $backend->set( 'foo', 'bar' );
19                 $this->assertEquals( 'bar', $cache->get( 'foo' ) );
20
21                 $backend->set( 'foo', 'baz' );
22                 $this->assertEquals( 'bar', $cache->get( 'foo' ), 'cached' );
23         }
24
25         /**
26          * @covers CachedBagOStuff::set
27          * @covers CachedBagOStuff::delete
28          */
29         public function testSetAndDelete() {
30                 $backend = new HashBagOStuff;
31                 $cache = new CachedBagOStuff( $backend );
32
33                 for ( $i = 0; $i < 10; $i++ ) {
34                         $cache->set( "key$i", 1 );
35                         $this->assertEquals( 1, $cache->get( "key$i" ) );
36                         $this->assertEquals( 1, $backend->get( "key$i" ) );
37                         $cache->delete( "key$i" );
38                         $this->assertEquals( false, $cache->get( "key$i" ) );
39                         $this->assertEquals( false, $backend->get( "key$i" ) );
40                 }
41         }
42
43         /**
44          * @covers CachedBagOStuff::set
45          * @covers CachedBagOStuff::delete
46          */
47         public function testWriteCacheOnly() {
48                 $backend = new HashBagOStuff;
49                 $cache = new CachedBagOStuff( $backend );
50
51                 $cache->set( 'foo', 'bar', 0, CachedBagOStuff::WRITE_CACHE_ONLY );
52                 $this->assertEquals( 'bar', $cache->get( 'foo' ) );
53                 $this->assertFalse( $backend->get( 'foo' ) );
54
55                 $cache->set( 'foo', 'old' );
56                 $this->assertEquals( 'old', $cache->get( 'foo' ) );
57                 $this->assertEquals( 'old', $backend->get( 'foo' ) );
58
59                 $cache->set( 'foo', 'new', 0, CachedBagOStuff::WRITE_CACHE_ONLY );
60                 $this->assertEquals( 'new', $cache->get( 'foo' ) );
61                 $this->assertEquals( 'old', $backend->get( 'foo' ) );
62
63                 $cache->delete( 'foo', CachedBagOStuff::WRITE_CACHE_ONLY );
64                 $this->assertEquals( 'old', $cache->get( 'foo' ) ); // Reloaded from backend
65         }
66
67         /**
68          * @covers CachedBagOStuff::doGet
69          */
70         public function testCacheBackendMisses() {
71                 $backend = new HashBagOStuff;
72                 $cache = new CachedBagOStuff( $backend );
73
74                 // First hit primes the cache with miss from the backend
75                 $this->assertEquals( false, $cache->get( 'foo' ) );
76
77                 // Change the value in the backend
78                 $backend->set( 'foo', true );
79
80                 // Second hit returns the cached miss
81                 $this->assertEquals( false, $cache->get( 'foo' ) );
82
83                 // But a fresh value is read from the backend
84                 $backend->set( 'bar', true );
85                 $this->assertEquals( true, $cache->get( 'bar' ) );
86         }
87
88         /**
89          * @covers CachedBagOStuff::setDebug
90          */
91         public function testSetDebug() {
92                 $backend = new HashBagOStuff();
93                 $cache = new CachedBagOStuff( $backend );
94                 // Access private property 'debugMode'
95                 $backend = TestingAccessWrapper::newFromObject( $backend );
96                 $cache = TestingAccessWrapper::newFromObject( $cache );
97                 $this->assertFalse( $backend->debugMode );
98                 $this->assertFalse( $cache->debugMode );
99
100                 $cache->setDebug( true );
101                 // Should have set both
102                 $this->assertTrue( $backend->debugMode, 'sets backend' );
103                 $this->assertTrue( $cache->debugMode, 'sets self' );
104         }
105
106         /**
107          * @covers CachedBagOStuff::deleteObjectsExpiringBefore
108          */
109         public function testExpire() {
110                 $backend = $this->getMockBuilder( HashBagOStuff::class )
111                         ->setMethods( [ 'deleteObjectsExpiringBefore' ] )
112                         ->getMock();
113                 $backend->expects( $this->once() )
114                         ->method( 'deleteObjectsExpiringBefore' )
115                         ->willReturn( false );
116
117                 $cache = new CachedBagOStuff( $backend );
118                 $cache->deleteObjectsExpiringBefore( '20110401000000' );
119         }
120
121         /**
122          * @covers CachedBagOStuff::makeKey
123          */
124         public function testMakeKey() {
125                 $backend = $this->getMockBuilder( HashBagOStuff::class )
126                         ->setMethods( [ 'makeKey' ] )
127                         ->getMock();
128                 $backend->method( 'makeKey' )
129                         ->willReturn( 'special/logic' );
130
131                 // CachedBagOStuff wraps any backend with a process cache
132                 // using HashBagOStuff. Hash has no special key limitations,
133                 // but backends often do. Make sure it uses the backend's
134                 // makeKey() logic, not the one inherited from HashBagOStuff
135                 $cache = new CachedBagOStuff( $backend );
136
137                 $this->assertEquals( 'special/logic', $backend->makeKey( 'special', 'logic' ) );
138                 $this->assertEquals( 'special/logic', $cache->makeKey( 'special', 'logic' ) );
139         }
140
141         /**
142          * @covers CachedBagOStuff::makeGlobalKey
143          */
144         public function testMakeGlobalKey() {
145                 $backend = $this->getMockBuilder( HashBagOStuff::class )
146                         ->setMethods( [ 'makeGlobalKey' ] )
147                         ->getMock();
148                 $backend->method( 'makeGlobalKey' )
149                         ->willReturn( 'special/logic' );
150
151                 $cache = new CachedBagOStuff( $backend );
152
153                 $this->assertEquals( 'special/logic', $backend->makeGlobalKey( 'special', 'logic' ) );
154                 $this->assertEquals( 'special/logic', $cache->makeGlobalKey( 'special', 'logic' ) );
155         }
156 }