-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SparkPostApiTransport handleError response
- Loading branch information
Showing
5 changed files
with
114 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/.idea | ||
/composer.lock | ||
/vendor/ | ||
/.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
], | ||
"require": { | ||
"php": "^7.1", | ||
"ext-json": "*", | ||
"symfony/mailer": "^4.4|^5.0" | ||
}, | ||
"require-dev": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,11 @@ | |
*/ | ||
class SinkEnvelopeListenerTest extends TestCase | ||
{ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
BypassFinals::enable(); | ||
} | ||
|
||
public function testSinkEmail() | ||
{ | ||
$envelope = new Envelope(Address::fromString('test case <[email protected]>'), [ | ||
|
@@ -37,8 +42,6 @@ public function testSinkEmail() | |
|
||
public function testNoPrefix() | ||
{ | ||
BypassFinals::enable(); | ||
|
||
$messageEvent = $this->createMock(MessageEvent::class); | ||
$messageEvent | ||
->expects(self::never()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Mailer\DelayedEnvelope; | ||
use Symfony\Component\Mailer\Envelope; | ||
use Symfony\Component\Mailer\Exception\HttpTransportException; | ||
use Symfony\Component\Mime\Address; | ||
use Symfony\Component\Mime\Email; | ||
use Symfony\Component\Mime\RawMessage; | ||
|
@@ -21,15 +22,43 @@ | |
*/ | ||
class SparkPostApiTransportTest extends TestCase | ||
{ | ||
private function createDefaultMessage(): Email | ||
{ | ||
return (new Email()) | ||
->from('[email protected]') | ||
->to('[email protected]') | ||
->subject('Test email') | ||
->text('Test email for you!') | ||
->attach(str_pad('', 200, 'A'), 'name.txt'); | ||
} | ||
|
||
private function createDefaultEnvelope(): Envelope | ||
{ | ||
return new Envelope(new Address('[email protected]'), [new Address('[email protected]')]); | ||
} | ||
|
||
private function createDefaultResponse(): ResponseInterface | ||
{ | ||
$response = $this->createMock(ResponseInterface::class); | ||
$response | ||
->expects(self::atLeastOnce()) | ||
->method('getStatusCode') | ||
->willReturn(200); | ||
return $response; | ||
} | ||
|
||
/** | ||
* @dataProvider dataSend | ||
*/ | ||
public function testSend(RawMessage $message, Envelope $envelope, string $expectedJson) | ||
public function testSend(RawMessage $message, Envelope $envelope, string $expectedJson): void | ||
{ | ||
$response = $this->createDefaultResponse(); | ||
|
||
$client = $this->createMock(HttpClientInterface::class); | ||
$client->expects(self::once()) | ||
$client | ||
->expects(self::once()) | ||
->method('request') | ||
->willReturnCallback(function (string $method, string $url, array $options = []) use ($expectedJson) { | ||
->willReturnCallback(function (string $method, string $url, array $options = []) use ($expectedJson, $response) { | ||
self::assertEquals('POST', $method); | ||
self::assertEquals('https://api.sparkpost.com/api/v1/transmissions/', $url); | ||
self::assertArrayHasKey('headers', $options); | ||
|
@@ -40,7 +69,7 @@ public function testSend(RawMessage $message, Envelope $envelope, string $expect | |
self::assertIsArray($options['json']); | ||
self::assertJsonStringEqualsJsonString($expectedJson, json_encode($options['json'])); | ||
|
||
return $this->createMock(ResponseInterface::class); | ||
return $response; | ||
}); | ||
$transport = new SparkPostApiTransport('api-key', $client); | ||
$transport->send($message, $envelope); | ||
|
@@ -198,20 +227,15 @@ public function dataSend() | |
]; | ||
} | ||
|
||
public function testTruncateAttachemntData() | ||
public function testTruncateAttachmentData(): void | ||
{ | ||
$message = (new Email()) | ||
->from('[email protected]') | ||
->to('[email protected]') | ||
->subject('Test email') | ||
->text('Test email for you!') | ||
->attach(str_pad('', 200, 'A'), 'name.txt'); | ||
$envelope = new Envelope(new Address('[email protected]'), [new Address('[email protected]')]); | ||
$response = $this->createDefaultResponse(); | ||
|
||
$client = $this->createMock(HttpClientInterface::class); | ||
$client | ||
->expects(self::once()) | ||
->method('request'); | ||
->method('request') | ||
->willReturn($response); | ||
|
||
$logger = $this->createMock(LoggerInterface::class); | ||
$logger | ||
|
@@ -223,7 +247,7 @@ public function testTruncateAttachemntData() | |
self::assertSame('<<<truncated>>>', $context['content']['attachments'][0]['data']); | ||
}); | ||
$transport = new SparkPostApiTransport('api-key', $client, null, $logger); | ||
$transport->send($message, $envelope); | ||
$transport->send($this->createDefaultMessage(), $this->createDefaultEnvelope()); | ||
} | ||
|
||
public function testToString() | ||
|
@@ -232,4 +256,48 @@ public function testToString() | |
$transport = new SparkPostApiTransport('api-key', $client); | ||
self::assertSame('sparkpost+api://', (string) $transport); | ||
} | ||
|
||
public function testNotSuccess(): void | ||
{ | ||
self::expectException(HttpTransportException::class); | ||
self::expectExceptionMessage('[1902] Message generation rejected. Blocked Sending Domain <domain.com>.'); | ||
|
||
$error = [ | ||
'errors' => [ | ||
[ | ||
'message' => 'Message generation rejected', | ||
'description' => 'Blocked Sending Domain <domain.com>', | ||
'code' => '1902', | ||
], | ||
], | ||
'results' => [ | ||
'total_rejected_recipients' => 0, | ||
'total_accepted_recipients' => 1, | ||
'id' => '012345678901234567', | ||
], | ||
]; | ||
$response = $this->createMock(ResponseInterface::class); | ||
$response | ||
->expects(self::atLeastOnce()) | ||
->method('getStatusCode') | ||
->willReturn(400); | ||
$response | ||
->expects(self::atLeastOnce()) | ||
->method('getContent') | ||
->willReturn(json_encode($error)); | ||
|
||
$client = $this->createMock(HttpClientInterface::class); | ||
$client | ||
->expects(self::once()) | ||
->method('request') | ||
->willReturn($response); | ||
|
||
$logger = $this->createMock(LoggerInterface::class); | ||
$logger | ||
->expects(self::once()) | ||
->method('error'); | ||
|
||
$transport = new SparkPostApiTransport('api-key', $client, null, $logger); | ||
$transport->send($this->createDefaultMessage(), $this->createDefaultEnvelope()); | ||
} | ||
} |