3 use Wikimedia\TestingAccessWrapper
;
8 class CachedBagOStuffTest
extends PHPUnit_Framework_TestCase
{
10 use MediaWikiCoversValidator
;
13 * @covers CachedBagOStuff::__construct
14 * @covers CachedBagOStuff::doGet
16 public function testGetFromBackend() {
17 $backend = new HashBagOStuff
;
18 $cache = new CachedBagOStuff( $backend );
20 $backend->set( 'foo', 'bar' );
21 $this->assertEquals( 'bar', $cache->get( 'foo' ) );
23 $backend->set( 'foo', 'baz' );
24 $this->assertEquals( 'bar', $cache->get( 'foo' ), 'cached' );
28 * @covers CachedBagOStuff::set
29 * @covers CachedBagOStuff::delete
31 public function testSetAndDelete() {
32 $backend = new HashBagOStuff
;
33 $cache = new CachedBagOStuff( $backend );
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 $cache->delete( "key$i" );
40 $this->assertEquals( false, $cache->get( "key$i" ) );
41 $this->assertEquals( false, $backend->get( "key$i" ) );
46 * @covers CachedBagOStuff::set
47 * @covers CachedBagOStuff::delete
49 public function testWriteCacheOnly() {
50 $backend = new HashBagOStuff
;
51 $cache = new CachedBagOStuff( $backend );
53 $cache->set( 'foo', 'bar', 0, CachedBagOStuff
::WRITE_CACHE_ONLY
);
54 $this->assertEquals( 'bar', $cache->get( 'foo' ) );
55 $this->assertFalse( $backend->get( 'foo' ) );
57 $cache->set( 'foo', 'old' );
58 $this->assertEquals( 'old', $cache->get( 'foo' ) );
59 $this->assertEquals( 'old', $backend->get( 'foo' ) );
61 $cache->set( 'foo', 'new', 0, CachedBagOStuff
::WRITE_CACHE_ONLY
);
62 $this->assertEquals( 'new', $cache->get( 'foo' ) );
63 $this->assertEquals( 'old', $backend->get( 'foo' ) );
65 $cache->delete( 'foo', CachedBagOStuff
::WRITE_CACHE_ONLY
);
66 $this->assertEquals( 'old', $cache->get( 'foo' ) ); // Reloaded from backend
70 * @covers CachedBagOStuff::doGet
72 public function testCacheBackendMisses() {
73 $backend = new HashBagOStuff
;
74 $cache = new CachedBagOStuff( $backend );
76 // First hit primes the cache with miss from the backend
77 $this->assertEquals( false, $cache->get( 'foo' ) );
79 // Change the value in the backend
80 $backend->set( 'foo', true );
82 // Second hit returns the cached miss
83 $this->assertEquals( false, $cache->get( 'foo' ) );
85 // But a fresh value is read from the backend
86 $backend->set( 'bar', true );
87 $this->assertEquals( true, $cache->get( 'bar' ) );
91 * @covers CachedBagOStuff::setDebug
93 public function testSetDebug() {
94 $backend = new HashBagOStuff();
95 $cache = new CachedBagOStuff( $backend );
96 // Access private property 'debugMode'
97 $backend = TestingAccessWrapper
::newFromObject( $backend );
98 $cache = TestingAccessWrapper
::newFromObject( $cache );
99 $this->assertFalse( $backend->debugMode
);
100 $this->assertFalse( $cache->debugMode
);
102 $cache->setDebug( true );
103 // Should have set both
104 $this->assertTrue( $backend->debugMode
, 'sets backend' );
105 $this->assertTrue( $cache->debugMode
, 'sets self' );
109 * @covers CachedBagOStuff::deleteObjectsExpiringBefore
111 public function testExpire() {
112 $backend = $this->getMockBuilder( HashBagOStuff
::class )
113 ->setMethods( [ 'deleteObjectsExpiringBefore' ] )
115 $backend->expects( $this->once() )
116 ->method( 'deleteObjectsExpiringBefore' )
117 ->willReturn( false );
119 $cache = new CachedBagOStuff( $backend );
120 $cache->deleteObjectsExpiringBefore( '20110401000000' );
124 * @covers CachedBagOStuff::makeKey
126 public function testMakeKey() {
127 $backend = $this->getMockBuilder( HashBagOStuff
::class )
128 ->setMethods( [ 'makeKey' ] )
130 $backend->method( 'makeKey' )
131 ->willReturn( 'special/logic' );
133 // CachedBagOStuff wraps any backend with a process cache
134 // using HashBagOStuff. Hash has no special key limitations,
135 // but backends often do. Make sure it uses the backend's
136 // makeKey() logic, not the one inherited from HashBagOStuff
137 $cache = new CachedBagOStuff( $backend );
139 $this->assertEquals( 'special/logic', $backend->makeKey( 'special', 'logic' ) );
140 $this->assertEquals( 'special/logic', $cache->makeKey( 'special', 'logic' ) );
144 * @covers CachedBagOStuff::makeGlobalKey
146 public function testMakeGlobalKey() {
147 $backend = $this->getMockBuilder( HashBagOStuff
::class )
148 ->setMethods( [ 'makeGlobalKey' ] )
150 $backend->method( 'makeGlobalKey' )
151 ->willReturn( 'special/logic' );
153 $cache = new CachedBagOStuff( $backend );
155 $this->assertEquals( 'special/logic', $backend->makeGlobalKey( 'special', 'logic' ) );
156 $this->assertEquals( 'special/logic', $cache->makeGlobalKey( 'special', 'logic' ) );