Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,57 @@ public function testSendThrowsForErrorResponse()
$transport->send($mail);
}

public function testSendThrowsForErrorsResponse()
{
$client = new MockHttpClient(static function (string $method, string $url, array $options): ResponseInterface {
return new MockResponse(json_encode([
'errors' => [
'to' => [
'contains a blocked address',
'number of email addresses exceeds maximum volume',
],
'attachments.name' => ['Extension file type blocked, see Docs for full list of allowed file types'],
],
]), [
'http_code' => 400,
'response_headers' => [
'content-type' => 'application/json',
],
]);
});
$transport = new MailPaceApiTransport('KEY', $client);
$transport->setPort(8984);

$mail = new Email();
$mail->subject('Hello!')
->to(new Address('[email protected]', 'Saif Eddin'))
->from(new Address('[email protected]', 'Fabien'))
->text('Hello There!');

$this->expectException(HttpTransportException::class);
$this->expectExceptionMessage('Unable to send an email: to: contains a blocked address & number of email addresses exceeds maximum volume; attachments.name: Extension file type blocked, see Docs for full list of allowed file types (code 400).');
$transport->send($mail);
}

public function testSendThrowsForInternalServerErrorResponse()
{
$client = new MockHttpClient(static function (string $method, string $url, array $options): ResponseInterface {
return new MockResponse('', ['http_code' => 500]);
});
$transport = new MailPaceApiTransport('KEY', $client);
$transport->setPort(8984);

$mail = new Email();
$mail->subject('Hello!')
->to(new Address('[email protected]', 'Saif Eddin'))
->from(new Address('[email protected]', 'Fabien'))
->text('Hello There!');

$this->expectException(HttpTransportException::class);
$this->expectExceptionMessage('Unable to send an email: (code 500).');
$transport->send($mail);
}

public function testTagAndMetadataHeaders()
{
$email = new Email();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,20 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
}

if (200 !== $statusCode) {
throw new HttpTransportException('Unable to send an email: '.$result['error'].sprintf(' (code %d).', $statusCode), $response);
$errorMessage = 'Unable to send an email: ';
if (isset($result['error'])) {
$errorMessage .= $result['error'];
} elseif (isset($result['errors'])) {
$errors = [];
foreach ($result['errors'] as $key => $val) {
$errors[] = $key.': '.implode(' & ', $val);
}
$errorMessage .= implode('; ', $errors);
} else {
$errorMessage .= 'unknown error';
}
$errorMessage .= sprintf(' (code %d).', $statusCode);
throw new HttpTransportException($errorMessage, $response);
}

$sentMessage->setMessageId($result['id']);
Expand Down