6 class HashBagOStuffTest
extends PHPUnit_Framework_TestCase
{
9 * @covers HashBagOStuff::__construct
11 public function testConstruct() {
12 $this->assertInstanceOf(
19 * @covers HashBagOStuff::__construct
20 * @expectedException InvalidArgumentException
22 public function testConstructBadZero() {
23 $cache = new HashBagOStuff( [ 'maxKeys' => 0 ] );
27 * @covers HashBagOStuff::__construct
28 * @expectedException InvalidArgumentException
30 public function testConstructBadNeg() {
31 $cache = new HashBagOStuff( [ 'maxKeys' => -1 ] );
35 * @covers HashBagOStuff::__construct
36 * @expectedException InvalidArgumentException
38 public function testConstructBadType() {
39 $cache = new HashBagOStuff( [ 'maxKeys' => 'x' ] );
43 * @covers HashBagOStuff::delete
45 public function testDelete() {
46 $cache = new HashBagOStuff();
47 for ( $i = 0; $i < 10; $i++
) {
48 $cache->set( "key$i", 1 );
49 $this->assertEquals( 1, $cache->get( "key$i" ) );
50 $cache->delete( "key$i" );
51 $this->assertEquals( false, $cache->get( "key$i" ) );
56 * @covers HashBagOStuff::clear
58 public function testClear() {
59 $cache = new HashBagOStuff();
60 for ( $i = 0; $i < 10; $i++
) {
61 $cache->set( "key$i", 1 );
62 $this->assertEquals( 1, $cache->get( "key$i" ) );
65 for ( $i = 0; $i < 10; $i++
) {
66 $this->assertEquals( false, $cache->get( "key$i" ) );
71 * @covers HashBagOStuff::doGet
72 * @covers HashBagOStuff::expire
74 public function testExpire() {
75 $cache = new HashBagOStuff();
76 $cacheInternal = TestingAccessWrapper
::newFromObject( $cache );
77 $cache->set( 'foo', 1 );
78 $cache->set( 'bar', 1, 10 );
79 $cache->set( 'baz', 1, -10 );
81 $this->assertEquals( 0, $cacheInternal->bag
['foo'][$cache::KEY_EXP
], 'Indefinite' );
82 // 2 seconds tolerance
83 $this->assertEquals( time() +
10, $cacheInternal->bag
['bar'][$cache::KEY_EXP
], 'Future', 2 );
84 $this->assertEquals( time() - 10, $cacheInternal->bag
['baz'][$cache::KEY_EXP
], 'Past', 2 );
86 $this->assertEquals( 1, $cache->get( 'bar' ), 'Key not expired' );
87 $this->assertEquals( false, $cache->get( 'baz' ), 'Key expired' );
91 * Ensure maxKeys eviction prefers keeping new keys.
93 * @covers HashBagOStuff::set
95 public function testEvictionAdd() {
96 $cache = new HashBagOStuff( [ 'maxKeys' => 10 ] );
97 for ( $i = 0; $i < 10; $i++
) {
98 $cache->set( "key$i", 1 );
99 $this->assertEquals( 1, $cache->get( "key$i" ) );
101 for ( $i = 10; $i < 20; $i++
) {
102 $cache->set( "key$i", 1 );
103 $this->assertEquals( 1, $cache->get( "key$i" ) );
104 $this->assertEquals( false, $cache->get( "key" . $i - 10 ) );
109 * Ensure maxKeys eviction prefers recently set keys
110 * even if the keys pre-exist.
112 * @covers HashBagOStuff::set
114 public function testEvictionSet() {
115 $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
117 foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
118 $cache->set( $key, 1 );
122 $cache->set( 'foo', 1 );
124 // Add a 4th key (beyond the allowed maximum)
125 $cache->set( 'quux', 1 );
127 // Foo's life should have been extended over Bar
128 foreach ( [ 'foo', 'baz', 'quux' ] as $key ) {
129 $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
131 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );
135 * Ensure maxKeys eviction prefers recently retrieved keys (LRU).
137 * @covers HashBagOStuff::doGet
138 * @covers HashBagOStuff::hasKey
140 public function testEvictionGet() {
141 $cache = new HashBagOStuff( [ 'maxKeys' => 3 ] );
143 foreach ( [ 'foo', 'bar', 'baz' ] as $key ) {
144 $cache->set( $key, 1 );
148 $cache->get( 'foo', 1 );
150 // Add a 4th key (beyond the allowed maximum)
151 $cache->set( 'quux', 1 );
153 // Foo's life should have been extended over Bar
154 foreach ( [ 'foo', 'baz', 'quux' ] as $key ) {
155 $this->assertEquals( 1, $cache->get( $key ), "Kept $key" );
157 $this->assertEquals( false, $cache->get( 'bar' ), 'Evicted bar' );