From b3fc74a4e795f93852a5e6f8ce6f7a56a6e53a11 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 +- .../Client/HTTPResponseInterface.php | 14 ++++++++ .../Client/ResponseAdapterInterface.php | 10 ------ app/Notification/Slack/SlackNotification.php | 4 +-- composer.json | 2 +- .../Slack/FakeSlackNotificationResponse.php | 24 +++++++++++++ .../FakeSlackNotificationResponseTest.php | 34 ++++++++++++++++++ .../Slack/SlackNotificationTest.php | 36 ------------------- 11 files changed, 84 insertions(+), 59 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 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/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'), diff --git a/composer.json b/composer.json index 8c78f37..840051c 100755 --- a/composer.json +++ b/composer.json @@ -41,6 +41,6 @@ "phpunit/phpunit": "8.5.*" }, "scripts": { - "tests": "./vendor/bin/phpunit tests --color=always --stop-on-failure", + "tests": "./vendor/bin/phpunit tests --color=always --stop-on-failure" } } 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()); - } -} From 129255fbf1b1fb2df1ad368fc7d934955fc055b3 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 --- app/Notification/AppNotificationInterface.php | 8 ++--- .../Client/Guzzle/GuzzleHTTPClient.php | 31 ----------------- .../Client/Guzzle/GuzzleHttpClient.php | 29 ++++++++++++++++ .../Client/Guzzle/HTTPResponse.php | 31 ----------------- .../Client/HTTPClientAdapterInterface.php | 10 ------ .../Client/HTTPResponseInterface.php | 14 -------- .../Client/HttpClientAdapterInterface.php | 12 +++++++ app/Notification/Slack/SlackNotification.php | 10 +++--- app/Route/Router.php | 4 +-- tests/Notification/Fake/FakeHttpClient.php | 23 +++++++++++++ .../FakeNotificationResponseTest.php | 30 ++++++++++++++++ .../Slack/FakeSlackNotificationResponse.php | 24 ------------- .../FakeSlackNotificationResponseTest.php | 34 ------------------- 13 files changed, 105 insertions(+), 155 deletions(-) delete mode 100644 app/Notification/Client/Guzzle/GuzzleHTTPClient.php create mode 100644 app/Notification/Client/Guzzle/GuzzleHttpClient.php delete mode 100644 app/Notification/Client/Guzzle/HTTPResponse.php delete mode 100644 app/Notification/Client/HTTPClientAdapterInterface.php delete mode 100644 app/Notification/Client/HTTPResponseInterface.php create mode 100644 app/Notification/Client/HttpClientAdapterInterface.php create mode 100644 tests/Notification/Fake/FakeHttpClient.php create mode 100644 tests/Notification/FakeNotificationResponseTest.php delete mode 100644 tests/Notification/Slack/FakeSlackNotificationResponse.php delete mode 100644 tests/Notification/Slack/FakeSlackNotificationResponseTest.php diff --git a/app/Notification/AppNotificationInterface.php b/app/Notification/AppNotificationInterface.php index ea4caef..d221d90 100644 --- a/app/Notification/AppNotificationInterface.php +++ b/app/Notification/AppNotificationInterface.php @@ -4,12 +4,12 @@ namespace App\Notification; -use App\Notification\Client\HTTPClientAdapterInterface; -use App\Notification\Client\HTTPResponseInterface; +use App\Notification\Client\HttpClientAdapterInterface; +use Psr\Http\Message\ResponseInterface; interface AppNotificationInterface { - public function __construct(HTTPClientAdapterInterface $client, string $message, string $messageType); + public function __construct(HttpClientAdapterInterface $client, string $message, string $messageType); - public function notify(): HTTPResponseInterface; + public function notify(): ResponseInterface; } diff --git a/app/Notification/Client/Guzzle/GuzzleHTTPClient.php b/app/Notification/Client/Guzzle/GuzzleHTTPClient.php deleted file mode 100644 index 41fee05..0000000 --- a/app/Notification/Client/Guzzle/GuzzleHTTPClient.php +++ /dev/null @@ -1,31 +0,0 @@ -client = new Client(); - } - - public function post(string $url, array $params): HTTPResponseInterface - { - $guzzleResponse = $this->client->post( - $url, - [ - 'json' => $params - ] - ); - - return new HTTPResponse($guzzleResponse); - } -} diff --git a/app/Notification/Client/Guzzle/GuzzleHttpClient.php b/app/Notification/Client/Guzzle/GuzzleHttpClient.php new file mode 100644 index 0000000..1f8b189 --- /dev/null +++ b/app/Notification/Client/Guzzle/GuzzleHttpClient.php @@ -0,0 +1,29 @@ +client = new Client(); + } + + public function post(string $url, array $params): ResponseInterface + { + return $this->client->post( + $url, + [ + 'json' => $params + ] + ); + } +} diff --git a/app/Notification/Client/Guzzle/HTTPResponse.php b/app/Notification/Client/Guzzle/HTTPResponse.php deleted file mode 100644 index 2342b07..0000000 --- a/app/Notification/Client/Guzzle/HTTPResponse.php +++ /dev/null @@ -1,31 +0,0 @@ -clientResponse = $clientResponse; - } - - public function getResponse(): array - { - return self::sanitizeResponse($this->clientResponse); - } - - private static function sanitizeResponse(ResponseInterface $guzzleResponse): array - { - return [ - 'status_code' => $guzzleResponse->getStatusCode(), - 'message' => $guzzleResponse->getReasonPhrase() - ]; - } -} diff --git a/app/Notification/Client/HTTPClientAdapterInterface.php b/app/Notification/Client/HTTPClientAdapterInterface.php deleted file mode 100644 index a76c24f..0000000 --- a/app/Notification/Client/HTTPClientAdapterInterface.php +++ /dev/null @@ -1,10 +0,0 @@ -message = new SlackStylizedMessageCreator($message, $messageType); $this->client = $client; } - public function notify(): HTTPResponseInterface + public function notify(): ResponseInterface { return $this->client->post( getenv('SLACK_API_WEBHOOK'), diff --git a/app/Route/Router.php b/app/Route/Router.php index cf432f8..c7ae75e 100644 --- a/app/Route/Router.php +++ b/app/Route/Router.php @@ -4,7 +4,7 @@ namespace App\Route; -use App\Notification\Client\Guzzle\GuzzleHTTPClient; +use App\Notification\Client\Guzzle\GuzzleHttpClient; use App\Notification\NotificationTypeEnum; use App\Notification\StatusCodeEnum; use App\Notification\Slack\SlackNotification; @@ -41,7 +41,7 @@ private static function createReflectionMethod(array $uriContent): ReflectionMet return new ReflectionMethod($uriContent['namespace'], $uriContent['method']); } catch (ReflectionException $exception) { (new SlackNotification( - new GuzzleHTTPClient(), + new GuzzleHttpClient(), $exception->getMessage(), NotificationTypeEnum::ERROR() ))->notify(); diff --git a/tests/Notification/Fake/FakeHttpClient.php b/tests/Notification/Fake/FakeHttpClient.php new file mode 100644 index 0000000..2f20403 --- /dev/null +++ b/tests/Notification/Fake/FakeHttpClient.php @@ -0,0 +1,23 @@ +response = $response; + } + + public function post(string $url, array $params): ResponseInterface + { + return $this->response; + } +} diff --git a/tests/Notification/FakeNotificationResponseTest.php b/tests/Notification/FakeNotificationResponseTest.php new file mode 100644 index 0000000..5715d95 --- /dev/null +++ b/tests/Notification/FakeNotificationResponseTest.php @@ -0,0 +1,30 @@ +mockResponse($notificationResponse); + + $response = $fakeClient->post($fakeUrl = 'https://fake.com', $emptyParams = []); + + Assert::assertEquals($expectedStatusCodeResponse = 200, $response->getStatusCode()); + Assert::assertEquals($expectedPhraseResponse = 'OK', $response->getReasonPhrase()); + } +} diff --git a/tests/Notification/Slack/FakeSlackNotificationResponse.php b/tests/Notification/Slack/FakeSlackNotificationResponse.php deleted file mode 100644 index f49287f..0000000 --- a/tests/Notification/Slack/FakeSlackNotificationResponse.php +++ /dev/null @@ -1,24 +0,0 @@ -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 deleted file mode 100644 index 7057249..0000000 --- a/tests/Notification/Slack/FakeSlackNotificationResponseTest.php +++ /dev/null @@ -1,34 +0,0 @@ - 200, - 'message' => 'OK', - ]; - - Assert::assertInstanceOf(HTTPResponseInterface::class, $fakerResponse); - Assert::assertEquals($expectedResponse, $fakerResponse->getResponse()); - } -}