Skip to content

Commit d537479

Browse files
committed
Improve Notification Test
1 parent b3fc74a commit d537479

11 files changed

+105
-97
lines changed

app/Notification/AppNotificationInterface.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
namespace App\Notification;
55

66

7-
use App\Notification\Client\HTTPClientAdapterInterface;
8-
use App\Notification\Client\HTTPResponseInterface;
7+
use App\Notification\Client\HttpClientAdapterInterface;
8+
use Psr\Http\Message\ResponseInterface;
99

1010
interface AppNotificationInterface
1111
{
12-
public function __construct(HTTPClientAdapterInterface $client, string $message, string $messageType);
12+
public function __construct(HttpClientAdapterInterface $client, string $message, string $messageType);
1313

14-
public function notify(): HTTPResponseInterface;
14+
public function notify(): ResponseInterface;
1515
}

app/Notification/Client/Guzzle/GuzzleHTTPClient.php

-31
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
4+
namespace App\Notification\Client\Guzzle;
5+
6+
7+
use App\Notification\Client\HttpClientAdapterInterface;
8+
use GuzzleHttp\Client;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
final class GuzzleHttpClient implements HttpClientAdapterInterface
12+
{
13+
private Client $client;
14+
15+
public function __construct()
16+
{
17+
$this->client = new Client();
18+
}
19+
20+
public function post(string $url, array $params): ResponseInterface
21+
{
22+
return $this->client->post(
23+
$url,
24+
[
25+
'json' => $params
26+
]
27+
);
28+
}
29+
}

app/Notification/Client/Guzzle/HTTPResponse.php

-31
This file was deleted.

app/Notification/Client/HTTPClientAdapterInterface.php

-10
This file was deleted.

app/Notification/Client/HTTPResponseInterface.php

-14
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
4+
namespace App\Notification\Client;
5+
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
interface HttpClientAdapterInterface
10+
{
11+
public function post(string $url, array $params): ResponseInterface;
12+
}

app/Notification/Slack/SlackNotification.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55

66

77
use App\Notification\AppNotificationInterface;
8-
use App\Notification\Client\HTTPClientAdapterInterface;
9-
use App\Notification\Client\HTTPResponseInterface;
8+
use App\Notification\Client\HttpClientAdapterInterface;
9+
use Psr\Http\Message\ResponseInterface;
1010

1111
final class SlackNotification implements AppNotificationInterface
1212
{
1313
private SlackStylizedMessageCreator $message;
14-
private HTTPClientAdapterInterface $client;
14+
private HttpClientAdapterInterface $client;
1515

16-
public function __construct(HTTPClientAdapterInterface $client, string $message, string $messageType)
16+
public function __construct(HttpClientAdapterInterface $client, string $message, string $messageType)
1717
{
1818
$this->message = new SlackStylizedMessageCreator($message, $messageType);
1919
$this->client = $client;
2020
}
2121

22-
public function notify(): HTTPResponseInterface
22+
public function notify(): ResponseInterface
2323
{
2424
return $this->client->post(
2525
getenv('SLACK_API_WEBHOOK'),

app/Route/Router.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace App\Route;
55

66

7-
use App\Notification\Client\Guzzle\GuzzleHTTPClient;
7+
use App\Notification\Client\Guzzle\GuzzleHttpClient;
88
use App\Notification\NotificationTypeEnum;
99
use App\Notification\StatusCodeEnum;
1010
use App\Notification\Slack\SlackNotification;
@@ -41,7 +41,7 @@ private static function createReflectionMethod(array $uriContent): ReflectionMet
4141
return new ReflectionMethod($uriContent['namespace'], $uriContent['method']);
4242
} catch (ReflectionException $exception) {
4343
(new SlackNotification(
44-
new GuzzleHTTPClient(),
44+
new GuzzleHttpClient(),
4545
$exception->getMessage(),
4646
NotificationTypeEnum::ERROR()
4747
))->notify();
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
4+
namespace App\Test\Notification\Fake;
5+
6+
7+
use App\Notification\Client\HttpClientAdapterInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
class FakeHttpClient implements HttpClientAdapterInterface
11+
{
12+
private ResponseInterface $response;
13+
14+
public function mockResponse(ResponseInterface $response): void
15+
{
16+
$this->response = $response;
17+
}
18+
19+
public function post(string $url, array $params): ResponseInterface
20+
{
21+
return $this->response;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Test\Notification\Slack;
4+
5+
use App\Test\Notification\Fake\FakeHttpClient;
6+
use GuzzleHttp\Psr7\Response;
7+
use PHPUnit\Framework\Assert;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class FakeNotificationResponseTest extends TestCase
11+
{
12+
public static function test_FakeHttpClient_ShouldAssertHttpResponse(): void
13+
{
14+
$notificationResponse = new Response(
15+
$statusCode = 200,
16+
$responseHeader = [],
17+
$responseBody = null,
18+
$protocolVersion = '1.1',
19+
$responseReason = 'OK'
20+
);
21+
22+
$fakeClient = new FakeHttpClient();
23+
$fakeClient->mockResponse($notificationResponse);
24+
25+
$response = $fakeClient->post($fakeUrl = 'https://fake.com', $emptyParams = []);
26+
27+
Assert::assertEquals($expectedStatusCodeResponse = 200, $response->getStatusCode());
28+
Assert::assertEquals($expectedPhraseResponse = 'OK', $response->getReasonPhrase());
29+
}
30+
}

0 commit comments

Comments
 (0)