Skip to content

Commit 5bef440

Browse files
committed
Make simplified service, improve test case
1 parent 930e95b commit 5bef440

File tree

4 files changed

+34
-76
lines changed

4 files changed

+34
-76
lines changed

Config/services.php

+1-10
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
use MauticPlugin\SparkpostBundle\Mailer\Factory\SparkpostTransportFactory;
66
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
77

8-
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
9-
108
return static function (ContainerConfigurator $configurator) {
119
$services = $configurator->services()
1210
->defaults()
@@ -16,12 +14,5 @@
1614
$services->load('MauticPlugin\\SparkpostBundle\\', '../')
1715
->exclude('../{Config,Helper/SparkpostResponse.php,Mailer/Transport/SparkpostTransport.php}');
1816

19-
$services->get(SparkpostTransportFactory::class)
20-
->args([
21-
service('mautic.email.model.transport_callback'),
22-
service('event_dispatcher'),
23-
service('http_client'),
24-
service('logger'),
25-
])
26-
->tag('mailer.transport_factory');
17+
$services->get(SparkpostTransportFactory::class)->tag('mailer.transport_factory');
2718
};

Tests/Functional/Mailer/Transport/SparkpostTransportTest.php

+33-64
Original file line numberDiff line numberDiff line change
@@ -12,91 +12,40 @@
1212
use Symfony\Component\HttpClient\MockHttpClient;
1313
use Symfony\Component\HttpClient\Response\MockResponse;
1414
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Contracts\HttpClient\HttpClientInterface;
1516

1617
class SparkpostTransportTest extends MauticMysqlTestCase
1718
{
1819
protected function setUp(): void
1920
{
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]';
2225
parent::setUp();
2326
}
2427

2528
public function testEmailSendToContactSync(): void
2629
{
2730
$expectedResponses = [
2831
function ($method, $url, $options): MockResponse {
29-
$payload = file_get_contents(__DIR__.'/../../SparkpostResponses/content-previewer.json');
3032
Assert::assertSame(Request::METHOD_POST, $method);
3133
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}!"}}');
6037
},
6138
function ($method, $url, $options): MockResponse {
62-
$payload = file_get_contents(__DIR__.'/../../SparkpostResponses/transmissions.json');
6339
Assert::assertSame(Request::METHOD_POST, $method);
6440
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"}}');
9544
},
9645
];
9746

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);
10049
$mockHttpClient->setResponseFactory($expectedResponses);
10150

10251
$contact = $this->createContact('[email protected]');
@@ -136,6 +85,26 @@ function ($method, $url, $options): MockResponse {
13685
Assert::assertSame('', $email->getReplyTo()[0]->getName());
13786
}
13887

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+
139108
private function createContact(string $email): Lead
140109
{
141110
$lead = new Lead();

Tests/Functional/SparkpostResponses/content-previewer.json

-1
This file was deleted.

Tests/Functional/SparkpostResponses/transmissions.json

-1
This file was deleted.

0 commit comments

Comments
 (0)