Passed
Pull Request — main (#48)
by Sílvio
03:09
created

FileCacheStoreFeatureTest::testAutoFlush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.9
1
<?php
2
3
use PHPUnit\Framework\TestCase;
4
use Silviooosilva\CacheerPhp\Cacheer;
5
use Silviooosilva\CacheerPhp\Helpers\EnvHelper;
6
use Silviooosilva\CacheerPhp\Utils\CacheDriver;
7
8
class FileCacheStoreFeatureTest extends TestCase
9
{
10
    private $cache;
11
    private $cacheDir;
12
13
    protected function setUp(): void
14
    {
15
        $this->cacheDir = __DIR__ . '/cache';
16
        if (!file_exists($this->cacheDir) || !is_dir($this->cacheDir)) {
17
            mkdir($this->cacheDir, 0755, true);
18
        }
19
        $this->cache = new Cacheer(['cacheDir' => $this->cacheDir]);
20
    }
21
22
    protected function tearDown(): void
23
    {
24
        $this->cache->flushCache();
25
    }
26
27
    public function testAutoFlush()
28
    {
29
        $options = [
30
            'cacheDir' => $this->cacheDir,
31
            'flushAfter' => '10 seconds'
32
        ];
33
34
        $this->cache = new Cacheer($options);
35
        $this->cache->putCache('test_key', 'test_data');
36
37
        $this->assertEquals('test_data', $this->cache->getCache('test_key'));
38
        $this->assertTrue($this->cache->isSuccess());
39
40
        sleep(11);
41
42
        $this->cache = new Cacheer($options);
43
        $this->cache->getCache('test_key');
44
        $this->assertFalse($this->cache->isSuccess());
45
    }
46
47
    public function testDataOutputShouldBeOfTypeJson()
48
    {
49
        $this->cache = new Cacheer(['cacheDir' => $this->cacheDir], true);
50
        $this->cache->putCache('key_json', 'data_json');
51
        $output = $this->cache->getCache('key_json')->toJson();
52
        $this->assertJson($output);
53
    }
54
55
    public function testDataOutputShouldBeOfTypeArray()
56
    {
57
        $this->cache = new Cacheer(['cacheDir' => $this->cacheDir], true);
58
        $this->cache->putCache('key_array', 'data_array');
59
        $output = $this->cache->getCache('key_array')->toArray();
60
        $this->assertIsArray($output);
61
    }
62
63
    public function testDataOutputShouldBeOfTypeObject()
64
    {
65
        $this->cache = new Cacheer(['cacheDir' => $this->cacheDir], true);
66
        $this->cache->putCache('key_object', ['id' => 123]);
67
        $output = $this->cache->getCache('key_object')->toObject();
68
        $this->assertIsObject($output);
69
    }
70
71
    public function testPutMany()
72
    {
73
        $cacheer = new Cacheer(['cacheDir' => __DIR__ . '/cache']);
74
        $items = [
75
            [
76
                'cacheKey' => 'user_1_profile',
77
                'cacheData' => ['name' => 'John Doe', 'email' => '[email protected]']
78
            ],
79
            [
80
                'cacheKey' => 'user_2_profile',
81
                'cacheData' => ['name' => 'Jane Doe', 'email' => '[email protected]']
82
            ],
83
        ];
84
85
        $cacheer->putMany($items);
86
87
        foreach ($items as $item) {
88
            $this->assertEquals($item['cacheData'], $cacheer->getCache($item['cacheKey']));
89
        }
90
    }
91
92
    public function test_remember_saves_and_recover_values()
93
    {
94
        $this->cache->flushCache();
95
        $value = $this->cache->remember('remember_test_key', 60, function () {
96
            return 'valor_teste';
97
        });
98
        $this->assertEquals('valor_teste', $value);
99
        $cachedValue = $this->cache->remember('remember_test_key', 60, function(){
100
            return 'novo_valor';
101
        });
102
        $this->assertEquals('valor_teste', $cachedValue);
103
    }
104
105
    public function test_remember_forever_saves_value_indefinitely()
106
    {
107
        $this->cache->flushCache();
108
        $value = $this->cache->rememberForever('remember_forever_key', function () {
109
            return 'valor_eterno';
110
        });
111
        $this->assertEquals('valor_eterno', $value);
112
        $cachedValue = $this->cache->rememberForever('remember_forever_key', function () {
113
            return 'novo_valor';
114
        });
115
        $this->assertEquals('valor_eterno', $cachedValue);
116
    }
117
118
    public function test_get_and_forget()
119
    {
120
        $this->cache->putCache('cache_key_test', 10);
121
        $data = $this->cache->getAndForget('cache_key_test');
122
        $this->assertEquals(10, $data);
123
        $this->assertNull($this->cache->getAndForget('cache_key_test'));
124
    }
125
126
    public function test_store_if_not_present_with_add_function()
127
    {
128
        $this->cache->putCache('cache_key_test', 'existent_data');
129
        $this->assertTrue($this->cache->add('cache_key_test', 100));
130
        $this->assertFalse($this->cache->add('non_existent_key', 'non_existent_data'));
131
    }
132
133
    public function test_increment_and_decrement_functions()
134
    {
135
        $this->cache->putCache('num_key', 2025);
136
        $this->cache->increment('num_key', 2);
137
        $this->assertEquals(2027, $this->cache->getCache('num_key'));
138
        $this->cache->decrement('num_key', 2);
139
        $this->assertEquals(2025, $this->cache->getCache('num_key'));
140
    }
141
142
    public function test_get_many_cache_items()
143
    {
144
        $items = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'];
145
        foreach ($items as $k => $v) { $this->cache->putCache($k, $v); }
146
        $retrieved = $this->cache->getMany(array_keys($items));
147
        $this->assertEquals($items, $retrieved);
148
    }
149
150
    public function test_get_all_cache_items()
151
    {
152
        $items = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'];
153
        foreach ($items as $k => $v) { $this->cache->putCache($k, $v); }
154
        $retrieved = $this->cache->getAll();
155
        $this->assertCount(3, $retrieved);
156
    }
157
}
158