diff --git a/src/CacheEntry.php b/src/CacheEntry.php index ebeeaf3..4d2a965 100644 --- a/src/CacheEntry.php +++ b/src/CacheEntry.php @@ -272,13 +272,18 @@ public function __serialize(): array public function __unserialize(array $data): void { - $this->request = self::restoreStreamBody($data['request']); - $this->response = $data['response'] !== null ? self::restoreStreamBody($data['response']) : null; - $this->staleAt = $data['staleAt']; - $this->staleIfErrorTo = $data['staleIfErrorTo']; - $this->staleWhileRevalidateTo = $data['staleWhileRevalidateTo']; - $this->dateCreated = $data['dateCreated']; - $this->timestampStale = $data['timestampStale']; + $prefix = ''; + if (isset($data["\0*\0request"])) { + // We are unserializing a cache entry which was serialized with a version < 4.1.1 + $prefix = "\0*\0"; + } + $this->request = self::restoreStreamBody($data[$prefix.'request']); + $this->response = $data[$prefix.'response'] !== null ? self::restoreStreamBody($data[$prefix.'response']) : null; + $this->staleAt = $data[$prefix.'staleAt']; + $this->staleIfErrorTo = $data[$prefix.'staleIfErrorTo']; + $this->staleWhileRevalidateTo = $data[$prefix.'staleWhileRevalidateTo']; + $this->dateCreated = $data[$prefix.'dateCreated']; + $this->timestampStale = $data[$prefix.'timestampStale']; } /** diff --git a/tests/CacheEntryTest.php b/tests/CacheEntryTest.php index 833ad78..5d3a504 100644 --- a/tests/CacheEntryTest.php +++ b/tests/CacheEntryTest.php @@ -134,6 +134,29 @@ public function testSerializationShouldNotMutateCacheEntry() $this->assertEquals($cacheEntry, $originalCacheEntry); } + /** + * @dataProvider versionsToTestProvider + */ + public function testPreviousUnserialize($version) + { + if (version_compare(PHP_VERSION, '7.4.0') < 0) { + $this->markTestSkipped('Compat with previous version is not available with \Serializable interface'); + } + + $cacheEntry = unserialize(file_get_contents(__DIR__."/data/{$version}_serialized_cache_entry")); + + self::assertInstanceOf(CacheEntry::class, $cacheEntry); + } + + public function versionsToTestProvider() + { + return [ + ['v4.0.0'], + ['v4.1.0'], + ['v4.1.1'], + ]; + } + private function setResponseHeader($name, $value) { $this->responseHeaders[$name] = [$value]; diff --git a/tests/data/v4.0.0_serialized_cache_entry b/tests/data/v4.0.0_serialized_cache_entry new file mode 100644 index 0000000..339af48 Binary files /dev/null and b/tests/data/v4.0.0_serialized_cache_entry differ diff --git a/tests/data/v4.1.0_serialized_cache_entry b/tests/data/v4.1.0_serialized_cache_entry new file mode 100644 index 0000000..72d74bf Binary files /dev/null and b/tests/data/v4.1.0_serialized_cache_entry differ diff --git a/tests/data/v4.1.1_serialized_cache_entry b/tests/data/v4.1.1_serialized_cache_entry new file mode 100644 index 0000000..4866e7b Binary files /dev/null and b/tests/data/v4.1.1_serialized_cache_entry differ