3 use Wikimedia\TestingAccessWrapper
;
8 class CachedBagOStuffTest
extends PHPUnit_Framework_TestCase
{
11 * @covers CachedBagOStuff::__construct
12 * @covers CachedBagOStuff::doGet
14 public function testGetFromBackend() {
15 $backend = new HashBagOStuff
;
16 $cache = new CachedBagOStuff( $backend );
18 $backend->set( 'foo', 'bar' );
19 $this->assertEquals( 'bar', $cache->get( 'foo' ) );
21 $backend->set( 'foo', 'baz' );
22 $this->assertEquals( 'bar', $cache->get( 'foo' ), 'cached' );
26 * @covers CachedBagOStuff::set
27 * @covers CachedBagOStuff::delete
29 public function testSetAndDelete() {
30 $backend = new HashBagOStuff
;
31 $cache = new CachedBagOStuff( $backend );
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" ) );
44 * @covers CachedBagOStuff::set
45 * @covers CachedBagOStuff::delete
47 public function testWriteCacheOnly() {
48 $backend = new HashBagOStuff
;
49 $cache = new CachedBagOStuff( $backend );
51 $cache->set( 'foo', 'bar', 0, CachedBagOStuff
::WRITE_CACHE_ONLY
);
52 $this->assertEquals( 'bar', $cache->get( 'foo' ) );
53 $this->assertFalse( $backend->get( 'foo' ) );
55 $cache->set( 'foo', 'old' );
56 $this->assertEquals( 'old', $cache->get( 'foo' ) );
57 $this->assertEquals( 'old', $backend->get( 'foo' ) );
59 $cache->set( 'foo', 'new', 0, CachedBagOStuff
::WRITE_CACHE_ONLY
);
60 $this->assertEquals( 'new', $cache->get( 'foo' ) );
61 $this->assertEquals( 'old', $backend->get( 'foo' ) );
63 $cache->delete( 'foo', CachedBagOStuff
::WRITE_CACHE_ONLY
);
64 $this->assertEquals( 'old', $cache->get( 'foo' ) ); // Reloaded from backend
68 * @covers CachedBagOStuff::doGet
70 public function testCacheBackendMisses() {
71 $backend = new HashBagOStuff
;
72 $cache = new CachedBagOStuff( $backend );
74 // First hit primes the cache with miss from the backend
75 $this->assertEquals( false, $cache->get( 'foo' ) );
77 // Change the value in the backend
78 $backend->set( 'foo', true );
80 // Second hit returns the cached miss
81 $this->assertEquals( false, $cache->get( 'foo' ) );
83 // But a fresh value is read from the backend
84 $backend->set( 'bar', true );
85 $this->assertEquals( true, $cache->get( 'bar' ) );
89 * @covers CachedBagOStuff::setDebug
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
);
100 $cache->setDebug( true );
101 // Should have set both
102 $this->assertTrue( $backend->debugMode
, 'sets backend' );
103 $this->assertTrue( $cache->debugMode
, 'sets self' );
107 * @covers CachedBagOStuff::deleteObjectsExpiringBefore
109 public function testExpire() {
110 $backend = $this->getMockBuilder( HashBagOStuff
::class )
111 ->setMethods( [ 'deleteObjectsExpiringBefore' ] )
113 $backend->expects( $this->once() )
114 ->method( 'deleteObjectsExpiringBefore' )
115 ->willReturn( false );
117 $cache = new CachedBagOStuff( $backend );
118 $cache->deleteObjectsExpiringBefore( '20110401000000' );
122 * @covers CachedBagOStuff::makeKey
124 public function testMakeKey() {
125 $backend = $this->getMockBuilder( HashBagOStuff
::class )
126 ->setMethods( [ 'makeKey' ] )
128 $backend->method( 'makeKey' )
129 ->willReturn( 'special/logic' );
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 );
137 $this->assertEquals( 'special/logic', $backend->makeKey( 'special', 'logic' ) );
138 $this->assertEquals( 'special/logic', $cache->makeKey( 'special', 'logic' ) );
142 * @covers CachedBagOStuff::makeGlobalKey
144 public function testMakeGlobalKey() {
145 $backend = $this->getMockBuilder( HashBagOStuff
::class )
146 ->setMethods( [ 'makeGlobalKey' ] )
148 $backend->method( 'makeGlobalKey' )
149 ->willReturn( 'special/logic' );
151 $cache = new CachedBagOStuff( $backend );
153 $this->assertEquals( 'special/logic', $backend->makeGlobalKey( 'special', 'logic' ) );
154 $this->assertEquals( 'special/logic', $cache->makeGlobalKey( 'special', 'logic' ) );