Skip to content

Improve HTTP Client structure #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/Notification/AppNotificationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
namespace App\Notification;


use App\Notification\Client\HTTPClientAdapterInterface;
use App\Notification\Client\ResponseAdapterInterface;
use App\Notification\Client\HttpClientAdapterInterface;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This namespace doesn't match the file structure.

Suggested change
use App\Notification\Client\HttpClientAdapterInterface;
use App\Notification\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(): ResponseAdapterInterface;
public function notify(): ResponseInterface;
}
32 changes: 0 additions & 32 deletions app/Notification/Client/Guzzle/GuzzleHTTPClient.php

This file was deleted.

29 changes: 29 additions & 0 deletions app/Notification/Client/Guzzle/GuzzleHttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php


namespace App\Notification\Client\Guzzle;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
namespace App\Notification\Client\Guzzle;
namespace App\Notification\HttpClient\Adapter;



use App\Notification\Client\HttpClientAdapterInterface;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;

final class GuzzleHttpClient implements HttpClientAdapterInterface
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems at the moment there is no test for this class

{
private Client $client;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private Client $client;
private Client $guzzle;


public function __construct()
{
$this->client = new Client();
}

public function post(string $url, array $params): ResponseInterface
{
return $this->client->post(
$url,
[
'json' => $params
]
);
}
}
31 changes: 0 additions & 31 deletions app/Notification/Client/Guzzle/GuzzleResponse.php

This file was deleted.

10 changes: 0 additions & 10 deletions app/Notification/Client/HTTPClientAdapterInterface.php

This file was deleted.

12 changes: 12 additions & 0 deletions app/Notification/Client/HttpClientAdapterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php


namespace App\Notification\Client;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more appropriated to have HttpClient instead of just Client as subdomain, because Client alone doesn't mean much, opposed as HttpClient

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise will leave the impression those are NotificationClients instead



use Psr\Http\Message\ResponseInterface;

interface HttpClientAdapterInterface
Copy link

@deenison deenison Apr 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if Adapter in the name is really necessary - (this is not a change request)

{
public function post(string $url, array $params): ResponseInterface;
}
10 changes: 0 additions & 10 deletions app/Notification/Client/ResponseAdapterInterface.php

This file was deleted.

10 changes: 5 additions & 5 deletions app/Notification/Slack/SlackNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@


use App\Notification\AppNotificationInterface;
use App\Notification\Client\HTTPClientAdapterInterface;
use App\Notification\Client\ResponseAdapterInterface;
use App\Notification\Client\HttpClientAdapterInterface;
use Psr\Http\Message\ResponseInterface;

final class SlackNotification implements AppNotificationInterface
{
private SlackStylizedMessageCreator $message;
private HTTPClientAdapterInterface $client;
private HttpClientAdapterInterface $client;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private HttpClientAdapterInterface $client;
private HttpClientAdapterInterface $httpClient;


public function __construct(HTTPClientAdapterInterface $client, string $message, string $messageType)
public function __construct(HttpClientAdapterInterface $client, string $message, string $messageType)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function __construct(HttpClientAdapterInterface $client, string $message, string $messageType)
public function __construct(HttpClientAdapterInterface $httpClient, string $message, string $messageType)

{
$this->message = new SlackStylizedMessageCreator($message, $messageType);
$this->client = $client;
}

public function notify(): ResponseAdapterInterface
public function notify(): ResponseInterface
{
return $this->client->post(
getenv('SLACK_API_WEBHOOK'),
Expand Down
4 changes: 2 additions & 2 deletions app/Route/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn't belong to the main intent of this PR. A more appropriated approach would be to split things up.:

  1. we put this PR on hold
  2. create a PR fixing the issue
  3. Once it got merged, we get back here, rebase and re-send it

}
}
23 changes: 23 additions & 0 deletions tests/Notification/Fake/FakeHttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to establish a pattern/consistency regarding the application files structure - tabs vs spaces, how many, blank lines between <?php and namespace statements, etc :)

There a lot of variations it seems.

I'd suggest you take a look on https://editorconfig.org


namespace App\Test\Notification\Fake;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems Fake is actually a "concrete" application subdomain. This may be very misleading.

An approach to get it clear up, is to have a test-subdomain called Support:

tests/
  app/
    ...  
  support/
    Notification/
      HttpClient/
        Adapter/
          FakeHttpClient.php
    ...

and then pointing it out on composer.json:

"autoload-dev": {
  "psr-4": {
    "App\\Test\\": "tests/app",
    "App\\Test\\Support\\": "tests/support"
  }
}

(perhaps "Test" should be the first term, actually - but this is for another discussion in another time)



use App\Notification\Client\HttpClientAdapterInterface;
use Psr\Http\Message\ResponseInterface;

class FakeHttpClient implements HttpClientAdapterInterface
{
private ResponseInterface $response;

public function mockResponse(ResponseInterface $response): void
{
$this->response = $response;
}

public function post(string $url, array $params): ResponseInterface
{
return $this->response;
}
}
30 changes: 30 additions & 0 deletions tests/Notification/FakeNotificationResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Test\Notification\Slack;

use App\Test\Notification\Fake\FakeHttpClient;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;

class FakeNotificationResponseTest extends TestCase
{
public static function test_FakeHttpClient_ShouldAssertHttpResponse(): void
{
$notificationResponse = new Response(
$statusCode = 200,
$responseHeader = [],
$responseBody = null,
$protocolVersion = '1.1',
$responseReason = 'OK'
);

$fakeClient = new FakeHttpClient();
$fakeClient->mockResponse($notificationResponse);

$response = $fakeClient->post($fakeUrl = 'https://fake.com', $emptyParams = []);

Assert::assertEquals($expectedStatusCodeResponse = 200, $response->getStatusCode());
Assert::assertEquals($expectedPhraseResponse = 'OK', $response->getReasonPhrase());
}
}
36 changes: 0 additions & 36 deletions tests/Notification/Slack/SlackNotificationTest.php

This file was deleted.