From 69bcc3859ae943f45906a0a3d1f41ed2626dfbdd Mon Sep 17 00:00:00 2001 From: Matheus Leite Date: Fri, 27 Mar 2020 11:52:41 -0300 Subject: [PATCH 1/2] Improve HTTP Client structure --- app/Notification/AppNotificationInterface.php | 4 ++-- .../Client/Guzzle/GuzzleHTTPClient.php | 9 ++++----- .../{GuzzleResponse.php => HTTPResponse.php} | 4 ++-- .../Client/HTTPClientAdapterInterface.php | 2 +- app/Notification/Client/HTTPResponseInterface.php | 14 ++++++++++++++ .../Client/ResponseAdapterInterface.php | 10 ---------- app/Notification/Slack/SlackNotification.php | 4 ++-- 7 files changed, 25 insertions(+), 22 deletions(-) rename app/Notification/Client/Guzzle/{GuzzleResponse.php => HTTPResponse.php} (85%) create mode 100644 app/Notification/Client/HTTPResponseInterface.php delete mode 100644 app/Notification/Client/ResponseAdapterInterface.php diff --git a/app/Notification/AppNotificationInterface.php b/app/Notification/AppNotificationInterface.php index 3d7acc9..ea4caef 100644 --- a/app/Notification/AppNotificationInterface.php +++ b/app/Notification/AppNotificationInterface.php @@ -5,11 +5,11 @@ use App\Notification\Client\HTTPClientAdapterInterface; -use App\Notification\Client\ResponseAdapterInterface; +use App\Notification\Client\HTTPResponseInterface; interface AppNotificationInterface { public function __construct(HTTPClientAdapterInterface $client, string $message, string $messageType); - public function notify(): ResponseAdapterInterface; + public function notify(): HTTPResponseInterface; } diff --git a/app/Notification/Client/Guzzle/GuzzleHTTPClient.php b/app/Notification/Client/Guzzle/GuzzleHTTPClient.php index 4be067c..41fee05 100644 --- a/app/Notification/Client/Guzzle/GuzzleHTTPClient.php +++ b/app/Notification/Client/Guzzle/GuzzleHTTPClient.php @@ -5,9 +5,8 @@ use App\Notification\Client\HTTPClientAdapterInterface; -use App\Notification\Client\ResponseAdapterInterface; +use App\Notification\Client\HTTPResponseInterface; use GuzzleHttp\Client; -use Psr\Http\Message\ResponseInterface; class GuzzleHTTPClient implements HTTPClientAdapterInterface { @@ -18,15 +17,15 @@ public function __construct() $this->client = new Client(); } - public function post(string $url, array $params): ResponseAdapterInterface + public function post(string $url, array $params): HTTPResponseInterface { - $clientResponse = $this->client->post( + $guzzleResponse = $this->client->post( $url, [ 'json' => $params ] ); - return new GuzzleResponse($clientResponse); + return new HTTPResponse($guzzleResponse); } } diff --git a/app/Notification/Client/Guzzle/GuzzleResponse.php b/app/Notification/Client/Guzzle/HTTPResponse.php similarity index 85% rename from app/Notification/Client/Guzzle/GuzzleResponse.php rename to app/Notification/Client/Guzzle/HTTPResponse.php index a641b21..2342b07 100644 --- a/app/Notification/Client/Guzzle/GuzzleResponse.php +++ b/app/Notification/Client/Guzzle/HTTPResponse.php @@ -4,10 +4,10 @@ namespace App\Notification\Client\Guzzle; -use App\Notification\Client\ResponseAdapterInterface; +use App\Notification\Client\HTTPResponseInterface; use Psr\Http\Message\ResponseInterface; -class GuzzleResponse implements ResponseAdapterInterface +class HTTPResponse implements HTTPResponseInterface { private ResponseInterface $clientResponse; diff --git a/app/Notification/Client/HTTPClientAdapterInterface.php b/app/Notification/Client/HTTPClientAdapterInterface.php index 25c7c16..a76c24f 100644 --- a/app/Notification/Client/HTTPClientAdapterInterface.php +++ b/app/Notification/Client/HTTPClientAdapterInterface.php @@ -6,5 +6,5 @@ interface HTTPClientAdapterInterface { - public function post(string $url, array $params): ResponseAdapterInterface; + public function post(string $url, array $params): HTTPResponseInterface; } diff --git a/app/Notification/Client/HTTPResponseInterface.php b/app/Notification/Client/HTTPResponseInterface.php new file mode 100644 index 0000000..6f32925 --- /dev/null +++ b/app/Notification/Client/HTTPResponseInterface.php @@ -0,0 +1,14 @@ +client = $client; } - public function notify(): ResponseAdapterInterface + public function notify(): HTTPResponseInterface { return $this->client->post( getenv('SLACK_API_WEBHOOK'), From 680c62ab328883dd4f3f46e62f252819a010bf15 Mon Sep 17 00:00:00 2001 From: Matheus Leite Date: Fri, 27 Mar 2020 11:59:52 -0300 Subject: [PATCH 2/2] Improve Notification Test --- .../Slack/FakeSlackNotificationResponse.php | 24 +++++++++++++ .../FakeSlackNotificationResponseTest.php | 34 ++++++++++++++++++ .../Slack/SlackNotificationTest.php | 36 ------------------- 3 files changed, 58 insertions(+), 36 deletions(-) create mode 100644 tests/Notification/Slack/FakeSlackNotificationResponse.php create mode 100644 tests/Notification/Slack/FakeSlackNotificationResponseTest.php delete mode 100644 tests/Notification/Slack/SlackNotificationTest.php diff --git a/tests/Notification/Slack/FakeSlackNotificationResponse.php b/tests/Notification/Slack/FakeSlackNotificationResponse.php new file mode 100644 index 0000000..f49287f --- /dev/null +++ b/tests/Notification/Slack/FakeSlackNotificationResponse.php @@ -0,0 +1,24 @@ +response = $response; + } + + public function getResponse(): array + { + return (new HTTPResponse($this->response))->getResponse(); + } +} diff --git a/tests/Notification/Slack/FakeSlackNotificationResponseTest.php b/tests/Notification/Slack/FakeSlackNotificationResponseTest.php new file mode 100644 index 0000000..7057249 --- /dev/null +++ b/tests/Notification/Slack/FakeSlackNotificationResponseTest.php @@ -0,0 +1,34 @@ + 200, + 'message' => 'OK', + ]; + + Assert::assertInstanceOf(HTTPResponseInterface::class, $fakerResponse); + Assert::assertEquals($expectedResponse, $fakerResponse->getResponse()); + } +} diff --git a/tests/Notification/Slack/SlackNotificationTest.php b/tests/Notification/Slack/SlackNotificationTest.php deleted file mode 100644 index 8aa25aa..0000000 --- a/tests/Notification/Slack/SlackNotificationTest.php +++ /dev/null @@ -1,36 +0,0 @@ -notify(); - - $expectedResponse = [ - 'status_code' => 200, - 'message' => 'OK', - ]; - - Assert::assertInstanceOf(ResponseAdapterInterface::class, $notificationResponse); - Assert::assertEquals($expectedResponse, $notificationResponse->getResponse()); - } -}