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