Skip to content

Commit 6f3d593

Browse files
authored
Merge pull request #4 from jayanratna/tests
add additional tests
2 parents c627f07 + 3ecec74 commit 6f3d593

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

src/Exceptions/ErrorException.php

+24
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,28 @@ public function __construct(private readonly array $contents)
1313
{
1414
parent::__construct($contents['message']);
1515
}
16+
17+
/**
18+
* Get the error message.
19+
*/
20+
public function getErrorMessage(): string
21+
{
22+
return $this->getMessage();
23+
}
24+
25+
/**
26+
* Get the error type
27+
*/
28+
public function getErrorType(): string
29+
{
30+
return $this->contents['type'];
31+
}
32+
33+
/**
34+
* Get the error code.
35+
*/
36+
public function getErrorCode(): int
37+
{
38+
return $this->contents['code'];
39+
}
1640
}

tests/Transporters/HttpTransporter.php

+74
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
<?php
22

3+
use GuzzleHttp\Exception\ConnectException;
34
use GuzzleHttp\Psr7\Request;
45
use GuzzleHttp\Psr7\Response;
56
use Psr\Http\Client\ClientInterface;
67
use Resend\Enums\Transporter\ContentType;
8+
use Resend\Exceptions\ErrorException;
9+
use Resend\Exceptions\TransporterException;
10+
use Resend\Exceptions\UnserializableResponse;
711
use Resend\Transporters\HttpTransporter;
812
use Resend\ValueObjects\ApiKey;
913
use Resend\ValueObjects\Transporter\BaseUri;
@@ -43,3 +47,73 @@
4347

4448
$this->http->request($payload);
4549
});
50+
51+
test('request response', function () {
52+
$payload = Payload::create('email', ['to' => '[email protected]']);
53+
$response = new Response(200, [], json_encode([
54+
'id' => 'test_123',
55+
'to' => '[email protected]',
56+
]));
57+
58+
$this->client
59+
->shouldReceive('sendRequest')
60+
->once()
61+
->andReturn($response);
62+
63+
$response = $this->http->request($payload);
64+
65+
expect($response);
66+
});
67+
68+
test('request can handle client errors', function () {
69+
$payload = Payload::create('email', ['to' => '[email protected]']);
70+
71+
$baseUri = BaseUri::from('api.resend.com');
72+
$headers = Headers::withAuthorization(ApiKey::from('foo'));
73+
74+
$this->client
75+
->shouldReceive('sendRequest')
76+
->once()
77+
->andThrow(new ConnectException('Could not resolve host.', $payload->toRequest($baseUri, $headers)));
78+
79+
expect(fn () => $this->http->request($payload))->toThrow(function (TransporterException $exception) {
80+
expect($exception->getMessage())->toBe('Could not resolve host.')
81+
->and($exception->getCode())->toBe(0)
82+
->and($exception->getPrevious())->toBeInstanceOf(ConnectException::class);
83+
});
84+
});
85+
86+
test('request can handle serialization errors', function () {
87+
$payload = Payload::create('email', ['to' => '[email protected]']);
88+
$response = new Response(200, [], 'err');
89+
90+
$this->client
91+
->shouldReceive('sendRequest')
92+
->once()
93+
->andReturn($response);
94+
95+
$this->http->request($payload);
96+
})->throws(UnserializableResponse::class, 'Syntax error');
97+
98+
test('request can handle server errors', function () {
99+
$payload = Payload::create('email', ['to' => '[email protected]']);
100+
$response = new Response(422, [], json_encode([
101+
'error' => [
102+
'code' => 422,
103+
'type' => 'missing_required_field',
104+
'message' => 'Missing `to` field',
105+
],
106+
]));
107+
108+
$this->client
109+
->shouldReceive('sendRequest')
110+
->once()
111+
->andReturn($response);
112+
113+
expect(fn () => $this->http->request($payload))->toThrow(function (ErrorException $exception) {
114+
expect($exception->getMessage())->toBe('Missing `to` field')
115+
->and($exception->getErrorMessage())->toBe('Missing `to` field')
116+
->and($exception->getErrorCode())->toBe(422)
117+
->and($exception->getErrorType())->toBe('missing_required_field');
118+
});
119+
});

0 commit comments

Comments
 (0)