forked from ipinfo/php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultCacheTest.php
More file actions
216 lines (175 loc) · 7.34 KB
/
DefaultCacheTest.php
File metadata and controls
216 lines (175 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
namespace ipinfo\ipinfo\tests;
use ipinfo\ipinfo\cache\DefaultCache;
use PHPUnit\Framework\TestCase;
class DefaultCacheTest extends TestCase
{
public function testHasValue()
{
$cache = new DefaultCache($maxsize = 4, $ttl = 2);
$key1 = 'test';
$value1 = 'obama';
$cache->set($key1, $value1);
$key2 = 'test2';
$value2 = 'mccain';
$cache->set($key2, $value2);
$this->assertTrue($cache->has($key1));
$this->assertTrue($cache->has($key2));
}
public function testDoesNotHaveValue()
{
$cache = new DefaultCache($maxsize = 4, $ttl = 2);
$key = 'test';
$this->assertFalse($cache->has($key));
}
public function testGetValue()
{
$cache = new DefaultCache($maxsize = 4, $ttl = 2);
$key1 = 'test';
$value1 = 'obama';
$cache->set($key1, $value1);
$key2 = 'test2';
$value2 = 'mccain';
$cache->set($key2, $value2);
$this->assertEquals($value1, $cache->get($key1));
$this->assertEquals($value2, $cache->get($key2));
}
public function testMaxSizeExceeded()
{
$cache = new DefaultCache($maxsize = 2, $ttl = 2);
$key1 = 'test';
$value1 = 'obama';
$cache->set($key1, $value1);
$this->assertEquals($value1, $cache->get($key1));
$key2 = 'test2';
$value2 = 'mccain';
$cache->set($key2, $value2);
$this->assertEquals($value2, $cache->get($key2));
// Test that once the maxsize is exceeded, the earliest item is pushed out.
$key3 = 'test3';
$value3 = 'gore';
$cache->set($key3, $value3);
$this->assertEquals(null, $cache->get($key1));
$this->assertEquals($value2, $cache->get($key2));
$this->assertEquals($value3, $cache->get($key3));
}
public function testTimeToLiveExceeded()
{
$cache = new DefaultCache($maxsize = 2, $ttl = 1);
$key = 'test';
$value = 'obama';
$cache->set($key, $value);
$this->assertEquals($value, $cache->get($key));
// Let the TTL expire.
sleep(2);
$this->assertEquals(null, $cache->get($key));
}
public function testCacheWithIPv6DifferentNotations()
{
// Create cache instance
$cache = new DefaultCache(10, 600);
// Original IPv6 address
$standard_ip = "2607:f8b0:4005:805::200e";
$standard_value = "standard_value";
$cache->set($standard_ip, $standard_value);
// Variations with zeros
$variations = [
"2607:f8b0:4005:805:0:0:0:200e", // Full form
"2607:f8b0:4005:805:0000:0000:0000:200e", // Full form with leading zeros
"2607:f8b0:4005:805:0000:00:00:200e", // Full form with few leading zeros
"2607:F8B0:4005:805::200E", // Uppercase notation
"2607:f8b0:4005:0805::200e", // Leading zero in a group
"2607:f8b0:4005:805:0::200e", // Partially expanded
"2607:f8b0:4005:805:0000::200e", // Full zeros in a second group
];
// DefaultCache does normalize IPs, so we need to check if the cache has the same value
foreach ($variations as $ip) {
$this->assertTrue($cache->has($ip), "Cache should have variation: $ip");
}
}
public function testDefaultCacheWithIPv4AndPostfixes()
{
// Create cache
$cache = new DefaultCache(5, 600);
// Test IPv4 with various postfixes
$ipv4 = '8.8.8.8';
$postfixes = ['_v1', '_latest', '_beta', '_test_123'];
foreach ($postfixes as $postfix) {
$key = $ipv4 . $postfix;
$value = "value_for_$key";
// Set value with postfix
$cache->set($key, $value);
// Verify it's in cache
$this->assertTrue($cache->has($key), "Cache should have key with postfix: $key");
$this->assertEquals($value, $cache->get($key), "Should get correct value for key with postfix");
// Verify base IP is not affected
if (!$cache->has($ipv4)) {
$cache->set($ipv4, "base_ip_value");
}
$this->assertNotEquals($cache->get($ipv4), $cache->get($key), "Base IP and IP with postfix should have different values");
}
// Check all keys are still available (capacity not exceeded)
foreach ($postfixes as $postfix) {
$this->assertTrue($cache->has($ipv4 . $postfix), "All postfix keys should be available");
}
$this->assertTrue($cache->has($ipv4), "Base IP should be available");
}
public function testCacheWithIPv6AndPostfixes()
{
// Create cache
$cache = new DefaultCache(5, 600);
// Test IPv6 with various postfixes
$ipv6 = '2607:f8b0:4005:805::200e';
$postfixes = ['_v1', '_latest', '_beta', '_test_123'];
foreach ($postfixes as $postfix) {
$key = $ipv6 . $postfix;
$value = "value_for_$key";
// Set value with postfix
$cache->set($key, $value);
// Verify it's in cache
$this->assertTrue($cache->has($key), "Cache should have key with postfix: $key");
$this->assertEquals($value, $cache->get($key), "Should get correct value for key with postfix");
}
// Add the base IP to cache if not present
if (!$cache->has($ipv6)) {
$cache->set($ipv6, "base_ip_value");
}
// Ensure all keys are distinct and have different values
$this->assertEquals("base_ip_value", $cache->get($ipv6), "Base IP should have its own value");
foreach ($postfixes as $postfix) {
$key = $ipv6 . $postfix;
$expected = "value_for_$key";
$this->assertEquals($expected, $cache->get($key), "Each postfix should have its own value");
}
}
public function testCacheWithIPv6NotationsAndPostfixes()
{
// Create cache instance
$cache = new DefaultCache(100, 600);
// Original IPv6 address
$standard_ip = "2607:f8b0:4005:805::200e";
$postfixes = ['_v1', '_latest', '_beta', '_test_123'];
// Variations with zeros
$variations = [
"2607:f8b0:4005:805:0:0:0:200e", // Full form
"2607:f8b0:4005:805:0000:0000:0000:200e", // Full form with leading zeros
"2607:f8b0:4005:805:0000:00:00:200e", // Full form with few leading zeros
"2607:F8B0:4005:805::200E", // Uppercase notation
"2607:f8b0:4005:0805::200e", // Leading zero in a group
"2607:f8b0:4005:805:0::200e", // Partially expanded
"2607:f8b0:4005:805:0000::200e", // Full zeros in a second group
];
foreach ($postfixes as $postfix) {
// Set cache with first postfix
$value = "value_for_$standard_ip";
$cache->set($standard_ip . $postfix, $value);
foreach ($variations as $variation_id => $ip) {
$key = $ip . $postfix;
$this->assertTrue($cache->has($key), "Cache should have variation #$variation_id with postfix: $key");
$this->assertEquals($value, $cache->get($key), "Should get correct value for key with postfix");
}
}
// Check that the base IP not cached
$this->assertFalse($cache->has($standard_ip), "Base IP should not be cached");
}
}