Passed
Push — master ( ac99ce...447e68 )
by Alexander
12:41 queued 11:20
created

ApcuCacheTest::testDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Yiisoft\ApcuCache\Tests;
4
5
use DateInterval;
6
use Psr\SimpleCache\CacheInterface;
7
use Psr\SimpleCache\InvalidArgumentException;
8
use ReflectionException;
9
use Yiisoft\ApcuCache\ApcuCache;
10
11
class ApcuCacheTest extends TestCase
12
{
13
    public static function setUpBeforeClass(): void
14
    {
15
        if (!extension_loaded('apcu')) {
16
            self::markTestSkipped('Required extension "apcu" is not loaded');
17
        }
18
19
        if (!ini_get('apc.enable_cli')) {
20
            self::markTestSkipped('APC is installed but not enabled. Enable with "apc.enable_cli=1" from php.ini. Skipping.');
21
        }
22
    }
23
24
    protected function createCacheInstance(): CacheInterface
25
    {
26
        return new ApcuCache();
27
    }
28
29
    /**
30
     * @dataProvider dataProvider
31
     * @param $key
32
     * @param $value
33
     * @throws InvalidArgumentException
34
     */
35
    public function testSet($key, $value): void
36
    {
37
        $cache = $this->createCacheInstance();
38
        $cache->clear();
39
40
        for ($i = 0; $i < 2; $i++) {
41
            $this->assertTrue($cache->set($key, $value));
42
        }
43
    }
44
45
    /**
46
     * @dataProvider dataProvider
47
     * @param $key
48
     * @param $value
49
     * @throws InvalidArgumentException
50
     */
51
    public function testGet($key, $value): void
52
    {
53
        $cache = $this->createCacheInstance();
54
        $cache->clear();
55
56
        $cache->set($key, $value);
57
        $valueFromCache = $cache->get($key, 'default');
58
59
        $this->assertSameExceptObject($value, $valueFromCache);
60
    }
61
62
    /**
63
     * @dataProvider dataProvider
64
     * @param $key
65
     * @param $value
66
     * @throws InvalidArgumentException
67
     */
68
    public function testValueInCacheCannotBeChanged($key, $value): void
69
    {
70
        $cache = $this->createCacheInstance();
71
        $cache->clear();
72
73
        $cache->set($key, $value);
74
        $valueFromCache = $cache->get($key, 'default');
75
76
        $this->assertSameExceptObject($value, $valueFromCache);
77
78
        if (is_object($value)) {
79
            $originalValue = clone $value;
80
            $valueFromCache->test_field = 'changed';
81
            $value->test_field = 'changed';
82
            $valueFromCacheNew = $cache->get($key, 'default');
83
            $this->assertSameExceptObject($originalValue, $valueFromCacheNew);
84
        }
85
    }
86
87
    /**
88
     * @dataProvider dataProvider
89
     * @param $key
90
     * @param $value
91
     * @throws InvalidArgumentException
92
     */
93
    public function testHas($key, $value): void
94
    {
95
        $cache = $this->createCacheInstance();
96
        $cache->clear();
97
98
        $cache->set($key, $value);
99
100
        $this->assertTrue($cache->has($key));
101
        // check whether exists affects the value
102
        $this->assertSameExceptObject($value, $cache->get($key));
103
104
        $this->assertTrue($cache->has($key));
105
        $this->assertFalse($cache->has('not_exists'));
106
    }
107
108
    public function testGetNonExistent(): void
109
    {
110
        $cache = $this->createCacheInstance();
111
        $cache->clear();
112
113
        $this->assertNull($cache->get('non_existent_key'));
114
    }
115
116
    /**
117
     * @dataProvider dataProvider
118
     * @param $key
119
     * @param $value
120
     * @throws InvalidArgumentException
121
     */
122
    public function testDelete($key, $value): void
123
    {
124
        $cache = $this->createCacheInstance();
125
        $cache->clear();
126
127
        $cache->set($key, $value);
128
129
        $this->assertSameExceptObject($value, $cache->get($key));
130
        $this->assertTrue($cache->delete($key));
131
        $this->assertNull($cache->get($key));
132
    }
133
134
    /**
135
     * @dataProvider dataProvider
136
     * @param $key
137
     * @param $value
138
     * @throws InvalidArgumentException
139
     */
140
    public function testClear($key, $value): void
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

140
    public function testClear($key, /** @scrutinizer ignore-unused */ $value): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
    {
142
        $cache = $this->createCacheInstance();
143
        $cache = $this->prepare($cache);
144
145
        $this->assertTrue($cache->clear());
146
        $this->assertNull($cache->get($key));
147
    }
148
149
    /**
150
     * @dataProvider dataProviderSetMultiple
151
     * @param int|null $ttl
152
     * @throws InvalidArgumentException
153
     */
154
    public function testSetMultiple(?int $ttl): void
155
    {
156
        $cache = $this->createCacheInstance();
157
        $cache->clear();
158
159
        $data = $this->getDataProviderData();
160
161
        $cache->setMultiple($data, $ttl);
162
163
        foreach ($data as $key => $value) {
164
            $this->assertSameExceptObject($value, $cache->get($key));
165
        }
166
    }
167
168
    /**
169
     * @return array testing multiSet with and without expiry
170
     */
171
    public function dataProviderSetMultiple(): array
172
    {
173
        return [
174
            [null],
175
            [2],
176
        ];
177
    }
178
179
    public function testGetMultiple(): void
180
    {
181
        $cache = $this->createCacheInstance();
182
        $cache->clear();
183
184
        $data = $this->getDataProviderData();
185
186
        $cache->setMultiple($data);
187
188
        $this->assertSameExceptObject($data, $cache->getMultiple(array_keys($data)));
189
    }
190
191
    public function testDeleteMultiple(): void
192
    {
193
        $cache = $this->createCacheInstance();
194
        $cache->clear();
195
196
        $data = $this->getDataProviderData();
197
198
        $cache->setMultiple($data);
199
200
        $this->assertSameExceptObject($data, $cache->getMultiple(array_keys($data)));
201
202
        $cache->deleteMultiple(array_keys($data));
203
204
        $emptyData = array_map(static function ($v) {
0 ignored issues
show
Unused Code introduced by
The parameter $v is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

204
        $emptyData = array_map(static function (/** @scrutinizer ignore-unused */ $v) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
205
            return null;
206
        }, $data);
207
208
        $this->assertSameExceptObject($emptyData, $cache->getMultiple(array_keys($data)));
209
    }
210
211
    public function testNegativeTtl(): void
212
    {
213
        $cache = $this->createCacheInstance();
214
        $cache->clear();
215
        $cache->setMultiple([
216
            'a' => 1,
217
            'b' => 2,
218
        ]);
219
220
        $this->assertTrue($cache->has('a'));
221
        $this->assertTrue($cache->has('b'));
222
223
        $cache->set('a', 11, -1);
224
225
        $this->assertFalse($cache->has('a'));
226
    }
227
228
    /**
229
     * @dataProvider dataProviderNormalizeTtl
230
     * @param mixed $ttl
231
     * @param mixed $expectedResult
232
     * @throws ReflectionException
233
     */
234
    public function testNormalizeTtl($ttl, $expectedResult): void
235
    {
236
        $cache = new ApcuCache();
237
        $this->assertSameExceptObject($expectedResult, $this->invokeMethod($cache, 'normalizeTtl', [$ttl]));
238
    }
239
240
    /**
241
     * Data provider for {@see testNormalizeTtl()}
242
     * @return array test data
243
     *
244
     * @throws \Exception
245
     */
246
    public function dataProviderNormalizeTtl(): array
247
    {
248
        return [
249
            [123, 123],
250
            ['123', 123],
251
            [null, 0],
252
            [0, 0],
253
            [new DateInterval('PT6H8M'), 6 * 3600 + 8 * 60],
254
            [new DateInterval('P2Y4D'), 2 * 365 * 24 * 3600 + 4 * 24 * 3600],
255
        ];
256
    }
257
258
    /**
259
     * @dataProvider iterableProvider
260
     * @param array $array
261
     * @param iterable $iterable
262
     * @throws InvalidArgumentException
263
     */
264
    public function testValuesAsIterable(array $array, iterable $iterable): void
265
    {
266
        $cache = $this->createCacheInstance();
267
        $cache->clear();
268
269
        $cache->setMultiple($iterable);
270
271
        $this->assertSameExceptObject($array, $cache->getMultiple(array_keys($array)));
272
    }
273
274
    public function iterableProvider(): array
275
    {
276
        return [
277
            'array' => [
278
                ['a' => 1, 'b' => 2,],
279
                ['a' => 1, 'b' => 2,],
280
            ],
281
            'ArrayIterator' => [
282
                ['a' => 1, 'b' => 2,],
283
                new \ArrayIterator(['a' => 1, 'b' => 2,]),
284
            ],
285
            'IteratorAggregate' => [
286
                ['a' => 1, 'b' => 2,],
287
                new class() implements \IteratorAggregate
288
                {
289
                    public function getIterator()
290
                    {
291
                        return new \ArrayIterator(['a' => 1, 'b' => 2,]);
292
                    }
293
                }
294
            ],
295
            'generator' => [
296
                ['a' => 1, 'b' => 2,],
297
                (static function () {
298
                    yield 'a' => 1;
299
                    yield 'b' => 2;
300
                })()
301
            ]
302
        ];
303
    }
304
305
    public function testSetWithDateIntervalTtl(): void
306
    {
307
        $cache = $this->createCacheInstance();
308
        $cache->clear();
309
310
        $cache->set('a', 1, new DateInterval('PT1H'));
311
        $this->assertSameExceptObject(1, $cache->get('a'));
312
313
        $cache->setMultiple(['b' => 2]);
314
        $this->assertSameExceptObject(['b' => 2], $cache->getMultiple(['b']));
315
    }
316
}
317