Merge "Add additional tracking information to mediawiki.searchSuggest"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / CachedBagOStuffTest.php
1 <?php
2
3 /**
4 * @group BagOStuff
5 */
6 class CachedBagOStuffTest extends PHPUnit_Framework_TestCase {
7
8 public function testGetFromBackend() {
9 $backend = new HashBagOStuff;
10 $cache = new CachedBagOStuff( $backend );
11
12 $backend->set( 'foo', 'bar' );
13 $this->assertEquals( 'bar', $cache->get( 'foo' ) );
14
15 $backend->set( 'foo', 'baz' );
16 $this->assertEquals( 'bar', $cache->get( 'foo' ), 'cached' );
17 }
18
19 public function testSetAndDelete() {
20 $backend = new HashBagOStuff;
21 $cache = new CachedBagOStuff( $backend );
22
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" ) );
30 }
31 }
32
33 public function testWriteCacheOnly() {
34 $backend = new HashBagOStuff;
35 $cache = new CachedBagOStuff( $backend );
36
37 $cache->set( 'foo', 'bar', 0, CachedBagOStuff::WRITE_CACHE_ONLY );
38 $this->assertEquals( 'bar', $cache->get( 'foo' ) );
39 $this->assertFalse( $backend->get( 'foo' ) );
40
41 $cache->set( 'foo', 'old' );
42 $this->assertEquals( 'old', $cache->get( 'foo' ) );
43 $this->assertEquals( 'old', $backend->get( 'foo' ) );
44
45 $cache->set( 'foo', 'new', 0, CachedBagOStuff::WRITE_CACHE_ONLY );
46 $this->assertEquals( 'new', $cache->get( 'foo' ) );
47 $this->assertEquals( 'old', $backend->get( 'foo' ) );
48
49 $cache->delete( 'foo', CachedBagOStuff::WRITE_CACHE_ONLY );
50 $this->assertEquals( 'old', $cache->get( 'foo' ) ); // Reloaded from backend
51 }
52
53 public function testCacheBackendMisses() {
54 $backend = new HashBagOStuff;
55 $cache = new CachedBagOStuff( $backend );
56
57 // First hit primes the cache with miss from the backend
58 $this->assertEquals( false, $cache->get( 'foo' ) );
59
60 // Change the value in the backend
61 $backend->set( 'foo', true );
62
63 // Second hit returns the cached miss
64 $this->assertEquals( false, $cache->get( 'foo' ) );
65
66 // But a fresh value is read from the backend
67 $backend->set( 'bar', true );
68 $this->assertEquals( true, $cache->get( 'bar' ) );
69 }
70 }