Skip to content

Commit

Permalink
Improve Notification Test
Browse files Browse the repository at this point in the history
  • Loading branch information
mathleite committed Mar 27, 2020
1 parent b3fc74a commit d537479
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 97 deletions.
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\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;
}
31 changes: 0 additions & 31 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;


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

final class GuzzleHttpClient implements HttpClientAdapterInterface
{
private Client $client;

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/HTTPResponse.php

This file was deleted.

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

This file was deleted.

14 changes: 0 additions & 14 deletions app/Notification/Client/HTTPResponseInterface.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;


use Psr\Http\Message\ResponseInterface;

interface HttpClientAdapterInterface
{
public function post(string $url, array $params): ResponseInterface;
}
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\HTTPResponseInterface;
use App\Notification\Client\HttpClientAdapterInterface;
use Psr\Http\Message\ResponseInterface;

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

public function __construct(HTTPClientAdapterInterface $client, string $message, string $messageType)
public function __construct(HttpClientAdapterInterface $client, string $message, string $messageType)
{
$this->message = new SlackStylizedMessageCreator($message, $messageType);
$this->client = $client;
}

public function notify(): HTTPResponseInterface
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
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


namespace App\Test\Notification\Fake;


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());
}
}

0 comments on commit d537479

Please sign in to comment.