Skip to content

Commit

Permalink
fix: restore cache entry unserialization compatibility (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
e-zannelli authored Jun 14, 2023
1 parent 0e12dcc commit 2546d10
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
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.

0 comments on commit 2546d10

Please sign in to comment.