Skip to content

Commit 726955a

Browse files
author
Xavier Leune
committed
Handle stream on request + specific tests on igbinary
1 parent 787a06e commit 726955a

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/CacheEntry.php

+29-3
Original file line numberDiff line numberDiff line change
@@ -260,17 +260,43 @@ public function __sleep()
260260
{
261261
// Stream/Resource can't be serialized... So we copy the content into an implementation of `Psr\Http\Message\StreamInterface`
262262
if ($this->response !== null) {
263-
$body = (string)$this->response->getBody();
263+
$responseBody = (string)$this->response->getBody();
264264
$this->response = $this->response->withBody(
265265
new PumpStream(
266-
new BodyStore($body),
266+
new BodyStore($responseBody),
267267
[
268-
'size' => mb_strlen($body)
268+
'size' => mb_strlen($responseBody),
269269
]
270270
)
271271
);
272272
}
273273

274+
$requestBody = (string)$this->request->getBody();
275+
$this->request = $this->request->withBody(
276+
new PumpStream(
277+
new BodyStore($requestBody),
278+
[
279+
'size' => mb_strlen($requestBody)
280+
]
281+
)
282+
);
283+
274284
return array_keys(get_object_vars($this));
275285
}
286+
287+
public function __wakeup()
288+
{
289+
// We re-create the stream of the response
290+
if ($this->response !== null) {
291+
$this->response = $this->response
292+
->withBody(
293+
\GuzzleHttp\Psr7\Utils::streamFor((string) $this->response->getBody())
294+
);
295+
}
296+
$this->request = $this->request
297+
->withBody(
298+
\GuzzleHttp\Psr7\Utils::streamFor((string) $this->request->getBody())
299+
);
300+
}
301+
276302
}

tests/CacheEntryTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Kevinrob\GuzzleCache\Tests;
44

5+
use GuzzleHttp\Psr7\Request;
6+
use GuzzleHttp\Psr7\Response;
57
use Kevinrob\GuzzleCache\CacheEntry;
68
use PHPUnit\Framework\TestCase;
79
use Psr\Http\Message\RequestInterface;
@@ -81,6 +83,34 @@ public function testTtlUsesMaximumPossibleLifetime()
8183
$this->assertEquals(70, $cacheEntry->getTTL());
8284
}
8385

86+
public function testCacheEntryShouldBeSerializableWithIgBinaryWithoutWarning()
87+
{
88+
$request = new Request(
89+
'GET',
90+
'test.local',
91+
[],
92+
'Sample body' // Always include a body in the request to be sure there is a stream in it
93+
);
94+
$response = new Response(
95+
200, [
96+
'Cache-Control' => 'max-age=60',
97+
],
98+
'Test content'
99+
);
100+
$cacheEntry = new CacheEntry($request, $response, $this->makeDateTimeOffset(10));
101+
102+
if(extension_loaded('igbinary')) {
103+
/**
104+
* @var CacheEntry $cacheEntryPostDeserialization
105+
*/
106+
$cacheEntryPostDeserialization = igbinary_unserialize(igbinary_serialize($cacheEntry));
107+
$this->assertEquals((string)$cacheEntry->getOriginalRequest()->getBody(), (string)$cacheEntryPostDeserialization->getOriginalRequest()->getBody());
108+
$this->assertEquals((string)$cacheEntry->getOriginalResponse()->getBody(), (string)$cacheEntryPostDeserialization->getOriginalResponse()->getBody());
109+
} else {
110+
$this->addWarning('Extension igbinary not loaded, not asserting serialization.');
111+
}
112+
}
113+
84114
private function setResponseHeader($name, $value)
85115
{
86116
$this->responseHeaders[$name] = [$value];

0 commit comments

Comments
 (0)