Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
60 / 60 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | pest()->group('unit'); |
| 11 | |
| 12 | use Phexium\Plugin\Cache\Adapter\NullCache; |
| 13 | |
| 14 | beforeEach(function (): void { |
| 15 | $this->cache = new NullCache(); |
| 16 | }); |
| 17 | |
| 18 | describe('Basic operations', function (): void { |
| 19 | it('does not store value on set()', function (): void { |
| 20 | $this->cache->set('key', 'value'); |
| 21 | |
| 22 | expect($this->cache->has('key'))->toBeFalse(); |
| 23 | expect($this->cache->get('key'))->toBeNull(); |
| 24 | }); |
| 25 | |
| 26 | it('does not store value on set() with TTL', function (): void { |
| 27 | $this->cache->set('key', 'value', 3600); |
| 28 | |
| 29 | expect($this->cache->has('key'))->toBeFalse(); |
| 30 | expect($this->cache->get('key'))->toBeNull(); |
| 31 | }); |
| 32 | |
| 33 | it('returns true on set()', function (): void { |
| 34 | expect($this->cache->set('key', 'value'))->toBeTrue(); |
| 35 | }); |
| 36 | |
| 37 | it('does not store value on set() with DateInterval TTL', function (): void { |
| 38 | $this->cache->set('key', 'value', new DateInterval('PT1H')); |
| 39 | |
| 40 | expect($this->cache->has('key'))->toBeFalse(); |
| 41 | }); |
| 42 | |
| 43 | it('always returns null on get() without default', function (): void { |
| 44 | expect($this->cache->get('unknown'))->toBeNull(); |
| 45 | |
| 46 | $this->cache->set('key', 'value'); |
| 47 | |
| 48 | expect($this->cache->get('key'))->toBeNull(); |
| 49 | }); |
| 50 | |
| 51 | it('returns default value on get() for non-existent key', function (): void { |
| 52 | expect($this->cache->get('unknown', 'default'))->toBe('default'); |
| 53 | }); |
| 54 | |
| 55 | it('always returns false on has()', function (): void { |
| 56 | expect($this->cache->has('unknown'))->toBeFalse(); |
| 57 | |
| 58 | $this->cache->set('key', 'value'); |
| 59 | |
| 60 | expect($this->cache->has('key'))->toBeFalse(); |
| 61 | }); |
| 62 | |
| 63 | it('always returns false on delete()', function (): void { |
| 64 | expect($this->cache->delete('unknown'))->toBeFalse(); |
| 65 | |
| 66 | $this->cache->set('key', 'value'); |
| 67 | |
| 68 | expect($this->cache->delete('key'))->toBeFalse(); |
| 69 | }); |
| 70 | |
| 71 | it('returns true on clear()', function (): void { |
| 72 | $this->cache->set('key', 'value'); |
| 73 | |
| 74 | expect($this->cache->clear())->toBeTrue(); |
| 75 | |
| 76 | expect($this->cache->has('key'))->toBeFalse(); |
| 77 | }); |
| 78 | }); |
| 79 | |
| 80 | describe('Batch operations', function (): void { |
| 81 | it('returns defaults for all keys on getMultiple()', function (): void { |
| 82 | $result = $this->cache->getMultiple(['key1', 'key2'], 'default'); |
| 83 | |
| 84 | expect($result)->toBe(['key1' => 'default', 'key2' => 'default']); |
| 85 | }); |
| 86 | |
| 87 | it('returns true on setMultiple()', function (): void { |
| 88 | $result = $this->cache->setMultiple(['key1' => 'value1', 'key2' => 'value2']); |
| 89 | |
| 90 | expect($result)->toBeTrue(); |
| 91 | }); |
| 92 | |
| 93 | it('returns true on deleteMultiple()', function (): void { |
| 94 | $result = $this->cache->deleteMultiple(['key1', 'key2']); |
| 95 | |
| 96 | expect($result)->toBeTrue(); |
| 97 | }); |
| 98 | }); |