6 class CachedBagOStuffTest
extends PHPUnit_Framework_TestCase
{
8 public function testGetFromBackend() {
9 $backend = new HashBagOStuff
;
10 $cache = new CachedBagOStuff( $backend );
12 $backend->set( 'foo', 'bar' );
13 $this->assertEquals( 'bar', $cache->get( 'foo' ) );
15 $backend->set( 'foo', 'baz' );
16 $this->assertEquals( 'bar', $cache->get( 'foo' ), 'cached' );
19 public function testSetAndDelete() {
20 $backend = new HashBagOStuff
;
21 $cache = new CachedBagOStuff( $backend );
23 for ( $i = 0; $i < 10; $i++
) {
24 $cache->set( "key$i", 1 );
25 $this->assertEquals( 1, $cache->get( "key$i" ) );
26 $this->assertEquals( 1, $backend->get( "key$i" ) );
27 $cache->delete( "key$i" );
28 $this->assertEquals( false, $cache->get( "key$i" ) );
29 $this->assertEquals( false, $backend->get( "key$i" ) );
33 public function testWriteCacheOnly() {
34 $backend = new HashBagOStuff
;
35 $cache = new CachedBagOStuff( $backend );
37 $cache->set( 'foo', 'bar', 0, CachedBagOStuff
::WRITE_CACHE_ONLY
);
38 $this->assertEquals( 'bar', $cache->get( 'foo' ) );
39 $this->assertFalse( $backend->get( 'foo' ) );
41 $cache->set( 'foo', 'old' );
42 $this->assertEquals( 'old', $cache->get( 'foo' ) );
43 $this->assertEquals( 'old', $backend->get( 'foo' ) );
45 $cache->set( 'foo', 'new', 0, CachedBagOStuff
::WRITE_CACHE_ONLY
);
46 $this->assertEquals( 'new', $cache->get( 'foo' ) );
47 $this->assertEquals( 'old', $backend->get( 'foo' ) );
49 $cache->delete( 'foo', CachedBagOStuff
::WRITE_CACHE_ONLY
);
50 $this->assertEquals( 'old', $cache->get( 'foo' ) ); // Reloaded from backend
53 public function testCacheBackendMisses() {
54 $backend = new HashBagOStuff
;
55 $cache = new CachedBagOStuff( $backend );
57 // First hit primes the cache with miss from the backend
58 $this->assertEquals( false, $cache->get( 'foo' ) );
60 // Change the value in the backend
61 $backend->set( 'foo', true );
63 // Second hit returns the cached miss
64 $this->assertEquals( false, $cache->get( 'foo' ) );
66 // But a fresh value is read from the backend
67 $backend->set( 'bar', true );
68 $this->assertEquals( true, $cache->get( 'bar' ) );