1 | <?php |
||||
2 | |||||
3 | namespace Kodus\Cache\Test\Unit; |
||||
4 | |||||
5 | use DateInterval; |
||||
6 | use Kodus\Cache\MockCache; |
||||
7 | use Kodus\Cache\InvalidArgumentException; |
||||
8 | use TypeError; |
||||
9 | use UnitTester; |
||||
10 | |||||
11 | class MockCacheCest |
||||
12 | { |
||||
13 | const DEFAULT_EXPIRATION = 86400; |
||||
14 | |||||
15 | protected MockCache $cache; |
||||
16 | |||||
17 | public function _before(): void |
||||
18 | { |
||||
19 | $this->cache = new MockCache(self::DEFAULT_EXPIRATION); |
||||
20 | } |
||||
21 | |||||
22 | public function setGetAndDelete(UnitTester $I): void |
||||
23 | { |
||||
24 | $I->assertTrue($this->cache->set("key1", "value1")); |
||||
25 | $I->assertTrue($this->cache->set("key2", "value2")); |
||||
26 | |||||
27 | $I->assertSame("value1", $this->cache->get("key1")); |
||||
28 | $I->assertSame("value2", $this->cache->get("key2")); |
||||
29 | |||||
30 | $I->assertTrue($this->cache->delete("key1")); |
||||
31 | $I->assertFalse($this->cache->delete("key1")); |
||||
32 | |||||
33 | $I->assertSame(null, $this->cache->get("key1")); |
||||
34 | $I->assertSame("value2", $this->cache->get("key2")); |
||||
35 | |||||
36 | $I->expectThrowable(InvalidArgumentException::class, function () { |
||||
37 | $this->cache->set("key@", "value1"); |
||||
38 | }); |
||||
39 | |||||
40 | $I->expectThrowable(InvalidArgumentException::class, function () { |
||||
41 | $this->cache->get("key@"); |
||||
42 | }); |
||||
43 | |||||
44 | $I->expectThrowable(InvalidArgumentException::class, function () { |
||||
45 | $this->cache->delete("key@"); |
||||
46 | }); |
||||
47 | } |
||||
48 | |||||
49 | public function getNonExisting(UnitTester $I): void |
||||
50 | { |
||||
51 | $I->assertSame(null, $this->cache->get("key")); |
||||
52 | $I->assertSame("default", $this->cache->get("key", "default")); |
||||
53 | } |
||||
54 | |||||
55 | public function expirationInSeconds(UnitTester $I): void |
||||
56 | { |
||||
57 | $this->cache->set("key", "value", 10); |
||||
58 | |||||
59 | $this->cache->skipTime(5); |
||||
60 | |||||
61 | $I->assertSame("value", $this->cache->get("key")); |
||||
62 | |||||
63 | $this->cache->skipTime(5); |
||||
64 | |||||
65 | $I->assertSame(null, $this->cache->get("key")); |
||||
66 | $I->assertSame("default", $this->cache->get("key", "default")); |
||||
67 | } |
||||
68 | |||||
69 | public function expirationByInterval(UnitTester $I): void |
||||
70 | { |
||||
71 | $interval = new DateInterval("PT10S"); |
||||
72 | |||||
73 | $this->cache->set("key", "value", $interval); |
||||
74 | |||||
75 | $this->cache->skipTime(5); |
||||
76 | |||||
77 | $I->assertSame("value", $this->cache->get("key")); |
||||
78 | |||||
79 | $this->cache->skipTime(5); |
||||
80 | |||||
81 | $I->assertSame(null, $this->cache->get("key")); |
||||
82 | $I->assertSame("default", $this->cache->get("key", "default")); |
||||
83 | } |
||||
84 | |||||
85 | public function expirationByDefault(UnitTester $I): void |
||||
86 | { |
||||
87 | $this->cache->set("key", "value"); |
||||
88 | |||||
89 | $this->cache->skipTime(self::DEFAULT_EXPIRATION - 5); |
||||
90 | |||||
91 | $I->assertSame("value", $this->cache->get("key")); |
||||
92 | |||||
93 | $this->cache->skipTime(10); |
||||
94 | |||||
95 | $I->assertSame(null, $this->cache->get("key")); |
||||
96 | $I->assertSame("default", $this->cache->get("key", "default")); |
||||
97 | } |
||||
98 | |||||
99 | public function expirationInThePast(UnitTester $I): void |
||||
100 | { |
||||
101 | $this->cache->set("key1", "value1", 0); |
||||
102 | $this->cache->set("key2", "value2", -10); |
||||
103 | |||||
104 | $I->assertSame("default", $this->cache->get("key1", "default")); |
||||
105 | $I->assertSame("default", $this->cache->get("key2", "default")); |
||||
106 | } |
||||
107 | |||||
108 | public function clear(UnitTester $I): void |
||||
109 | { |
||||
110 | // add some values that should be gone when we clear cache: |
||||
111 | |||||
112 | $this->cache->set("key1", "value1"); |
||||
113 | $this->cache->set("key2", "value2"); |
||||
114 | |||||
115 | $this->cache->clear(); |
||||
116 | |||||
117 | // check to confirm everything"s been wiped out: |
||||
118 | |||||
119 | $I->assertSame(null, $this->cache->get("key1")); |
||||
120 | $I->assertSame("default", $this->cache->get("key1", "default")); |
||||
121 | |||||
122 | $I->assertSame(null, $this->cache->get("key2")); |
||||
123 | $I->assertSame("default", $this->cache->get("key2", "default")); |
||||
124 | } |
||||
125 | |||||
126 | public function testGetAndSetMultiple(UnitTester $I): void |
||||
127 | { |
||||
128 | $this->cache->setMultiple(["key1" => "value1", "key2" => "value2"]); |
||||
129 | |||||
130 | $results = $this->cache->getMultiple(["key1", "key2", "key3"], false); |
||||
131 | |||||
132 | $I->assertSame(["key1" => "value1", "key2" => "value2", "key3" => false], $results); |
||||
133 | |||||
134 | $I->expectThrowable(TypeError::class, function () { |
||||
135 | $this->cache->getMultiple("Invalid type"); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
136 | }); |
||||
137 | |||||
138 | $I->expectThrowable(TypeError::class, function () { |
||||
139 | $this->cache->setMultiple("Invalid type"); |
||||
0 ignored issues
–
show
'Invalid type' of type string is incompatible with the type iterable expected by parameter $values of Kodus\Cache\MockCache::setMultiple() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
140 | }); |
||||
141 | |||||
142 | $I->expectThrowable(InvalidArgumentException::class, function () { |
||||
143 | $this->cache->setMultiple(["Invalid key@" => "value1"]); |
||||
144 | }); |
||||
145 | |||||
146 | $I->expectThrowable(InvalidArgumentException::class, function () { |
||||
147 | $this->cache->getMultiple(["Invalid key@"]); |
||||
148 | }); |
||||
149 | } |
||||
150 | |||||
151 | public function testDeleteMultiple(UnitTester $I): void |
||||
152 | { |
||||
153 | $this->cache->setMultiple(["key1" => "value1", "key2" => "value2", "key3" => "value3"]); |
||||
154 | |||||
155 | $this->cache->deleteMultiple(["key1", "key2"]); |
||||
156 | |||||
157 | $I->assertSame(["key1" => null, "key2" => null], $this->cache->getMultiple(["key1", "key2"])); |
||||
158 | |||||
159 | $I->assertSame("value3", $this->cache->get("key3")); |
||||
160 | |||||
161 | $I->expectThrowable(TypeError::class, function () { |
||||
162 | $this->cache->deleteMultiple("Invalid type"); |
||||
0 ignored issues
–
show
'Invalid type' of type string is incompatible with the type iterable expected by parameter $keys of Kodus\Cache\MockCache::deleteMultiple() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
163 | }); |
||||
164 | |||||
165 | $I->expectThrowable(InvalidArgumentException::class, function () { |
||||
166 | $this->cache->deleteMultiple(["Invalid key@"]); |
||||
167 | }); |
||||
168 | } |
||||
169 | |||||
170 | public function testHas(UnitTester $I): void |
||||
171 | { |
||||
172 | $this->cache->set("key", "value"); |
||||
173 | |||||
174 | $I->assertSame(true, $this->cache->has("key")); |
||||
175 | $I->assertSame(false, $this->cache->has("fudge")); |
||||
176 | } |
||||
177 | } |
||||
178 |