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