Issues (13)

tests/integration/FileCacheCest.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace Kodus\Cache\Test\Integration;
4
5
use Codeception\Util\FileSystem;
6
use DateInterval;
7
use IntegrationTester;
8
use Kodus\Cache\InvalidArgumentException;
9
use Kodus\Cache\Test\TestableFileCache;
10
use TypeError;
11
12
class FileCacheCest
13
{
14
    /**
15
     * @var int
16
     */
17
    const DEFAULT_EXPIRATION = 86400;
18
19
    /**
20
     * @var TestableFileCache
21
     */
22
    protected $cache;
23
24
    const DIR_MODE = 0775;
25
    const FILE_MODE = 0664;
26
27
    public function _before()
28
    {
29
        $path = dirname(__DIR__) . "/_output/cache";
30
31
        FileSystem::deleteDir($path);
32
33
        $this->cache = new TestableFileCache($path, self::DEFAULT_EXPIRATION, self::DIR_MODE, self::FILE_MODE);
34
35
        assert(file_exists($path));
36
37
        assert(is_writable($path));
38
    }
39
40
    public function _after()
41
    {
42
        $this->cache->clear();
43
    }
44
45
    public function applyDirAndFilePermissions(IntegrationTester $I)
46
    {
47
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
48
            $I->comment("running on Windows - skipping test for *nix file permissions");
49
50
            return;
51
        }
52
53
        $this->cache->set("key1", "value1");
54
55
        $path = $this->cache->getCachePath("key1");
56
57
        $dir_mode = fileperms(dirname($path)) & 0777;
58
59
        $I->assertSame(self::DIR_MODE, $dir_mode, sprintf("dir mode applied: %o", $dir_mode));
60
61
        $file_mode = fileperms($path) & 0777;
62
63
        $I->assertSame(self::FILE_MODE, $file_mode, sprintf("file mode applied: %o", $file_mode));
64
    }
65
66
    public function setGetAndDelete(IntegrationTester $I)
67
    {
68
        $I->assertTrue($this->cache->set("key1", "value1"));
69
        $I->assertTrue($this->cache->set("key2", "value2"));
70
71
        $I->assertSame("value1", $this->cache->get("key1"));
72
        $I->assertSame("value2", $this->cache->get("key2"));
73
74
        $I->assertTrue($this->cache->delete("key1"), "deleting existing value");
75
        $I->assertTrue($this->cache->delete("key1"), "deleting non-existent value");
76
77
        $I->assertSame(null, $this->cache->get("key1"));
78
        $I->assertSame("value2", $this->cache->get("key2"));
79
80
        $I->expectThrowable(InvalidArgumentException::class, function () {
81
            $this->cache->set("key@", "value1");
82
        });
83
84
        $I->expectThrowable(InvalidArgumentException::class, function () {
85
            $this->cache->get("key@");
86
        });
87
88
        $I->expectThrowable(InvalidArgumentException::class, function () {
89
            $this->cache->delete("key@");
90
        });
91
    }
92
93
    public function getNonExisting(IntegrationTester $I)
94
    {
95
        $I->assertSame(null, $this->cache->get("key"));
96
        $I->assertSame("default", $this->cache->get("key", "default"));
97
    }
98
99
    public function expirationInSeconds(IntegrationTester $I)
100
    {
101
        $this->cache->set("key", "value", 10);
102
103
        $this->cache->skipTime(5);
104
105
        $I->assertSame("value", $this->cache->get("key"));
106
107
        $this->cache->skipTime(5);
108
109
        $I->assertSame(null, $this->cache->get("key"));
110
        $I->assertSame("default", $this->cache->get("key", "default"));
111
    }
112
113
    public function expirationByInterval(IntegrationTester $I)
114
    {
115
        $interval = new DateInterval("PT10S");
116
117
        $this->cache->set("key", "value", $interval);
118
119
        $this->cache->skipTime(5);
120
121
        $I->assertSame("value", $this->cache->get("key"));
122
123
        $this->cache->skipTime(5);
124
125
        $I->assertSame(null, $this->cache->get("key"));
126
        $I->assertSame("default", $this->cache->get("key", "default"));
127
    }
128
129
    public function expirationByDefault(IntegrationTester $I)
130
    {
131
        $this->cache->set("key", "value");
132
133
        $this->cache->skipTime(self::DEFAULT_EXPIRATION - 5);
134
135
        $I->assertSame("value", $this->cache->get("key"));
136
137
        $this->cache->skipTime(10);
138
139
        $I->assertSame(null, $this->cache->get("key"));
140
        $I->assertSame("default", $this->cache->get("key", "default"));
141
    }
142
143
    public function expirationInThePast(IntegrationTester $I)
