Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restore cache entry unserialization compatibility #181

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/CacheEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/CacheEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Binary file added tests/data/v4.0.0_serialized_cache_entry
Binary file not shown.
Binary file added tests/data/v4.1.0_serialized_cache_entry
Binary file not shown.
Binary file added tests/data/v4.1.1_serialized_cache_entry
Binary file not shown.
Loading