Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Make simplified service, improve test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ts-navghane committed Aug 1, 2023
1 parent 930e95b commit 5bef440
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 76 deletions.
11 changes: 1 addition & 10 deletions Config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use MauticPlugin\SparkpostBundle\Mailer\Factory\SparkpostTransportFactory;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $configurator) {
$services = $configurator->services()
->defaults()
Expand All @@ -16,12 +14,5 @@
$services->load('MauticPlugin\\SparkpostBundle\\', '../')
->exclude('../{Config,Helper/SparkpostResponse.php,Mailer/Transport/SparkpostTransport.php}');

$services->get(SparkpostTransportFactory::class)
->args([
service('mautic.email.model.transport_callback'),
service('event_dispatcher'),
service('http_client'),
service('logger'),
])
->tag('mailer.transport_factory');
$services->get(SparkpostTransportFactory::class)->tag('mailer.transport_factory');
};
97 changes: 33 additions & 64 deletions Tests/Functional/Mailer/Transport/SparkpostTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,91 +12,40 @@
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class SparkpostTransportTest extends MauticMysqlTestCase
{
protected function setUp(): void
{
$this->configParams['mailer_dsn'] = 'mautic+sparkpost+api://:some_api@some_host:25?region=us';
$this->configParams['messenger_dsn_email'] = 'sync://';
$this->configParams['mailer_dsn'] = 'mautic+sparkpost+api://:some_api@some_host:25?region=us';
$this->configParams['messenger_dsn_email'] = 'sync://';
$this->configParams['mailer_custom_headers'] = ['x-global-custom-header' => 'value123'];
$this->configParams['mailer_from_email'] = '[email protected]';
parent::setUp();
}

public function testEmailSendToContactSync(): void
{
$expectedResponses = [
function ($method, $url, $options): MockResponse {
$payload = file_get_contents(__DIR__.'/../../SparkpostResponses/content-previewer.json');
Assert::assertSame(Request::METHOD_POST, $method);
Assert::assertSame('https://api.sparkpost.com/api/v1/utils/content-previewer/', $url);
$requestBodyArray = json_decode($options['body'], true);
$payloadArray = json_decode($payload, true);
Assert::assertArrayHasKey('UNSUBSCRIBETEXT', $requestBodyArray['substitution_data']);
Assert::assertArrayHasKey('UNSUBSCRIBEURL', $requestBodyArray['substitution_data']);
Assert::assertArrayHasKey('WEBVIEWTEXT', $requestBodyArray['substitution_data']);
Assert::assertArrayHasKey('WEBVIEWURL', $requestBodyArray['substitution_data']);
Assert::assertArrayHasKey('TRACKINGPIXEL', $requestBodyArray['substitution_data']);
// These keys have dynamic hash id for tracking, no way to validate these, so unsetting them
unset(
$requestBodyArray['substitution_data']['UNSUBSCRIBETEXT'],
$requestBodyArray['substitution_data']['UNSUBSCRIBEURL'],
$requestBodyArray['substitution_data']['WEBVIEWTEXT'],
$requestBodyArray['substitution_data']['WEBVIEWURL'],
$requestBodyArray['substitution_data']['TRACKINGPIXEL'],
);
Assert::assertSame($payloadArray, $requestBodyArray);
Assert::assertSame(
[
'Authorization: some_api',
'Content-Type: application/json',
'Accept: */*',
'Content-Length: 1157',
],
$options['headers']
);
$body = '{"results": {"subject": "Hello there!", "html": "This is test body for {contactfield=email}!"}}';

return new MockResponse($body);
$this->assertSparkpostRequestBody($options['body']);

return new MockResponse('{"results": {"subject": "Hello there!", "html": "This is test body for {contactfield=email}!"}}');
},
function ($method, $url, $options): MockResponse {
$payload = file_get_contents(__DIR__.'/../../SparkpostResponses/transmissions.json');
Assert::assertSame(Request::METHOD_POST, $method);
Assert::assertSame('https://api.sparkpost.com/api/v1/transmissions/', $url);
$requestBodyArray = json_decode($options['body'], true);
$payloadArray = json_decode($payload, true);
Assert::assertArrayHasKey('UNSUBSCRIBETEXT', $requestBodyArray['recipients'][0]['substitution_data']);
Assert::assertArrayHasKey('UNSUBSCRIBEURL', $requestBodyArray['recipients'][0]['substitution_data']);
Assert::assertArrayHasKey('WEBVIEWTEXT', $requestBodyArray['recipients'][0]['substitution_data']);
Assert::assertArrayHasKey('WEBVIEWURL', $requestBodyArray['recipients'][0]['substitution_data']);
Assert::assertArrayHasKey('TRACKINGPIXEL', $requestBodyArray['recipients'][0]['substitution_data']);
// These keys have dynamic hash id for tracking, no way to validate these, so unsetting them
unset(
$requestBodyArray['recipients'][0]['substitution_data']['UNSUBSCRIBETEXT'],
$requestBodyArray['recipients'][0]['substitution_data']['UNSUBSCRIBEURL'],
$requestBodyArray['recipients'][0]['substitution_data']['WEBVIEWTEXT'],
$requestBodyArray['recipients'][0]['substitution_data']['WEBVIEWURL'],
$requestBodyArray['recipients'][0]['substitution_data']['TRACKINGPIXEL'],
$requestBodyArray['recipients'][0]['metadata']['hashId'],
$requestBodyArray['recipients'][0]['metadata']['leadId'],
);
Assert::assertSame($payloadArray, $requestBodyArray);
Assert::assertSame(
[
'Authorization: some_api',
'Content-Type: application/json',
'Accept: */*',
'Content-Length: 1370',
],
$options['headers']
);
$body = '{"results": {"total_rejected_recipients": 0, "total_accepted_recipients": 1, "id": "11668787484950529"}}';

return new MockResponse($body);
$this->assertSparkpostRequestBody($options['body']);

return new MockResponse('{"results": {"total_rejected_recipients": 0, "total_accepted_recipients": 1, "id": "11668787484950529"}}');
},
];

$mockHttpClient = self::getContainer()->get('http_client');
Assert::assertInstanceOf(MockHttpClient::class, $mockHttpClient); // @phpstan-ignore-line
$mockHttpClient = self::getContainer()->get(HttpClientInterface::class);
\assert($mockHttpClient instanceof MockHttpClient);
$mockHttpClient->setResponseFactory($expectedResponses);

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

private function assertSparkpostRequestBody(string $body): void
{
$bodyArray = json_decode($body, true);
Assert::assertSame('Admin User <[email protected]>', $bodyArray['content']['from']);
Assert::assertSame('value123', $bodyArray['content']['headers']['x-global-custom-header']);
Assert::assertSame('This is test body for {{{ CONTACTFIELDEMAIL }}}!<img height="1" width="1" src="{{{ TRACKINGPIXEL }}}" alt="" />', $bodyArray['content']['html']);
Assert::assertSame('[email protected]', $bodyArray['content']['reply_to']);
Assert::assertSame('Hello there!', $bodyArray['content']['subject']);
Assert::assertSame('This is test body for {{{ CONTACTFIELDEMAIL }}}!', $bodyArray['content']['text']);
Assert::assertSame(['open_tracking' => false, 'click_tracking' => false], $bodyArray['options']);
Assert::assertSame('[email protected]', $bodyArray['substitution_data']['CONTACTFIELDEMAIL']);
Assert::assertSame('Hello there!', $bodyArray['substitution_data']['SUBJECT']);
Assert::assertArrayHasKey('SIGNATURE', $bodyArray['substitution_data']);
Assert::assertArrayHasKey('TRACKINGPIXEL', $bodyArray['substitution_data']);
Assert::assertArrayHasKey('UNSUBSCRIBETEXT', $bodyArray['substitution_data']);
Assert::assertArrayHasKey('UNSUBSCRIBEURL', $bodyArray['substitution_data']);
Assert::assertArrayHasKey('WEBVIEWTEXT', $bodyArray['substitution_data']);
Assert::assertArrayHasKey('WEBVIEWURL', $bodyArray['substitution_data']);
}

private function createContact(string $email): Lead
{
$lead = new Lead();
Expand Down
1 change: 0 additions & 1 deletion Tests/Functional/SparkpostResponses/content-previewer.json

This file was deleted.

1 change: 0 additions & 1 deletion Tests/Functional/SparkpostResponses/transmissions.json

This file was deleted.

0 comments on commit 5bef440

Please sign in to comment.