|
12 | 12 | use Symfony\Component\HttpClient\MockHttpClient;
|
13 | 13 | use Symfony\Component\HttpClient\Response\MockResponse;
|
14 | 14 | use Symfony\Component\HttpFoundation\Request;
|
| 15 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
15 | 16 |
|
16 | 17 | class SparkpostTransportTest extends MauticMysqlTestCase
|
17 | 18 | {
|
18 | 19 | protected function setUp(): void
|
19 | 20 | {
|
20 |
| - $this->configParams['mailer_dsn'] = 'mautic+sparkpost+api://:some_api@some_host:25?region=us'; |
21 |
| - $this->configParams['messenger_dsn_email'] = 'sync://'; |
| 21 | + $this->configParams['mailer_dsn'] = 'mautic+sparkpost+api://:some_api@some_host:25?region=us'; |
| 22 | + $this->configParams['messenger_dsn_email'] = 'sync://'; |
| 23 | + $this->configParams['mailer_custom_headers'] = ['x-global-custom-header' => 'value123']; |
| 24 | + $this-> configParams[ 'mailer_from_email'] = '[email protected]'; |
22 | 25 | parent::setUp();
|
23 | 26 | }
|
24 | 27 |
|
25 | 28 | public function testEmailSendToContactSync(): void
|
26 | 29 | {
|
27 | 30 | $expectedResponses = [
|
28 | 31 | function ($method, $url, $options): MockResponse {
|
29 |
| - $payload = file_get_contents(__DIR__.'/../../SparkpostResponses/content-previewer.json'); |
30 | 32 | Assert::assertSame(Request::METHOD_POST, $method);
|
31 | 33 | Assert::assertSame('https://api.sparkpost.com/api/v1/utils/content-previewer/', $url);
|
32 |
| - $requestBodyArray = json_decode($options['body'], true); |
33 |
| - $payloadArray = json_decode($payload, true); |
34 |
| - Assert::assertArrayHasKey('UNSUBSCRIBETEXT', $requestBodyArray['substitution_data']); |
35 |
| - Assert::assertArrayHasKey('UNSUBSCRIBEURL', $requestBodyArray['substitution_data']); |
36 |
| - Assert::assertArrayHasKey('WEBVIEWTEXT', $requestBodyArray['substitution_data']); |
37 |
| - Assert::assertArrayHasKey('WEBVIEWURL', $requestBodyArray['substitution_data']); |
38 |
| - Assert::assertArrayHasKey('TRACKINGPIXEL', $requestBodyArray['substitution_data']); |
39 |
| - // These keys have dynamic hash id for tracking, no way to validate these, so unsetting them |
40 |
| - unset( |
41 |
| - $requestBodyArray['substitution_data']['UNSUBSCRIBETEXT'], |
42 |
| - $requestBodyArray['substitution_data']['UNSUBSCRIBEURL'], |
43 |
| - $requestBodyArray['substitution_data']['WEBVIEWTEXT'], |
44 |
| - $requestBodyArray['substitution_data']['WEBVIEWURL'], |
45 |
| - $requestBodyArray['substitution_data']['TRACKINGPIXEL'], |
46 |
| - ); |
47 |
| - Assert::assertSame($payloadArray, $requestBodyArray); |
48 |
| - Assert::assertSame( |
49 |
| - [ |
50 |
| - 'Authorization: some_api', |
51 |
| - 'Content-Type: application/json', |
52 |
| - 'Accept: */*', |
53 |
| - 'Content-Length: 1157', |
54 |
| - ], |
55 |
| - $options['headers'] |
56 |
| - ); |
57 |
| - $body = '{"results": {"subject": "Hello there!", "html": "This is test body for {contactfield=email}!"}}'; |
58 |
| - |
59 |
| - return new MockResponse($body); |
| 34 | + $this->assertSparkpostRequestBody($options['body']); |
| 35 | + |
| 36 | + return new MockResponse('{"results": {"subject": "Hello there!", "html": "This is test body for {contactfield=email}!"}}'); |
60 | 37 | },
|
61 | 38 | function ($method, $url, $options): MockResponse {
|
62 |
| - $payload = file_get_contents(__DIR__.'/../../SparkpostResponses/transmissions.json'); |
63 | 39 | Assert::assertSame(Request::METHOD_POST, $method);
|
64 | 40 | Assert::assertSame('https://api.sparkpost.com/api/v1/transmissions/', $url);
|
65 |
| - $requestBodyArray = json_decode($options['body'], true); |
66 |
| - $payloadArray = json_decode($payload, true); |
67 |
| - Assert::assertArrayHasKey('UNSUBSCRIBETEXT', $requestBodyArray['recipients'][0]['substitution_data']); |
68 |
| - Assert::assertArrayHasKey('UNSUBSCRIBEURL', $requestBodyArray['recipients'][0]['substitution_data']); |
69 |
| - Assert::assertArrayHasKey('WEBVIEWTEXT', $requestBodyArray['recipients'][0]['substitution_data']); |
70 |
| - Assert::assertArrayHasKey('WEBVIEWURL', $requestBodyArray['recipients'][0]['substitution_data']); |
71 |
| - Assert::assertArrayHasKey('TRACKINGPIXEL', $requestBodyArray['recipients'][0]['substitution_data']); |
72 |
| - // These keys have dynamic hash id for tracking, no way to validate these, so unsetting them |
73 |
| - unset( |
74 |
| - $requestBodyArray['recipients'][0]['substitution_data']['UNSUBSCRIBETEXT'], |
75 |
| - $requestBodyArray['recipients'][0]['substitution_data']['UNSUBSCRIBEURL'], |
76 |
| - $requestBodyArray['recipients'][0]['substitution_data']['WEBVIEWTEXT'], |
77 |
| - $requestBodyArray['recipients'][0]['substitution_data']['WEBVIEWURL'], |
78 |
| - $requestBodyArray['recipients'][0]['substitution_data']['TRACKINGPIXEL'], |
79 |
| - $requestBodyArray['recipients'][0]['metadata']['hashId'], |
80 |
| - $requestBodyArray['recipients'][0]['metadata']['leadId'], |
81 |
| - ); |
82 |
| - Assert::assertSame($payloadArray, $requestBodyArray); |
83 |
| - Assert::assertSame( |
84 |
| - [ |
85 |
| - 'Authorization: some_api', |
86 |
| - 'Content-Type: application/json', |
87 |
| - 'Accept: */*', |
88 |
| - 'Content-Length: 1370', |
89 |
| - ], |
90 |
| - $options['headers'] |
91 |
| - ); |
92 |
| - $body = '{"results": {"total_rejected_recipients": 0, "total_accepted_recipients": 1, "id": "11668787484950529"}}'; |
93 |
| - |
94 |
| - return new MockResponse($body); |
| 41 | + $this->assertSparkpostRequestBody($options['body']); |
| 42 | + |
| 43 | + return new MockResponse('{"results": {"total_rejected_recipients": 0, "total_accepted_recipients": 1, "id": "11668787484950529"}}'); |
95 | 44 | },
|
96 | 45 | ];
|
97 | 46 |
|
98 |
| - $mockHttpClient = self::getContainer()->get('http_client'); |
99 |
| - Assert::assertInstanceOf(MockHttpClient::class, $mockHttpClient); // @phpstan-ignore-line |
| 47 | + $mockHttpClient = self::getContainer()->get(HttpClientInterface::class); |
| 48 | + \assert($mockHttpClient instanceof MockHttpClient); |
100 | 49 | $mockHttpClient->setResponseFactory($expectedResponses);
|
101 | 50 |
|
102 | 51 | $contact = $this-> createContact( '[email protected]');
|
@@ -136,6 +85,26 @@ function ($method, $url, $options): MockResponse {
|
136 | 85 | Assert::assertSame('', $email->getReplyTo()[0]->getName());
|
137 | 86 | }
|
138 | 87 |
|
| 88 | + private function assertSparkpostRequestBody(string $body): void |
| 89 | + { |
| 90 | + $bodyArray = json_decode($body, true); |
| 91 | + Assert:: assertSame( 'Admin User <[email protected]>', $bodyArray[ 'content'][ 'from']); |
| 92 | + Assert::assertSame('value123', $bodyArray['content']['headers']['x-global-custom-header']); |
| 93 | + Assert::assertSame('This is test body for {{{ CONTACTFIELDEMAIL }}}!<img height="1" width="1" src="{{{ TRACKINGPIXEL }}}" alt="" />', $bodyArray['content']['html']); |
| 94 | + Assert:: assertSame( '[email protected]', $bodyArray[ 'content'][ 'reply_to']); |
| 95 | + Assert::assertSame('Hello there!', $bodyArray['content']['subject']); |
| 96 | + Assert::assertSame('This is test body for {{{ CONTACTFIELDEMAIL }}}!', $bodyArray['content']['text']); |
| 97 | + Assert::assertSame(['open_tracking' => false, 'click_tracking' => false], $bodyArray['options']); |
| 98 | + Assert:: assertSame( '[email protected]', $bodyArray[ 'substitution_data'][ 'CONTACTFIELDEMAIL']); |
| 99 | + Assert::assertSame('Hello there!', $bodyArray['substitution_data']['SUBJECT']); |
| 100 | + Assert::assertArrayHasKey('SIGNATURE', $bodyArray['substitution_data']); |
| 101 | + Assert::assertArrayHasKey('TRACKINGPIXEL', $bodyArray['substitution_data']); |
| 102 | + Assert::assertArrayHasKey('UNSUBSCRIBETEXT', $bodyArray['substitution_data']); |
| 103 | + Assert::assertArrayHasKey('UNSUBSCRIBEURL', $bodyArray['substitution_data']); |
| 104 | + Assert::assertArrayHasKey('WEBVIEWTEXT', $bodyArray['substitution_data']); |
| 105 | + Assert::assertArrayHasKey('WEBVIEWURL', $bodyArray['substitution_data']); |
| 106 | + } |
| 107 | + |
139 | 108 | private function createContact(string $email): Lead
|
140 | 109 | {
|
141 | 110 | $lead = new Lead();
|
|
0 commit comments