Passed
Pull Request — master (#30)
by Alexander
01:23
created

BaseTest::testDefaultTtl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Yiisoft\Cache;
4
5
/**
6
 * Mock for the time() function for caching classes.
7
 * @return int
8
 */
9
function time(): int
10
{
11
    return \Yiisoft\Cache\Tests\BaseTest::$time ?: \time();
12
}
13
14
namespace Yiisoft\Cache\Tests;
15
16
17
use DateInterval;
18
use Psr\SimpleCache\CacheInterface;
19
use Psr\SimpleCache\InvalidArgumentException;
20
use ReflectionException;
21
use Yiisoft\Cache\Cache;
22
23
abstract class BaseTest extends TestCase
24
{
25
    /**
26
     * @var int virtual time to be returned by mocked time() function.
27
     * Null means normal time() behavior.
28
     */
29
    public static $time;
30
31
    protected function tearDown(): void
32
    {
33
        static::$time = null;
34
    }
35
36
    abstract protected function createCacheInstance(): CacheInterface;
37
38
    /**
39
     * @dataProvider dataProvider
40
     * @param $key
41
     * @param $value
42
     * @throws InvalidArgumentException
43
     */
44
    public function testSet($key, $value): void
45
    {
46
        $cache = $this->createCacheInstance();
47
        $cache->clear();
48
49
        for ($i = 0; $i < 2; $i++) {
50
            $this->assertTrue($cache->set($key, $value));
51
        }
52
    }
53
54
    /**
55
     * @dataProvider dataProvider
56
     * @param $key
57
     * @param $value
58
     * @throws InvalidArgumentException
59
     */
60
    public function testGet($key, $value): void
61
    {
62
        $cache = $this->createCacheInstance();
63
        $cache->clear();
64
65
        $cache->set($key, $value);
66
        $valueFromCache = $cache->get($key, 'default');
67
68
        $this->assertSameExceptObject($value, $valueFromCache);
69
    }
70
71
    /**
72
     * @dataProvider dataProvider
73
     * @param $key
74
     * @param $value
75
     * @throws InvalidArgumentException
76
     */
77
    public function testValueInCacheCannotBeChanged($key, $value): void
78
    {
79
        $cache = $this->createCacheInstance();
80
        $cache->clear();
81
82
        $cache->set($key, $value);
83
        $valueFromCache = $cache->get($key, 'default');
84
85
        $this->assertSameExceptObject($value, $valueFromCache);
86
87
        if (is_object($value)) {
88
            $originalValue = clone $value;
89
            $valueFromCache->test_field = 'changed';
90
            $value->test_field = 'changed';
91
            $valueFromCacheNew = $cache->get($key, 'default');
92
            $this->assertSameExceptObject($originalValue, $valueFromCacheNew);
93
        }
94
    }
95
96
    /**
97
     * @dataProvider dataProvider
98
     * @param $key
99
     * @param $value
100
     * @throws InvalidArgumentException
101
     */
102
    public function testHas($key, $value): void
103
    {
104
        $cache = $this->createCacheInstance();
105
        $cache->clear();
106
107
        $cache->set($key, $value);
108
109
        $this->assertTrue($cache->has($key));
110
        // check whether exists affects the value
111
        $this->assertSameExceptObject($value, $cache->get($key));
112
113
        $this->assertTrue($cache->has($key));
114
        $this->assertFalse($cache->has('not_exists'));
115
    }
116
117
    public function testGetNonExistent(): void
118
    {
119
        $cache = $this->createCacheInstance();
120
        $cache->clear();
121
122
        $this->assertNull($cache->get('non_existent_key'));
123
    }
124
125
    // TODO
126
    /*public function testExpire(): void
127
    {
128
        $cache = $this->createCacheInstance();
129
        $cache->clear();
130
131
        $this->assertTrue($cache->set('expire_test', 'expire_test', 2));
132
        usleep(500000);
133
        $this->assertSameExceptObject('expire_test', $cache->get('expire_test'));
134
        usleep(2500000);
135
        $this->assertNull($cache->get('expire_test'));
136
    }*/
137
138
    /**
139
     * @dataProvider dataProvider
140
     * @param $key
141
     * @param $value
142
     * @throws InvalidArgumentException
143
     */
144
    public function testDelete($key, $value): void
145
    {
146
        $cache = $this->createCacheInstance();
147
        $cache->clear();
148
149
        $cache->set($key, $value);
150
151
        $this->assertSameExceptObject($value, $cache->get($key));
152
        $this->assertTrue($cache->delete($key));
153
        $this->assertNull($cache->get($key));
154
    }
155
156
    /**
157
     * @dataProvider dataProvider
158
     * @param $key
159
     * @param $value
160
     * @throws InvalidArgumentException
161
     */
162
    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

162
    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...
163
    {
164
        $cache = $this->createCacheInstance();
165
        $cache = $this->prepare($cache);
166
167
        $this->assertTrue($cache->clear());
168
        $this->assertNull($cache->get($key));
169
    }
170
171
    /**
172
     * @dataProvider dataProviderSetMultiple
173
     * @param int|null $ttl
174
     * @throws InvalidArgumentException
175
     */
176
    public function testSetMultiple(?int $ttl): void
177
    {
178
        $cache = $this->createCacheInstance();
179
        $cache->clear();
180
181
        $data = $this->getDataProviderData();
182
183
        $cache->setMultiple($data, $ttl);
184
185
        foreach ($data as $key => $value) {
186
            $this->assertSameExceptObject($value, $cache->get($key));
187
        }
188
    }
189
190
    /**
191
     * @return array testing multiSet with and without expiry
192
     */
193
    public function dataProviderSetMultiple(): array
194
    {
195
        return [
196
            [null],
197
            [2],
198
        ];
199
    }
200
201
    public function testGetMultiple(): void
202
    {
203
        /** @var Cache $cache */
204
        $cache = $this->createCacheInstance();
205
        $cache->clear();
206
207
        $data = $this->getDataProviderData();
208
209
        $cache->setMultiple($data);
210
211
        $this->assertSameExceptObject($data, $cache->getMultiple(array_keys($data)));
212
    }
213
214
    public function testDeleteMultiple(): void
215
    {
216
        /** @var Cache $cache */
217
        $cache = $this->createCacheInstance();
218
        $cache->clear();
219
220
        $data = $this->getDataProviderData();
221
222
        $cache->setMultiple($data);
223
224
        $this->assertSameExceptObject($data, $cache->getMultiple(array_keys($data)));
225
226
        $cache->deleteMultiple(array_keys($data));
227
228
        $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

228
        $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...
229
            return null;
230
        }, $data);
231
232
        $this->assertSameExceptObject($emptyData, $cache->getMultiple(array_keys($data)));
233
    }
