FixedInMemoryLruCacheTest::testItemRemoval()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
namespace Onoi\Cache\Tests;
4
5
use Onoi\Cache\FixedInMemoryLruCache;
6
use Onoi\Cache\FixedInMemoryCache;
7
8
/**
9
 * @covers \Onoi\Cache\FixedInMemoryLruCache
10
 *
11
 * @group onoi-cache
12
 *
13
 * @license GNU GPL v2+
14
 * @since 1.0
15
 *
16
 * @author mwjames
17
 */
18
class FixedInMemoryLruCacheTest extends \PHPUnit_Framework_TestCase {
19
20
	public function testCanConstruct() {
21
22
		$this->assertInstanceOf(
23
			'\Onoi\Cache\FixedInMemoryLruCache',
24
			new FixedInMemoryLruCache()
25
		);
26
	}
27
28
	public function testGetName() {
29
30
		$instance = new FixedInMemoryLruCache( 5 );
31
32
		$this->assertInternalType(
33
			'string',
34
			$instance->getName()
35
		);
36
	}
37
38
	public function testItemRemoval() {
39
40
		$instance = new FixedInMemoryLruCache( 5 );
41
42
		$instance->save( 'foo', array( 'foo' ) );
43
		$instance->save( 42, null );
44
45
		$this->assertTrue( $instance->contains( 'foo' ) );
46
		$this->assertTrue( $instance->contains( 42 ) );
47
48
		$stats = $instance->getStats();
49
		$this->assertEquals(
50
			2,
51
			$stats['count']
52
		);
53
54
		$instance->delete( 'foo' );
55
56
		$this->assertFalse( $instance->contains( 'foo' ) );
57
		$this->assertFalse( $instance->delete( 'foo' ) );
58
59
		$stats = $instance->getStats();
60
61
		$this->assertEquals(
62
			1,
63
			$stats['count']
64
		);
65
66
		$this->assertEquals(
67
			1,
68
			$stats['deletes']
69
		);
70
	}
71
72
	public function testLeastRecentlyUsedShiftForLimitedCacheSize() {
73
74
		$instance = new FixedInMemoryLruCache( 5 );
75
		$instance->save( 'abcde', array( 'abcde' ) );
76
77
		$this->assertEquals(
78
			array( 'abcde' ),
79
			$instance->fetch( 'abcde' )
80
		);
81
82
		foreach ( array( 'éèêë', 'アイウエオ', 'АБВГД', 'αβγδε', '12345' ) as $alphabet ) {
83
			$instance->save( $alphabet, array( $alphabet ) );
84
		}
85
86
		// 'éèêë' was added and removes 'abcde' from the cache
87
		$this->assertFalse( $instance->fetch( 'abcde' ) );
88
89
		$stats = $instance->getStats();
90
91
		$this->assertEquals(
92
			5,
93
			$stats['count']
94
		);
95
96
		$this->assertEquals(
97
			6,
98
			$stats['inserts']
99
		);
100
101
		// 'éèêë' moves to the top (last postion as most recently used) and
102
		// 'アイウエオ' becomes the next LRU candidate
103
		$this->assertEquals(
104
			array( 'éèêë' ),
105
			$instance->fetch( 'éèêë' )
106
		);
107
108
		$instance->save( '@#$%&', '@#$%&' );
109
		$this->assertFalse( $instance->fetch( 'アイウエオ' ) );
110
111
		// АБВГД would be the next LRU slot but setting it again will move it to MRU
112
		// and push αβγδε into the next LRU position
113
		$instance->save( 'АБВГД', 'АБВГД' );
114
115
		$instance->save( '-+=<>', '-+=<>' );
116
		$this->assertFalse( $instance->fetch( 'αβγδε' ) );
117
118
		$stats = $instance->getStats();
119
120
		$this->assertEquals(
121
			5,
122
			$stats['count']
123
		);
124
	}
125
126
	public function testFetchTtlBasedItem() {
127
128
		$instance = new FixedInMemoryLruCache( 5 );
129
130
		$instance->save( 'foo', 'Bar', 3 );
131
132
		$this->assertTrue(
133
			$instance->contains( 'foo' )
134
		);
135
136
		$this->assertEquals(
137
			'Bar',
138
			$instance->fetch( 'foo' )
139
		);
140
141
		sleep( 4 );
142
143
		$this->assertFalse(
144
			$instance->contains( 'foo' )
145
		);
146
147
		$this->assertFalse(
148
			$instance->fetch( 'foo' )
149
		);
150
	}
151
152
	/**
153
	 * @dataProvider ttlConstantProvider
154
	 */
155
	public function testTtlConstantAccess( $ttl ) {
156
157
		$instance = new FixedInMemoryLruCache( 5 );
158
		$instance->save( 'foo', 'Bar', $ttl );
159
160
		$this->assertEquals(
161
			'Bar',
162
			$instance->fetch( 'foo' )
163
		);
164
	}
165
166
	public function ttlConstantProvider() {
167
168
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
169
			FixedInMemoryLruCache::TTL_MINUTE
170
		);
171
172
		$provider[] = array(
173
			FixedInMemoryLruCache::TTL_HOUR
174
		);
175
176
		$provider[] = array(
177
			FixedInMemoryLruCache::TTL_DAY
178
		);
179
180
		$provider[] = array(
181
			FixedInMemoryLruCache::TTL_WEEK
182
		);
183
184
		$provider[] = array(
185
			FixedInMemoryLruCache::TTL_MONTH
186
		);
187
188
		$provider[] = array(
189
			FixedInMemoryLruCache::TTL_YEAR
190
		);
191
192
		return $provider;
193
	}
194
195
}
196