6 class HashBagOStuffTest
extends PHPUnit_Framework_TestCase
{
8 public function testEvictionOrder() {
9 $cache = new HashBagOStuff( array( 'maxKeys' => 10 ) );
10 for ( $i = 0; $i < 10; $i++
) {
11 $cache->set( "key$i", 1 );
12 $this->assertEquals( 1, $cache->get( "key$i" ) );
14 for ( $i = 10; $i < 20; $i++
) {
15 $cache->set( "key$i", 1 );
16 $this->assertEquals( 1, $cache->get( "key$i" ) );
17 $this->assertEquals( false, $cache->get( "key" . $i - 10 ) );
21 public function testExpire() {
22 $cache = new HashBagOStuff();
23 $cacheInternal = TestingAccessWrapper
::newFromObject( $cache );
24 $cache->set( 'foo', 1 );
25 $cache->set( 'bar', 1, 10 );
26 $cache->set( 'baz', 1, -10 );
28 $this->assertEquals( 0, $cacheInternal->bag
['foo'][$cache::KEY_EXP
], 'Indefinite' );
29 // 2 seconds tolerance
30 $this->assertEquals( time() +
10, $cacheInternal->bag
['bar'][$cache::KEY_EXP
], 'Future', 2 );
31 $this->assertEquals( time() - 10, $cacheInternal->bag
['baz'][$cache::KEY_EXP
], 'Past', 2 );
33 $this->assertEquals( 1, $cache->get( 'bar' ), 'Key not expired' );
34 $this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' );
37 public function testKeyOrder() {
38 $cache = new HashBagOStuff( array( 'maxKeys' => 3 ) );
40 foreach ( array( 'foo', 'bar', 'baz' ) as $key ) {
41 $cache->set( $key, 1 );
45 $cache->set( 'foo', 1 );
47 // Add a 4th key (beyond the allowed maximum)
48 $cache->set( 'quux', 1 );
50 // Foo's life should have been extended over Bar
51 foreach ( array( 'foo', 'baz', 'quux' ) as $key ) {
52 $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
54 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );