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

DecoratorExtraBaseTest.php$0 ➔ testDefaultTtl()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Yiisoft\Cache\Tests;
5
6
7
use DateInterval;
8
use Psr\SimpleCache\CacheInterface;
9
use Yiisoft\Cache\Cache;
10
use Yiisoft\Cache\Dependency\TagDependency;
11
use Yiisoft\Cache\Serializer\PhpSerializer;
12
13
abstract class DecoratorExtraBaseTest extends TestCase
14
{
15
    abstract protected function createCacheInstance(): CacheInterface;
16
17
    public function testAdd(): void
18
    {
19
        /** @var Cache $cache */
20
        $cache = $this->createCacheInstance();
21
        $cache = $this->prepare($cache);
22
23
        // should not change existing keys
24
        $this->assertSameExceptObject('a', $cache->get('test_string'));
25
        $this->assertFalse($cache->add('test_string', 'b'));
26
27
28
        // should store data if it's not there yet
29
        $this->assertNull($cache->get('add_test'));
30
        $this->assertTrue($cache->add('add_test', 13));
31
        $this->assertSameExceptObject(13, $cache->get('add_test'));
32
    }
33
34
    public function testAddMultiple(): void
35
    {
36
        /** @var Cache $cache */
37
        $cache = $this->createCacheInstance();
38
        $cache = $this->prepare($cache);
39
40
        $this->assertNull($cache->get('add_test'));
41
42
        $this->assertTrue(
43
            $cache->addMultiple(
44
                [
45
                    'test_integer' => 13,
46
                    'add_test' => 13,
47
                ]
48
            )
49
        );
50
51
        $this->assertSameExceptObject(1, $cache->get('test_integer'));
52
        $this->assertSameExceptObject(13, $cache->get('add_test'));
53
    }
54
55
    public function testGetOrSet(): void
56
    {
57
        $cache = $this->createCacheInstance();
58
        $cache = $this->prepare($cache);
59
60
        $expected = get_class($cache);
61
62
        $this->assertSameExceptObject(null, $cache->get('something'));
63
        $this->assertSameExceptObject($expected, $cache->getOrSet('something', static function (CacheInterface $cache): string {
0 ignored issues
show
Bug introduced by
The method getOrSet() does not exist on Psr\SimpleCache\CacheInterface. It seems like you code against a sub-type of Psr\SimpleCache\CacheInterface such as Yiisoft\Cache\CacheInterface. ( Ignorable by Annotation )

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

63
        $this->assertSameExceptObject($expected, $cache->/** @scrutinizer ignore-call */ getOrSet('something', static function (CacheInterface $cache): string {
Loading history...
64
            return get_class($cache);
65
        }));
66
        $this->assertSameExceptObject($expected, $cache->get('something'));
67
    }
68
69
    public function testGetOrSetWithDependencies(): void
70
    {
71
        /** @var Cache $cache */
72
        $cache = $this->createCacheInstance();
73
74
        $dependency = new TagDependency('test');
75
76
        $expected = 'SilverFire';
77
        $loginClosure = static function (): string {
78
            return 'SilverFire';
79
        };
80
        $this->assertSameExceptObject($expected, $cache->getOrSet('some-login', $loginClosure, null, $dependency));
81
82
        // Call again with another login to make sure that value is cached
83
        $loginClosure = static function (): string {
84
            return 'SamDark';
85
        };
86
        $cache->getOrSet('some-login', $loginClosure, null, $dependency);
87
        $this->assertSameExceptObject($expected, $cache->getOrSet('some-login', $loginClosure, null, $dependency));
88
89
        TagDependency::invalidate($cache, 'test');
90
        $expected = 'SamDark';
91
        $this->assertSameExceptObject($expected, $cache->getOrSet('some-login', $loginClosure, null, $dependency));
92
    }
93
94
    public function testWithArrayKeys(): void
95
    {
96
        $key = [42];
97
        $cache = $this->createCacheInstance();
98
        $cache->clear();
99
100
        $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

100
        $this->assertNull($cache->get(/** @scrutinizer ignore-type */ $key));
Loading history...
101
102
        $cache->set($key, 42);
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::set(). ( Ignorable by Annotation )

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

102
        $cache->set(/** @scrutinizer ignore-type */ $key, 42);
Loading history...
103
        $this->assertSame(42, $cache->get($key));
104
    }
105
106
    public function testWithObjectKeys(): void
107
    {
108
        $key = new class
109
        {
110
            public $value = 42;
111
        };
112
        $cache = $this->createCacheInstance();
113
        $cache->clear();
114
115
        $this->assertNull($cache->get($key));
0 ignored issues
show
Bug introduced by
$key of type anonymous//tests/DecoratorExtraBaseTest.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

115
        $this->assertNull($cache->get(/** @scrutinizer ignore-type */ $key));
Loading history...
116
117
        $cache->set($key, 42);
0 ignored issues
show
Bug introduced by
$key of type anonymous//tests/DecoratorExtraBaseTest.php$0 is incompatible with the type string expected by parameter $key of Psr\SimpleCache\CacheInterface::set(). ( Ignorable by Annotation )

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

117
        $cache->set(/** @scrutinizer ignore-type */ $key, 42);
Loading history...
118
        $this->assertSame(42, $cache->get($key));
119
    }
120
121
    // TODO commented for speed
122
    /*public function testExpireAdd(): void
123
    {
124
        $cache = $this->createCacheInstance();
125
        $cache->clear();
126
127
        $this->assertTrue($cache->add('expire_testa', 'expire_testa', 2));
128
        usleep(500000);
129
        $this->assertSameExceptObject('expire_testa', $cache->get('expire_testa'));
130
        usleep(2500000);
131
        $this->assertNull($cache->get('expire_testa'));
132
    }*/
133
134
    public function testNormalizeKey(): void
135
    {
136
        /** @var Cache $cache */
137
        $cache = $this->createCacheInstance();
138
        $cache->clear();
139
        $cache->enableKeyNormalization();
140
141
        $cache->set('test_normalized', 1);
142
143
        $cache->disableKeyNormalization();
144
145
        $cache->set('test_not_normalized', 2);
146
147
        $cache->disableKeyNormalization();
148
        $this->assertFalse($cache->has('test_normalized'));
149
        $this->assertSameExceptObject(1, $cache->get('2753a58fdc3bf713af86cb3a97a55e57'));
150
        $this->assertSameExceptObject(2, $cache->get('test_not_normalized'));
151
    }
152
153
    public function testGetWithPrefix(): void
154
    {
155
        /** @var Cache $cache */
156
        $cache = $this->createCacheInstance();
157
        $cache->setKeyPrefix('prefix');
158
        $cache = $this->prepare($cache);
159
        $this->assertSameExceptObject(1, $cache->get('test_integer'));
160
    }
161
162
    public function testKeyPrefix(): void
163
    {
164
        /** @var Cache $cache */
165
        $cache = $this->createCacheInstance();
166
        $cache->clear();
167
        $cache->disableKeyNormalization();
168
        $cache->setKeyPrefix('prefix');
169
170
        $cache->set('test_with_prefix', 1);
171
172
        $cache->setKeyPrefix('');
173
174
        $cache->set('test_without_prefix', 2);
175
176
        $cache->setKeyPrefix('');
177
        $this->assertFalse($cache->has('test_with_prefix'));
178
        $this->assertSameExceptObject(1, $cache->get('prefixtest_with_prefix'));
179
        $this->assertSameExceptObject(2, $cache->get('test_without_prefix'));
180
    }
181
182
    public function testSerializer(): void
183
    {
184
        /** @var Cache $cache */
185
        $cache = $this->createCacheInstance();
186
        $cache->setSerializer(new PhpSerializer());
187
188
        $cache->set('test_serialized', 1);
189
190
        $cache->setSerializer(null);
191
192
        $cache->set('test_not_serialized', 2);
193
194
        $this->assertSameExceptObject('i:1;', $cache->get('test_serialized'));
195
        $this->assertSameExceptObject(2, $cache->get('test_not_serialized'));
196
197
        $cache->setSerializer(new PhpSerializer());
198
199
        $this->assertSameExceptObject(1, $cache->get('test_serialized'));
200
    }
201
202
    /**
203
     * @dataProvider featuresProvider
204
     * @param $features
205
     */
206
    public function testFeatures($features): void
207
    {
208
        /** @var Cache $cache */
209
        $cache = $this->createCacheInstance();
210
        $cache->setKeyPrefix($features[0]);
211
        $cache->setSerializer($features[1]);
212
        $features[2] ? $cache->enableKeyNormalization() : $cache->disableKeyNormalization();
213
214
        $this->featuresTest($cache);
215
    }
216
217
    public function featuresProvider()
218
    {
219
        // [prefix, serializer, normalization]
220
        return [
221
            [['', null, false]],
222
            [['testprefix', null, false]],
223
            [['', new PhpSerializer(), false]],
224
            [['', null, true]],
225
        ];
226
    }
227
228
    private function featuresTest(Cache $cache)
229
    {
230
        $this->prepare($cache);
231
232
        $dataWithPrefix = $this->getDataProviderData('for_multiple_');
233
        $cache->setMultiple($dataWithPrefix);
234
235
        $data = array_merge($this->getDataProviderData(), $dataWithPrefix, ['nonexistent-key' => null]);
236
        $dataWithDefault = $data;
237
        $dataWithDefault['nonexistent-key'] = 'default';
238
239
        foreach ($data as $key => $value) {
240
            if ($key === 'nonexistent-key') {
241
                $this->assertFalse($cache->has($key));
242
                $this->assertSameExceptObject(null, $cache->get($key));
243
                $this->assertSameExceptObject('default', $cache->get($key, 'default'));
244
            } else {
245
                $this->assertTrue($cache->has($key));
246
                $this->assertSameExceptObject($value, $cache->get($key));
247
                $this->assertSameExceptObject($value, $cache->get($key, 'default'));
248
            }
249
        }
250
251
        $this->assertSameExceptObject($data, $cache->getMultiple(array_keys($data)));
252
        $this->assertSameExceptObject($dataWithDefault, $cache->getMultiple(array_keys($data), 'default'));
253
    }
254
255
    public function testDefaultTtl(): void
256
    {
257
        $cache = $this->createCacheInstance();
258
        $cache->clear();
259
        /** @var Cache $cache */
260
        $cache->setDefaultTtl(2);
261
        $this->assertSameExceptObject(2, $cache->getDefaultTtl());
262
    }
263
264
    public function testDateIntervalTtl(): void
265
    {
266
        $interval = new DateInterval('PT3S');
267
        $cache = $this->createCacheInstance();
268
        $cache->clear();
269
        /** @var Cache $cache */
270
        $cache->setDefaultTtl($interval);
271
        $this->assertSameExceptObject(3, $cache->getDefaultTtl());
272
    }
273
}
274