6 class HashBagOStuffTest
extends PHPUnit_Framework_TestCase
{
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" ) );
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" ) );
25 for ( $i = 0; $i < 10; $i++
) {
26 $this->assertEquals( false, $cache->get( "key$i" ) );
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" ) );
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 ) );
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 );
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 );
55 $this->assertEquals( 1, $cache->get( 'bar' ), 'Key not expired' );
56 $this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' );
59 public function testKeyOrder() {
60 $cache = new HashBagOStuff( array( 'maxKeys' => 3 ) );
62 foreach ( array( 'foo', 'bar', 'baz' ) as $key ) {
63 $cache->set( $key, 1 );
67 $cache->set( 'foo', 1 );
69 // Add a 4th key (beyond the allowed maximum)
70 $cache->set( 'quux', 1 );
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" );
76 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );