|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use JakubBoucek\ComposerConsistency\Cache\FileCache; |
| 6 | +use Tester\Assert; |
| 7 | +use Tester\Environment; |
| 8 | +use Tester\Helpers; |
| 9 | +use Tester\TestCase; |
| 10 | + |
| 11 | +require __DIR__ . '/../vendor/autoload.php'; |
| 12 | + |
| 13 | +Environment::setup(); |
| 14 | + |
| 15 | +/** @testCase */ |
| 16 | +class FileCacheTest extends TestCase |
| 17 | +{ |
| 18 | + private const TEMP_DIR = __DIR__ . '/temp/cache'; |
| 19 | + private const LOCK_DIR = __DIR__ . '/lock'; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + @mkdir(self::LOCK_DIR, 0777, true); |
| 24 | + @mkdir(self::TEMP_DIR, 0777, true); |
| 25 | + Environment::lock('cache', self::LOCK_DIR); |
| 26 | + Helpers::purge(self::TEMP_DIR); |
| 27 | + } |
| 28 | + |
| 29 | + protected function getDataKeysValues(): array |
| 30 | + { |
| 31 | + return [ |
| 32 | + ['foo', 'bar', 'kopo'], |
| 33 | + [123, 456, 789], |
| 34 | + [new stdClass(), [true], null], |
| 35 | + ]; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @var mixed $value |
| 40 | + * @var mixed $key |
| 41 | + * @dataProvider getDataKeysValues |
| 42 | + */ |
| 43 | + public function testCacheMatchContent($key, $value): void |
| 44 | + { |
| 45 | + $cache = new FileCache(self::TEMP_DIR); |
| 46 | + |
| 47 | + $cache->write($key, $value); |
| 48 | + Assert::true($cache->isValid($key, $value)); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @var mixed $key |
| 53 | + * @var mixed $value |
| 54 | + * @var mixed $invalidValue |
| 55 | + * @dataProvider getDataKeysValues |
| 56 | + */ |
| 57 | + public function testCacheUnmatchContent($key, $value, $invalidValue): void |
| 58 | + { |
| 59 | + $cache = new FileCache(self::TEMP_DIR); |
| 60 | + |
| 61 | + $cache->write($key, $value); |
| 62 | + Assert::false($cache->isValid($key, $invalidValue)); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @var mixed $key |
| 67 | + * @var mixed $value |
| 68 | + * @dataProvider getDataKeysValues |
| 69 | + */ |
| 70 | + public function testCacheCheckExists($key, $value): void |
| 71 | + { |
| 72 | + $cache = new FileCache(self::TEMP_DIR); |
| 73 | + |
| 74 | + $cache->write($key, $value); |
| 75 | + Assert::true($cache->isExists($key)); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @var mixed $key |
| 80 | + * @var mixed $value |
| 81 | + * @var mixed $invalidKey |
| 82 | + * @dataProvider getDataKeysValues |
| 83 | + */ |
| 84 | + public function testCacheCheckNotExists($key, $value, $invalidKey): void |
| 85 | + { |
| 86 | + $cache = new FileCache(self::TEMP_DIR); |
| 87 | + |
| 88 | + $cache->write($key, $value); |
| 89 | + Assert::false($cache->isExists($invalidKey)); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @var mixed $key |
| 94 | + * @var mixed $value |
| 95 | + * @var mixed $invalidKey |
| 96 | + * @dataProvider getDataKeysValues |
| 97 | + */ |
| 98 | + public function testCacheCheckInvalidate($key, $value): void |
| 99 | + { |
| 100 | + $cache = new FileCache(self::TEMP_DIR); |
| 101 | + |
| 102 | + $cache->write($key, $value); |
| 103 | + $cache->invalidate($key); |
| 104 | + Assert::false($cache->isExists($key)); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @var mixed $key |
| 109 | + * @var mixed $value |
| 110 | + * @var mixed $invalidKey |
| 111 | + * @dataProvider getDataKeysValues |
| 112 | + */ |
| 113 | + public function testCacheCheckInvalidateOther($key, $value, $invalidKey): void |
| 114 | + { |
| 115 | + $cache = new FileCache(self::TEMP_DIR); |
| 116 | + |
| 117 | + $cache->write($key, $value); |
| 118 | + $cache->invalidate($invalidKey); |
| 119 | + Assert::true($cache->isExists($key)); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +(new FileCacheTest())->run(); |
0 commit comments