Merge "Improve MIME detection in FileBackend"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / HashBagOStuffTest.php
1 <?php
2
3 /**
4 * @group BagOStuff
5 */
6 class HashBagOStuffTest extends PHPUnit_Framework_TestCase {
7
8 public function testDelete() {
9 $cache = new HashBagOStuff();
10 for ( $i = 0; $i < 10; $i++ ) {
11 $cache->set( "key$i", 1 );
12 $this->assertEquals( 1, $cache->get( "key$i" ) );
13 $cache->delete( "key$i" );
14 $this->assertEquals( false, $cache->get( "key$i" ) );
15 }
16 }
17
18 public function testClear() {
19 $cache = new HashBagOStuff();
20 for ( $i = 0; $i < 10; $i++ ) {
21 $cache->set( "key$i", 1 );
22 $this->assertEquals( 1, $cache->get( "key$i" ) );
23 }
24 $cache->clear();
25 for ( $i = 0; $i < 10; $i++ ) {
26 $this->assertEquals( false, $cache->get( "key$i" ) );
27 }
28 }
29
30 public function testEvictionOrder() {
31 $cache = new HashBagOStuff( array( 'maxKeys' => 10 ) );
32 for ( $i = 0; $i < 10; $i++ ) {
33 $cache->set( "key$i", 1 );
34 $this->assertEquals( 1, $cache->get( "key$i" ) );
35 }
36 for ( $i = 10; $i < 20; $i++ ) {
37 $cache->set( "key$i", 1 );
38 $this->assertEquals( 1, $cache->get( "key$i" ) );
39 $this->assertEquals( false, $cache->get( "key" . $i - 10 ) );
40 }
41 }
42
43 public function testExpire() {
44 $cache = new HashBagOStuff();
45 $cacheInternal = TestingAccessWrapper::newFromObject( $cache );
46 $cache->set( 'foo', 1 );
47 $cache->set( 'bar', 1, 10 );
48 $cache->set( 'baz', 1, -10 );
49
50 $this->assertEquals( 0, $cacheInternal->bag['foo'][$cache::KEY_EXP], 'Indefinite' );
51 // 2 seconds tolerance
52 $this->assertEquals( time() + 10, $cacheInternal->bag['bar'][$cache::KEY_EXP], 'Future', 2 );
53 $this->assertEquals( time() - 10, $cacheInternal->bag['baz'][$cache::KEY_EXP], 'Past', 2 );
54
55 $this->assertEquals( 1, $cache->get( 'bar' ), 'Key not expired' );
56 $this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' );
57 }
58
59 public function testKeyOrder() {
60 $cache = new HashBagOStuff( array( 'maxKeys' => 3 ) );
61
62 foreach ( array( 'foo', 'bar', 'baz' ) as $key ) {
63 $cache->set( $key, 1 );
64 }
65
66 // Set existing key
67 $cache->set( 'foo', 1 );
68
69 // Add a 4th key (beyond the allowed maximum)
70 $cache->set( 'quux', 1 );
71
72 // Foo's life should have been extended over Bar
73 foreach ( array( 'foo', 'baz', 'quux' ) as $key ) {
74 $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
75 }
76 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
77 }
78 }