From f75bc6349f89862dc79f306d6ef8bea4da8abcd3 Mon Sep 17 00:00:00 2001 From: Sebastian De Deyne Date: Tue, 30 Jan 2018 10:56:43 +0100 Subject: [PATCH 1/4] Add exception handling for unserializable responses --- src/Exceptions/CouldntUnserialize.php | 13 +++++++++++++ src/ResponseSerializer.php | 20 +++++++++++++++++++- tests/ResponseSerializerTest.php | 9 +++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/Exceptions/CouldntUnserialize.php diff --git a/src/Exceptions/CouldntUnserialize.php b/src/Exceptions/CouldntUnserialize.php new file mode 100644 index 0000000..7a0d0ff --- /dev/null +++ b/src/Exceptions/CouldntUnserialize.php @@ -0,0 +1,13 @@ +containsValidResponseProperties($responseProperties)) { + throw CouldntUnserialize::serializedResponse($serializedResponse); + } + $response = $this->buildResponse($responseProperties); $response->headers = $responseProperties['headers']; @@ -44,6 +49,19 @@ protected function getResponseData(Response $response): array return compact('statusCode', 'headers', 'content', 'type'); } + protected function containsValidResponseProperties($properties): bool + { + if (! is_array($properties)) { + return false; + } + + if (! isset($properties['content'], $properties['statusCode'])) { + return false; + } + + return true; + } + protected function buildResponse(array $responseProperties): Response { $type = $responseProperties['type'] ?? self::RESPONSE_TYPE_NORMAL; diff --git a/tests/ResponseSerializerTest.php b/tests/ResponseSerializerTest.php index 9906e70..f31ee85 100644 --- a/tests/ResponseSerializerTest.php +++ b/tests/ResponseSerializerTest.php @@ -4,6 +4,7 @@ use Spatie\ResponseCache\ResponseSerializer; use Symfony\Component\HttpFoundation\Response; +use Spatie\ResponseCache\Exceptions\CouldntUnserialize; class ResponseSerializerTest extends TestCase { @@ -46,4 +47,12 @@ public function it_can_serialize_and_unserialize_a_response() $this->assertEquals('testValue', $unserializedResponse->headers->get('testHeader')); } + + /** @test */ + public function it_throws_when_something_something_else_than_a_response_is_unserialized() + { + $this->expectException(CouldntUnserialize::class); + + $this->responseSerializer->unserialize('b:0;'); + } } From f03b1d62537c78492e9e46f1511019224d80ef8e Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Tue, 30 Jan 2018 09:56:52 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI --- src/ResponseSerializer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ResponseSerializer.php b/src/ResponseSerializer.php index 56abbc7..f7c5258 100644 --- a/src/ResponseSerializer.php +++ b/src/ResponseSerializer.php @@ -2,9 +2,9 @@ namespace Spatie\ResponseCache; +use Symfony\Component\HttpFoundation\Response; use Spatie\ResponseCache\Exceptions\CouldntUnserialize; use Symfony\Component\HttpFoundation\BinaryFileResponse; -use Symfony\Component\HttpFoundation\Response; class ResponseSerializer { From dbf23760ebbda37cd5224c873c0367ec3c794009 Mon Sep 17 00:00:00 2001 From: Sebastian De Deyne Date: Tue, 30 Jan 2018 11:19:48 +0100 Subject: [PATCH 3/4] Rename CouldntUnserialize to CouldNotUnserialize --- .../{CouldntUnserialize.php => CouldNotUnserialize.php} | 4 ++-- src/ResponseSerializer.php | 4 ++-- tests/ResponseSerializerTest.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) rename src/Exceptions/{CouldntUnserialize.php => CouldNotUnserialize.php} (57%) diff --git a/src/Exceptions/CouldntUnserialize.php b/src/Exceptions/CouldNotUnserialize.php similarity index 57% rename from src/Exceptions/CouldntUnserialize.php rename to src/Exceptions/CouldNotUnserialize.php index 7a0d0ff..9e4a72d 100644 --- a/src/Exceptions/CouldntUnserialize.php +++ b/src/Exceptions/CouldNotUnserialize.php @@ -4,10 +4,10 @@ use Exception; -class CouldntUnserialize extends Exception +class CouldNotUnserialize extends Exception { public static function serializedResponse(string $serializedResponse): self { - return new static("Couldn't unserialize `{$serializedResponse}`"); + return new static("Could not unserialize `{$serializedResponse}`"); } } diff --git a/src/ResponseSerializer.php b/src/ResponseSerializer.php index 56abbc7..ad02b9b 100644 --- a/src/ResponseSerializer.php +++ b/src/ResponseSerializer.php @@ -2,7 +2,7 @@ namespace Spatie\ResponseCache; -use Spatie\ResponseCache\Exceptions\CouldntUnserialize; +use Spatie\ResponseCache\Exceptions\CouldNotUnserialize; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\Response; @@ -21,7 +21,7 @@ public function unserialize(string $serializedResponse): Response $responseProperties = unserialize($serializedResponse); if (! $this->containsValidResponseProperties($responseProperties)) { - throw CouldntUnserialize::serializedResponse($serializedResponse); + throw CouldNotUnserialize::serializedResponse($serializedResponse); } $response = $this->buildResponse($responseProperties); diff --git a/tests/ResponseSerializerTest.php b/tests/ResponseSerializerTest.php index f31ee85..bf39a68 100644 --- a/tests/ResponseSerializerTest.php +++ b/tests/ResponseSerializerTest.php @@ -4,7 +4,7 @@ use Spatie\ResponseCache\ResponseSerializer; use Symfony\Component\HttpFoundation\Response; -use Spatie\ResponseCache\Exceptions\CouldntUnserialize; +use Spatie\ResponseCache\Exceptions\CouldNotUnserialize; class ResponseSerializerTest extends TestCase { @@ -51,7 +51,7 @@ public function it_can_serialize_and_unserialize_a_response() /** @test */ public function it_throws_when_something_something_else_than_a_response_is_unserialized() { - $this->expectException(CouldntUnserialize::class); + $this->expectException(CouldNotUnserialize::class); $this->responseSerializer->unserialize('b:0;'); } From c69520f3654ed456d8f3c69b5105f75d029a8432 Mon Sep 17 00:00:00 2001 From: Sebastian De Deyne Date: Tue, 30 Jan 2018 11:20:07 +0100 Subject: [PATCH 4/4] Improve test naming --- tests/ResponseSerializerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ResponseSerializerTest.php b/tests/ResponseSerializerTest.php index bf39a68..1476a3e 100644 --- a/tests/ResponseSerializerTest.php +++ b/tests/ResponseSerializerTest.php @@ -49,7 +49,7 @@ public function it_can_serialize_and_unserialize_a_response() } /** @test */ - public function it_throws_when_something_something_else_than_a_response_is_unserialized() + public function it_throws_an_exception_when_something_else_than_a_response_is_unserialized() { $this->expectException(CouldNotUnserialize::class);