|
12 | 12 | namespace Cache\Adapter\Apcu\Tests; |
13 | 13 |
|
14 | 14 | use Cache\Adapter\Apcu\ApcuCachePool; |
| 15 | +use Cache\Adapter\Common\Exception\CachePoolException; |
15 | 16 | use PHPUnit\Framework\Attributes\DataProvider; |
16 | 17 | use PHPUnit\Framework\Attributes\PreserveGlobalState; |
17 | 18 | use PHPUnit\Framework\Attributes\RunInSeparateProcess; |
18 | 19 | use PHPUnit\Framework\TestCase; |
19 | 20 |
|
20 | 21 | final class ApcuFunctionStub |
21 | 22 | { |
| 23 | + public static ?\Throwable $exception = null; |
| 24 | + |
| 25 | + public static ?string $missingClass = null; |
| 26 | + |
| 27 | + public static ?string $serializedValue = null; |
| 28 | + |
22 | 29 | public static mixed $storedValue = null; |
23 | 30 |
|
24 | 31 | public static bool $success = true; |
25 | 32 |
|
| 33 | + public static bool $throwUnserializationException = false; |
| 34 | + |
26 | 35 | public static ?string $storedKey = null; |
27 | 36 |
|
28 | 37 | public static ?int $storedTtl = null; |
29 | 38 | } |
30 | 39 |
|
| 40 | +final class ApcuUnserializationFailure |
| 41 | +{ |
| 42 | + public function __unserialize(array $data) |
| 43 | + { |
| 44 | + throw new \Error('cached payload could not be decoded'); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +final class ApcuUnserializationException |
| 49 | +{ |
| 50 | + public function __unserialize(array $data) |
| 51 | + { |
| 52 | + throw new \RuntimeException('cached payload could not be decoded'); |
| 53 | + } |
| 54 | +} |
| 55 | + |
31 | 56 | final class ApcuCachePoolTest extends TestCase |
32 | 57 | { |
33 | 58 | #[RunInSeparateProcess] |
@@ -80,4 +105,59 @@ public static function invalidPayloads(): iterable |
80 | 105 | yield 'invalid tags' => [['value', [42], null]]; |
81 | 106 | yield 'invalid expiration' => [['value', [], 'tomorrow']]; |
82 | 107 | } |
| 108 | + |
| 109 | + #[RunInSeparateProcess] |
| 110 | + #[PreserveGlobalState(false)] |
| 111 | + public function testIncompleteClassPayloadIsACacheMiss() |
| 112 | + { |
| 113 | + require_once __DIR__.'/Fixtures/apcu_functions.php'; |
| 114 | + |
| 115 | + ApcuFunctionStub::$missingClass = 'GoneType'; |
| 116 | + ApcuFunctionStub::$storedValue = ['value', [], null]; |
| 117 | + ApcuFunctionStub::$success = true; |
| 118 | + |
| 119 | + self::assertFalse((new ApcuCachePool())->getItem('incomplete')->isHit()); |
| 120 | + } |
| 121 | + |
| 122 | + #[RunInSeparateProcess] |
| 123 | + #[PreserveGlobalState(false)] |
| 124 | + public function testUnserializationErrorIsACacheMiss() |
| 125 | + { |
| 126 | + require_once __DIR__.'/Fixtures/apcu_functions.php'; |
| 127 | + |
| 128 | + ApcuFunctionStub::$serializedValue = serialize([new ApcuUnserializationFailure(), [], null]); |
| 129 | + ApcuFunctionStub::$success = true; |
| 130 | + |
| 131 | + self::assertFalse((new ApcuCachePool())->getItem('broken')->isHit()); |
| 132 | + } |
| 133 | + |
| 134 | + #[RunInSeparateProcess] |
| 135 | + #[PreserveGlobalState(false)] |
| 136 | + public function testUnserializationExceptionIsACacheMiss() |
| 137 | + { |
| 138 | + require_once __DIR__.'/Fixtures/apcu_functions.php'; |
| 139 | + |
| 140 | + ApcuFunctionStub::$storedValue = ['value', [], null]; |
| 141 | + ApcuFunctionStub::$success = true; |
| 142 | + ApcuFunctionStub::$throwUnserializationException = true; |
| 143 | + |
| 144 | + self::assertFalse((new ApcuCachePool())->getItem('broken')->isHit()); |
| 145 | + } |
| 146 | + |
| 147 | + #[RunInSeparateProcess] |
| 148 | + #[PreserveGlobalState(false)] |
| 149 | + public function testBackendFetchExceptionIsNotTreatedAsCacheMiss() |
| 150 | + { |
| 151 | + require_once __DIR__.'/Fixtures/apcu_functions.php'; |
| 152 | + |
| 153 | + $backendException = new \RuntimeException('backend failed'); |
| 154 | + ApcuFunctionStub::$exception = $backendException; |
| 155 | + |
| 156 | + try { |
| 157 | + (new ApcuCachePool())->getItem('key')->isHit(); |
| 158 | + self::fail('The backend exception was not propagated.'); |
| 159 | + } catch (CachePoolException $exception) { |
| 160 | + self::assertSame($backendException, $exception->getPrevious()); |
| 161 | + } |
| 162 | + } |
83 | 163 | } |
0 commit comments