Skip to content

Commit

Permalink
Handle stream on request + specific tests on igbinary
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier Leune committed Jan 20, 2022
1 parent 787a06e commit 726955a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/CacheEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,43 @@ public function __sleep()
{
// Stream/Resource can't be serialized... So we copy the content into an implementation of `Psr\Http\Message\StreamInterface`
if ($this->response !== null) {
$body = (string)$this->response->getBody();
$responseBody = (string)$this->response->getBody();
$this->response = $this->response->withBody(
new PumpStream(
new BodyStore($body),
new BodyStore($responseBody),
[
'size' => mb_strlen($body)
'size' => mb_strlen($responseBody),
]
)
);
}

$requestBody = (string)$this->request->getBody();
$this->request = $this->request->withBody(
new PumpStream(
new BodyStore($requestBody),
[
'size' => mb_strlen($requestBody)
]
)
);

return array_keys(get_object_vars($this));
}

public function __wakeup()
{
// We re-create the stream of the response
if ($this->response !== null) {
$this->response = $this->response
->withBody(
\GuzzleHttp\Psr7\Utils::streamFor((string) $this->response->getBody())
);
}
$this->request = $this->request
->withBody(
\GuzzleHttp\Psr7\Utils::streamFor((string) $this->request->getBody())
);
}

}
30 changes: 30 additions & 0 deletions tests/CacheEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Kevinrob\GuzzleCache\Tests;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Kevinrob\GuzzleCache\CacheEntry;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
Expand Down Expand Up @@ -81,6 +83,34 @@ public function testTtlUsesMaximumPossibleLifetime()
$this->assertEquals(70, $cacheEntry->getTTL());
}

public function testCacheEntryShouldBeSerializableWithIgBinaryWithoutWarning()
{
$request = new Request(
'GET',
'test.local',
[],
'Sample body' // Always include a body in the request to be sure there is a stream in it
);
$response = new Response(
200, [
'Cache-Control' => 'max-age=60',
],
'Test content'
);
$cacheEntry = new CacheEntry($request, $response, $this->makeDateTimeOffset(10));

if(extension_loaded('igbinary')) {
/**
* @var CacheEntry $cacheEntryPostDeserialization
*/
$cacheEntryPostDeserialization = igbinary_unserialize(igbinary_serialize($cacheEntry));
$this->assertEquals((string)$cacheEntry->getOriginalRequest()->getBody(), (string)$cacheEntryPostDeserialization->getOriginalRequest()->getBody());
$this->assertEquals((string)$cacheEntry->getOriginalResponse()->getBody(), (string)$cacheEntryPostDeserialization->getOriginalResponse()->getBody());
} else {
$this->addWarning('Extension igbinary not loaded, not asserting serialization.');
}
}

private function setResponseHeader($name, $value)
{
$this->responseHeaders[$name] = [$value];
Expand Down

0 comments on commit 726955a

Please sign in to comment.