-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLoggerPluginTest.php
83 lines (70 loc) · 3 KB
/
LoggerPluginTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace Http\Client\Common\Plugin;
use Http\Client\Exception\HttpException;
use Http\Client\Exception\NetworkException;
use Http\Message\Formatter\SimpleFormatter;
use Http\Promise\FulfilledPromise;
use Http\Promise\RejectedPromise;
use Nyholm\Psr7\Request;
use Nyholm\Psr7\Response;
use PHPUnit\Framework\Attributes\CoversClass;
use Psr\Http\Message\RequestInterface;
use PHPUnit\Framework\TestCase;
#[CoversClass(LoggerPlugin::class)]
final class LoggerPluginTest extends TestCase
{
private TestLogger $logger;
private LoggerPlugin $plugin;
protected function setUp(): void
{
$this->logger = new TestLogger();
$this->plugin = new LoggerPlugin($this->logger, new SimpleFormatter());
}
public function testLogsRequestAndResponse()
{
$response = new Response();
$actualResponse = $this->plugin->handleRequest(
new Request('GET', 'http://example.com/'),
fn (RequestInterface $req) => new FulfilledPromise($response),
function () {}
)->wait();
self::assertSame($response, $actualResponse);
self::assertCount(2, $this->logger->logMessages);
self::assertSame("Sending request:\nGET http://example.com/ 1.1", $this->logger->logMessages[0]['info']);
self::assertSame("Received response:\n200 OK 1.1", $this->logger->logMessages[1]['info']);
}
public function testLogsRequestException()
{
$this->expectException(NetworkException::class);
try {
$this->plugin->handleRequest(
new Request('GET', 'http://example.com/'),
fn (RequestInterface $req) => new RejectedPromise(new NetworkException('Network error', $req)),
function () {}
)->wait();
} catch (NetworkException $exception) {
self::assertCount(2, $this->logger->logMessages);
self::assertSame("Sending request:\nGET http://example.com/ 1.1", $this->logger->logMessages[0]['info']);
self::assertSame("Error:\nNetwork error\nwhen sending request:\nGET http://example.com/ 1.1", $this->logger->logMessages[1]['error']);
throw $exception;
}
}
public function testLogsHttpException()
{
$this->expectException(HttpException::class);
try {
$this->plugin->handleRequest(
new Request('GET', 'http://example.com/'),
fn (RequestInterface $req) => new RejectedPromise(new HttpException('Not Found', $req, new Response())),
function () {}
)->wait();
} catch (HttpException $exception) {
// Expected
$this->assertCount(2, $this->logger->logMessages);
$this->assertSame("Sending request:\nGET http://example.com/ 1.1", $this->logger->logMessages[0]['info']);
// Ensure there's an error log for the exception
$this->assertStringContainsString("Error:\nNot Found", $this->logger->logMessages[1]['error']);
throw $exception;
}
}
}