144
    {
145
        $this->cache->set("key1", "value1", 0);
146
        $this->cache->set("key2", "value2", -10);
147
148
        $I->assertSame("default", $this->cache->get("key1", "default"));
149
        $I->assertSame("default", $this->cache->get("key2", "default"));
150
    }
151
152
    public function clear(IntegrationTester $I)
153
    {
154
        // add some values that should be gone when we clear cache:
155
156
        $this->cache->set("key1", "value1");
157
        $this->cache->set("key2", "value2");
158
159
        $I->assertTrue($this->cache->clear());
160
161
        // check to confirm everything"s been wiped out:
162
163
        $I->assertSame(null, $this->cache->get("key1"));
164
        $I->assertSame("default", $this->cache->get("key1", "default"));
165
166
        $I->assertSame(null, $this->cache->get("key2"));
167
        $I->assertSame("default", $this->cache->get("key2", "default"));
168
    }
169
170
    public function cleanExpired(IntegrationTester $I)
171
    {
172
        $this->cache->set("key1", "value1", 10);
173
        $this->cache->set("key2", "value2", 30);
174
175
        $this->cache->skipTime(20);
176
177
        $this->cache->cleanExpired();
178
179
        $I->assertFileNotExists($this->cache->getCachePath("key1"), "file has expired");
180
        $I->assertFileExists($this->cache->getCachePath("key2"), "file has not expired");
181
    }
182
183
    public function testGetAndSetMultiple(IntegrationTester $I)
184
    {
185
        $this->cache->setMultiple(["key1" => "value1", "key2" => "value2"]);
186
187
        $results = $this->cache->getMultiple(["key1", "key2", "key3"], false);
188
189
        $I->assertSame(["key1" => "value1", "key2" => "value2", "key3" => false], $results);
190
191
        $I->expectThrowable(TypeError::class, function () {
192
            $this->cache->getMultiple("Invalid type");
0 ignored issues
show
'Invalid type' of type string is incompatible with the type iterable expected by parameter $keys of Kodus\Cache\FileCache::getMultiple(). ( Ignorable by Annotation )

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

192
            $this->cache->getMultiple(/** @scrutinizer ignore-type */ "Invalid type");
Loading history...
193
        });
194
195
        $I->expectThrowable(TypeError::class, function () {
196
            $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\FileCache::setMultiple(). ( Ignorable by Annotation )

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

196
            $this->cache->setMultiple(/** @scrutinizer ignore-type */ "Invalid type");
Loading history...
197
        });
198
199
        $I->expectThrowable(InvalidArgumentException::class, function () {
200
            $this->cache->setMultiple(["Invalid key@" => "value1"]);
201
        });
202
203
        $I->expectThrowable(InvalidArgumentException::class, function () {
204
            $this->cache->getMultiple(["Invalid key@"]);
205
        });
206
    }
207
208
    public function testDeleteMultiple(IntegrationTester $I)
209
    {
210
        $this->cache->setMultiple(["key1" => "value1", "key2" => "value2", "key3" => "value3"]);
211
212
        $this->cache->deleteMultiple(["key1", "key2"]);
213
214
        $I->assertSame(["key1" => null, "key2" => null], $this->cache->getMultiple(["key1", "key2"]));
215
216
        $I->assertSame("value3", $this->cache->get("key3"));
217
218
        $I->expectThrowable(TypeError::class, function () {
219
            $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\FileCache::deleteMultiple(). ( Ignorable by Annotation )

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

219
            $this->cache->deleteMultiple(/** @scrutinizer ignore-type */ "Invalid type");
Loading history...
220
        });
221
222
        $I->expectThrowable(InvalidArgumentException::class, function () {
223
            $this->cache->deleteMultiple(["Invalid key@"]);
224
        });
225
    }
226
227
    public function testHas(IntegrationTester $I)
228
    {
229
        $this->cache->set("key", "value");
230
231
        $I->assertSame(true, $this->cache->has("key"));
232
        $I->assertSame(false, $this->cache->has("fudge"));
233
    }
234
235
    public function testIncrement(IntegrationTester $I)
236
    {
237
        // test setting initial value:
238
239
        $I->assertSame(5, $this->cache->increment("key", 5));
240
241
        // test incrementing value:
242
243
        $I->assertSame(10, $this->cache->increment("key", 5));
244
        $I->assertSame(11, $this->cache->increment("key"));
245
    }
246
247
    public function testDecrement(IntegrationTester $I)
248
    {
249
        // test setting initial value:
250
251
        $I->assertSame(10, $this->cache->increment("key", 10));
252
253
        // test decrementing value:
254
255
        $I->assertSame(5, $this->cache->decrement("key", 5));
256
        $I->assertSame(4, $this->cache->decrement("key"));
257
    }
258
}
259