Passed
Pull Request — master (#30)
by Alexander
02:06
created

CacheTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 295
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 137
c 0
b 0
f 0
dl 0
loc 295
rs 10
wmc 1

22 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetOrSet() 0 12 1
A testStoreSpecialValues() 0 10 1
A testAdd() 0 14 1
A hp$0 ➔ testWithObjectKeys() 0 12 1
A testGet() 0 11 1
A testWithArrayKeys() 0 10 1
A testHas() 0 11 1
A tearDown() 0 3 1
testWithObjectKeys() 0 12 ?
A testGetMultiple() 0 9 1
A testDelete() 0 8 1
A testAddMultiple() 0 18 1
A testSet() 0 9 2
A testExpireAdd() 0 10 1
A dataProviderSetMultiple() 0 3 1
A testExpire() 0 10 1
A testGetOrSetWithDependencies() 0 23 1
A testSetMultiple() 0 21 1
A testGetNonExistent() 0 6 1
A prepare() 0 8 1
A testClear() 0 7 1
A testDeleteMultiple() 0 17 1
1
<?php
2
namespace Yiisoft\CacheOld;
3
4
/**
5
 * Mock for the time() function for caching classes.
6
 * @return int
7
 */
8
function time(): int
9
{
10
    return \Yiisoft\CacheOld\Tests\CacheTest::$time ?: \time();
11
}
12
13
namespace Yiisoft\CacheOld\Tests;
14
15
use Psr\SimpleCache\InvalidArgumentException;
16
use Yiisoft\CacheOld\CacheInterface;
17
use Yiisoft\CacheOld\Dependency\TagDependency;
0 ignored issues
show
Bug introduced by
The type Yiisoft\CacheOld\Dependency\TagDependency was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
/**
20
 * Base class for testing cache backends.
21
 */
22
abstract class CacheTest extends TestCase
23
{
24
    /**
25
     * @var int virtual time to be returned by mocked time() function.
26
     * Null means normal time() behavior.
27
     */
28
    public static $time;
29
30
    abstract protected function createCacheInstance(): CacheInterface;
31
32
    protected function tearDown(): void
33
    {
34
        static::$time = null;
35
    }
36
37
    /**
38
     * This function configures given cache to match some expectations
39
     */
40
    public function prepare(CacheInterface $cache): CacheInterface
41
    {
42
        $this->assertTrue($cache->clear());
43
        $this->assertTrue($cache->set('string_test', 'string_test'));
44
        $this->assertTrue($cache->set('number_test', 42));
45
        $this->assertTrue($cache->set('array_test', ['array_test' => 'array_test']));
46
47
        return $cache;
48
    }
49
50
    public function testSet(): void
51
    {
52
        $cache = $this->createCacheInstance();
53
        $cache->clear();
54
55
        for ($i = 0; $i < 2; $i++) {
56
            $this->assertTrue($cache->set('string_test', 'string_test'));
57
            $this->assertTrue($cache->set('number_test', 42));
58
            $this->assertTrue($cache->set('array_test', ['array_test' => 'array_test']));
59
        }
60
    }
61
62
    public function testGet(): void
63
    {
64
        $cache = $this->createCacheInstance();
65
        $cache = $this->prepare($cache);
66
67
        $this->assertEquals('string_test', $cache->get('string_test'));
68
        $this->assertEquals(42, $cache->get('number_test'));
69
70
        $array = $cache->get('array_test');
71
        $this->assertArrayHasKey('array_test', $array);
72
        $this->assertEquals('array_test', $array['array_test']);
73
    }
74
75
    /**
76
     * @return array testing multiSet with and without expiry
77
     */
78
    public function dataProviderSetMultiple(): array
79
    {
80
        return [[null], [2]];
81
    }
82
83
    /**
84
     * @dataProvider dataProviderSetMultiple
85
     * @param int|null $ttl
86
     * @throws InvalidArgumentException
87
     */
88
    public function testSetMultiple(?int $ttl): void
89
    {
90
        $cache = $this->createCacheInstance();
91
        $cache->clear();
92
93
        $cache->setMultiple(
94
            [
95
                'string_test' => 'string_test',
96
                'number_test' => 42,
97
                'array_test' => ['array_test' => 'array_test'],
98
            ],
99
            $ttl
100
        );
101
102
        $this->assertEquals('string_test', $cache->get('string_test'));
103
104
        $this->assertEquals(42, $cache->get('number_test'));
105
106
        $array = $cache->get('array_test');
107
        $this->assertArrayHasKey('array_test', $array);
108
        $this->assertEquals('array_test', $array['array_test']);
109
    }
110
111
    public function testHas(): void
112
    {
113
        $cache = $this->createCacheInstance();
114
        $cache = $this->prepare($cache);
115
116
        $this->assertTrue($cache->has('string_test'));
117
        // check whether exists affects the value
118
        $this->assertEquals('string_test', $cache->get('string_test'));
119
120
        $this->assertTrue($cache->has('number_test'));
121
        $this->assertFalse($cache->has('not_exists'));
122
    }
123
124
    public function testGetNonExistent(): void
125
    {
126
        $cache = $this->createCacheInstance();
127
        $cache->clear();
128
129
        $this->assertNull($cache->get('non_existent_key'));
130
    }
131
132
    public function testStoreSpecialValues(): void
133
    {
134
        $cache = $this->createCacheInstance();
135
        $cache->clear();
136
137
        $this->assertTrue($cache->set('null_value', null));
138
        $this->assertNull($cache->get('null_value'));
139
140
        $this->assertTrue($cache->set('bool_value', true));
141
        $this->assertTrue($cache->get('bool_value'));
142
    }
143
144
    public function testGetMultiple(): void
145
    {
146
        $cache = $this->createCacheInstance();
147
        $cache = $this->prepare($cache);
148
149
        $this->assertEquals(['string_test' => 'string_test', 'number_test' => 42], $cache->getMultiple(['string_test', 'number_test']));
150
        // ensure that order does not matter
151
        $this->assertEquals(['number_test' => 42, 'string_test' => 'string_test'], $cache->getMultiple(['number_test', 'string_test']));
152
        $this->assertEquals(['number_test' => 42, 'non_existent_key' => null], $cache->getMultiple(['number_test', 'non_existent_key']));
153
    }
154
155
156
    public function testExpire(): void
157
    {
158
        $cache = $this->createCacheInstance();
159
        $cache->clear();
160
161
        $this->assertTrue($cache->set('expire_test', 'expire_test', 2));
162
        usleep(500000);
163
        $this->assertEquals('expire_test', $cache->get('expire_test'));
164
        usleep(2500000);
165
        $this->assertNull($cache->get('expire_test'));
166
    }
167
168
    public function testDelete(): void
169
    {
170
        $cache = $this->createCacheInstance();
171
        $cache = $this->prepare($cache);
172
173
        $this->assertEquals(42, $cache->get('number_test'));
174
        $this->assertTrue($cache->delete('number_test'));
175
        $this->assertNull($cache->get('number_test'));
176
    }
177
178
    public function testDeleteMultiple(): void
179
    {
180
        $cache = $this->createCacheInstance();
181
        $cache->clear();
182
183
        $cache->setMultiple([
184
            'a' => 1,
185
            'b' => 2,
186
        ]);
187
188
189
        $this->assertEquals(1, $cache->get('a'));
190
        $this->assertEquals(2, $cache->get('b'));
191
192
        $this->assertTrue($cache->deleteMultiple(['a', 'b']));
193
        $this->assertFalse($cache->has('a'));
194
        $this->assertFalse($cache->has('b'));
195
    }
196
197
    public function testClear(): void
198
    {
199
        $cache = $this->createCacheInstance();
200
        $cache = $this->prepare($cache);
201
202
        $this->assertTrue($cache->clear());
203
        $this->assertNull($cache->get('number_test'));
204
    }
205
206
    public function testExpireAdd(): void
207
    {
208
        $cache = $this->createCacheInstance();
209
        $cache->clear();
210
211
        $this->assertTrue($cache->add('expire_testa', 'expire_testa', 2));
212
        usleep(500000);
213
        $this->assertEquals('expire_testa', $cache->get('expire_testa'));
214
        usleep(2500000);
215
        $this->assertNull($cache->get('expire_testa'));
216
    }
217
218
    public function testAdd(): void
219
    {
220
        $cache = $this->createCacheInstance();
221
        $cache = $this->prepare($cache);
222
223
        // should not change existing keys
224
        $this->assertEquals(42, $cache->get('number_test'));
225
        $this->assertFalse($cache->add('number_test', 13));
226
227
228
        // should store data if it's not there yet
229
        $this->assertNull($cache->get('add_test'));
230
        $this->assertTrue($cache->add('add_test', 13));
231
        $this->assertEquals(13, $cache->get('add_test'));
232
    }
233
234
    public function testAddMultiple(): void
235
    {
236
        $cache = $this->createCacheInstance();
237
        $cache = $this->prepare($cache);
238
239
        $this->assertNull($cache->get('add_test'));
240
241
        $this->assertTrue(
242
            $cache->addMultiple(
243
                [
244
                    'number_test' => 13,
245
                    'add_test' => 13,
246
                ]
247
            )
248
        );
249
250
        $this->assertEquals(42, $cache->get('number_test'));
251
        $this->assertEquals(13, $cache->get('add_test'));
252
    }
253
254
    public function testGetOrSet(): void
255
    {
256
        $cache = $this->createCacheInstance();
257
        $cache = $this->prepare($cache);
258
259
        $expected = get_class($cache);
260
261
        $this->assertEquals(null, $cache->get('something'));
262
        $this->assertEquals($expected, $cache->getOrSet('something', static function (CacheInterface $cache): string {
263
            return get_class($cache);
264
        }));
265
        $this->assertEquals($expected, $cache->get('something'));
266
    }
267
268
    public function testGetOrSetWithDependencies(): void
269
    {
270
        $cache = $this->createCacheInstance();
271
        $cache = $this->prepare($cache);
272
273
        $dependency = new TagDependency('test');
274
275
        $expected = 'SilverFire';
276
        $loginClosure = static function (): string {
277
            return 'SilverFire';
278
        };
279
        $this->assertEquals($expected, $cache->getOrSet('some-login', $loginClosure, null, $dependency));
280
281
        // Call again with another login to make sure that value is cached
282
        $loginClosure = static function (): string {
283
            return 'SamDark';
284
        };
285
        $cache->getOrSet('some-login', $loginClosure, null, $dependency);
286
        $this->assertEquals($expected, $cache->getOrSet('some-login', $loginClosure, null, $dependency));
287
288
        TagDependency::invalidate($cache, 'test');
289
        $expected = 'SamDark';
290
        $this->assertEquals($expected, $cache->getOrSet('some-login', $loginClosure, null, $dependency));
291
    }
292
293
    public function testWithArrayKeys(): void
294
    {
295
        $key = [42];
296
        $cache = $this->createCacheInstance();
297
        $cache->clear();
298
299
        $this->assertNull($cache->get($key));
0 ignored issues
show
Bug introduced by
$key of type array<integer,integer> is incompatible with the type string expected by parameter $key of Psr\SimpleCache\CacheInterface::get(). ( Ignorable by Annotation )

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

299
        $this->assertNull($cache->get(/** @scrutinizer ignore-type */ $key));
Loading history...
300
301
        $cache->set($key, 42);
302
        $this->assertSame(42, $cache->get($key));
303
    }
304
305
    public function testWithObjectKeys(): void
306
    {
307
        $key = new class {
308
            public $value = 42;
309
        };
310
        $cache = $this->createCacheInstance();
311
        $cache->clear();
312
313
        $this->assertNull($cache->get($key));
0 ignored issues
show
Bug introduced by
$key of type anonymous//tests-old/CacheTest.php$0 is incompatible with the type string expected by parameter $key of Psr\SimpleCache\CacheInterface::get(). ( Ignorable by Annotation )

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

313
        $this->assertNull($cache->get(/** @scrutinizer ignore-type */ $key));
Loading history...
314
315
        $cache->set($key, 42);
316
        $this->assertSame(42, $cache->get($key));
317
    }
318
}
319