From d26324603870c4ee07eeadd050d6800b75b93a06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20S=C3=A6ther?= Date: Thu, 9 Sep 2021 08:57:49 +0200 Subject: [PATCH 1/3] Updated error trait to match new error structure in API response --- src/Servebolt/Traits/HasErrors.php | 2 +- tests/Unit/ResponseObjectTest.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Servebolt/Traits/HasErrors.php b/src/Servebolt/Traits/HasErrors.php index 734ae2b..5ba9120 100644 --- a/src/Servebolt/Traits/HasErrors.php +++ b/src/Servebolt/Traits/HasErrors.php @@ -38,7 +38,7 @@ public function getFirstError() public function getFirstErrorMessage() { if ($error = $this->getFirstError()) { - return $error->message; + return $error->title; } } diff --git a/tests/Unit/ResponseObjectTest.php b/tests/Unit/ResponseObjectTest.php index a71b56f..7279ffa 100644 --- a/tests/Unit/ResponseObjectTest.php +++ b/tests/Unit/ResponseObjectTest.php @@ -32,6 +32,7 @@ public function testResponseWithErrorMessages() $this->assertEquals($errorMessages, $responseObject->getErrors()); $this->assertEquals(current($errorMessages), $responseObject->getFirstError()); $this->assertEquals('Invalid input', $responseObject->getFirstError()->title); + $this->assertEquals('Invalid input', $responseObject->getFirstErrorMessage()); $this->assertEquals('Invalid URL www.servebolt.nl.', $responseObject->getFirstError()->detail); $this->assertEquals('1115', $responseObject->getFirstError()->code); $this->assertEquals((object) [], $responseObject->getFirstError()->source); @@ -53,7 +54,7 @@ public function testResponseWithMessages() $this->assertTrue($responseObject->hasMessages()); $this->assertEquals($messages, $responseObject->getMessages()); $this->assertEquals(current($messages), $responseObject->getFirstMessage()); - $this->assertEquals('This is a notification about something', $responseObject->getFirstMessage()->message); + $this->assertEquals('This is a notification about something', $responseObject->getFirstMessage()->title); } public function testResponseStatusCode() From 376a0822ceec52f0d75b313c004281f1caf2d3fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20S=C3=A6ther?= Date: Thu, 9 Sep 2021 09:05:50 +0200 Subject: [PATCH 2/3] Made tests pass, added minor helper function to error and message trait --- src/Servebolt/Response.php | 14 ++++++++++---- src/Servebolt/Traits/HasErrors.php | 8 ++++++++ src/Servebolt/Traits/HasMessages.php | 18 ++++++++++++++++++ tests/Unit/ResponseObjectTest.php | 3 ++- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/Servebolt/Response.php b/src/Servebolt/Response.php index da01028..569fb6a 100644 --- a/src/Servebolt/Response.php +++ b/src/Servebolt/Response.php @@ -210,8 +210,11 @@ private function parseMessages() : void if (!property_exists($this, 'messages')) { return; } - if (isset($this->responseBody->messages) && is_array($this->responseBody->messages)) { - $this->messages = $this->responseBody->messages; + if ( + isset($this->responseBody->messages) + && is_array($this->responseBody->messages) + ) { + $this->setMessages($this->responseBody->messages); } } @@ -220,8 +223,11 @@ private function parseErrors() : void if (!property_exists($this, 'errors')) { return; } - if (isset($this->responseBody->errors) && is_array($this->responseBody->errors)) { - $this->errors = $this->responseBody->errors; + if ( + isset($this->responseBody->errors) + && is_array($this->responseBody->errors) + ) { + $this->setErrors($this->responseBody->errors); } } } diff --git a/src/Servebolt/Traits/HasErrors.php b/src/Servebolt/Traits/HasErrors.php index 5ba9120..1459a92 100644 --- a/src/Servebolt/Traits/HasErrors.php +++ b/src/Servebolt/Traits/HasErrors.php @@ -7,6 +7,14 @@ trait HasErrors private $errors = []; + /** + * @param array $errors + */ + private function setErrors($errors) : void + { + $this->errors = $errors; + } + public function hasErrors() : bool { return ! empty($this->errors); diff --git a/src/Servebolt/Traits/HasMessages.php b/src/Servebolt/Traits/HasMessages.php index c722296..1049235 100644 --- a/src/Servebolt/Traits/HasMessages.php +++ b/src/Servebolt/Traits/HasMessages.php @@ -7,6 +7,14 @@ trait HasMessages private $messages = []; + /** + * @param array $messages + */ + private function setMessages($messages) : void + { + $this->messages = $messages; + } + public function hasMessages() : bool { return count($this->messages) > 0; @@ -22,6 +30,16 @@ public function getMessages() : array return $this->messages; } + /** + * @return null|string + */ + public function getFirstMessageString() + { + if ($message = $this->getFirstMessage()) { + return $message->message; + } + } + /** * @return null|object */ diff --git a/tests/Unit/ResponseObjectTest.php b/tests/Unit/ResponseObjectTest.php index 7279ffa..e80f86c 100644 --- a/tests/Unit/ResponseObjectTest.php +++ b/tests/Unit/ResponseObjectTest.php @@ -54,7 +54,8 @@ public function testResponseWithMessages() $this->assertTrue($responseObject->hasMessages()); $this->assertEquals($messages, $responseObject->getMessages()); $this->assertEquals(current($messages), $responseObject->getFirstMessage()); - $this->assertEquals('This is a notification about something', $responseObject->getFirstMessage()->title); + $this->assertEquals('This is a notification about something', $responseObject->getFirstMessageString()); + $this->assertEquals('This is a notification about something', $responseObject->getFirstMessage()->message); } public function testResponseStatusCode() From 47aff3de6d5cf3f6a4d4eb4e5a3d22b9e1f6720a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20S=C3=A6ther?= Date: Thu, 9 Sep 2021 09:08:20 +0200 Subject: [PATCH 3/3] PHPCS fix --- src/Servebolt/Response.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Servebolt/Response.php b/src/Servebolt/Response.php index 569fb6a..9b032b3 100644 --- a/src/Servebolt/Response.php +++ b/src/Servebolt/Response.php @@ -210,10 +210,7 @@ private function parseMessages() : void if (!property_exists($this, 'messages')) { return; } - if ( - isset($this->responseBody->messages) - && is_array($this->responseBody->messages) - ) { + if (isset($this->responseBody->messages) && is_array($this->responseBody->messages)) { $this->setMessages($this->responseBody->messages); } } @@ -223,10 +220,7 @@ private function parseErrors() : void if (!property_exists($this, 'errors')) { return; } - if ( - isset($this->responseBody->errors) - && is_array($this->responseBody->errors) - ) { + if (isset($this->responseBody->errors) && is_array($this->responseBody->errors)) { $this->setErrors($this->responseBody->errors); } }