234
235
    public function testZeroAndNegativeTtl()
236
    {
237
        $cache = $this->createCacheInstance();
238
        $cache->clear();
239
        $cache->setMultiple([
240
            'a' => 1,
241
            'b' => 2,
242
        ]);
243
244
        $this->assertTrue($cache->has('a'));
245
        $this->assertTrue($cache->has('b'));
246
247
        $cache->set('a', 11, -1);
248
249
        $this->assertFalse($cache->has('a'));
250
251
        $cache->set('b', 22, 0);
252
253
        $this->assertFalse($cache->has('b'));
254
    }
255
256
    /**
257
     * @dataProvider dataProviderNormalizeTtl
258
     * @covers       \Yiisoft\Cache\ArrayCache::normalizeTtl()
259
     * @covers       \Yiisoft\Cache\Memcached::normalizeTtl()
260
     * @covers       \Yiisoft\Cache\Cache::normalizeTtl()
261
     * @param mixed $ttl
262
     * @param mixed $expectedResult
263
     * @throws ReflectionException
264
     */
265
    public function testNormalizeTtl($ttl, $expectedResult): void
266
    {
267
        $cache = $this->createCacheInstance();
268
        $this->assertSameExceptObject($expectedResult, $this->invokeMethod($cache, 'normalizeTtl', [$ttl]));
269
    }
270
271
    /**
272
     * Data provider for {@see testNormalizeTtl()}
273
     * @return array test data
274
     *
275
     * @throws \Exception
276
     */
277
    public function dataProviderNormalizeTtl(): array
278
    {
279
        return [
280
            [123, 123],
281
            ['123', 123],
282
            [null, null],
283
            [0, 0],
284
            [new DateInterval('PT6H8M'), 6 * 3600 + 8 * 60],
285
            [new DateInterval('P2Y4D'), 2 * 365 * 24 * 3600 + 4 * 24 * 3600],
286
        ];
287
    }
288
289
290
    /**
291
     * @dataProvider iterableProvider
292
     * @param array $array
293
     * @param iterable $iterable
294
     * @throws InvalidArgumentException
295
     */
296
    public function testValuesAsIterable(array $array, iterable $iterable): void
297
    {
298
        $cache = $this->createCacheInstance();
299
        $cache->clear();
300
301
        $cache->setMultiple($iterable);
302
303
        $this->assertSameExceptObject($array, $cache->getMultiple(array_keys($array)));
304
    }
305
306
    public function iterableProvider(): array
307
    {
308
        return [
309
            'array' => [
310
                ['a' => 1, 'b' => 2,],
311
                ['a' => 1, 'b' => 2,],
312
            ],
313
            'ArrayIterator' => [
314
                ['a' => 1, 'b' => 2,],
315
                new \ArrayIterator(['a' => 1, 'b' => 2,]),
316
            ],
317
            'IteratorAggregate' => [
318
                ['a' => 1, 'b' => 2,],
319
                new class() implements \IteratorAggregate
320
                {
321
                    public function getIterator()
322
                    {
323
                        return new \ArrayIterator(['a' => 1, 'b' => 2,]);
324
                    }
325
                }
326
            ],
327
            'generator' => [
328
                ['a' => 1, 'b' => 2,],
329
                (static function () {
330
                    yield 'a' => 1;
331
                    yield 'b' => 2;
332
                })()
333
            ]
334
        ];
335
    }
336
}
337