Skip to content

[Mailer] Fix exception message on invalid event in SendgridPayloadConverter #58400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2024
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 @@ -39,7 +39,7 @@ public function convert(array $payload): AbstractMailerEvent
'unsubscribe' => MailerEngagementEvent::UNSUBSCRIBE,
'open' => MailerEngagementEvent::OPEN,
'spamreport' => MailerEngagementEvent::SPAM,
default => throw new ParseException(sprintf('Unsupported event "%s".', $payload['unsubscribe'])),
default => throw new ParseException(sprintf('Unsupported event "%s".', $payload['event'])),
};
$event = new MailerEngagementEvent($name, $payload['sg_message_id'], $payload);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Symfony\Component\Mailer\Bridge\Sendgrid\Tests\RemoteEvent;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Bridge\Sendgrid\RemoteEvent\SendgridPayloadConverter;
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
use Symfony\Component\RemoteEvent\Exception\ParseException;

class SendgridPayloadConverterTest extends TestCase
{
/**
* @dataProvider provideDeliveryEvents
*/
public function testMailDeliveryEvent(string $event, string $expectedEventName)
{
$converter = new SendgridPayloadConverter();

$event = $converter->convert([
'event' => $event,
'sg_message_id' => '123456',
'reason' => 'reason',
'timestamp' => '123456789',
'email' => 'test@example.com',
]);

$this->assertInstanceOf(MailerDeliveryEvent::class, $event);
$this->assertSame($expectedEventName, $event->getName());
$this->assertSame('123456', $event->getId());
$this->assertSame('reason', $event->getReason());
$this->assertSame('test@example.com', $event->getRecipientEmail());
}

public static function provideDeliveryEvents(): iterable
{
yield ['processed', MailerDeliveryEvent::DELIVERED];
yield ['delivered', MailerDeliveryEvent::DELIVERED];
yield ['bounce', MailerDeliveryEvent::BOUNCE];
yield ['dropped', MailerDeliveryEvent::DROPPED];
yield ['deferred', MailerDeliveryEvent::DEFERRED];
}

/**
* @dataProvider provideEngagementEvents
*/
public function testMailEngagementEvent(string $event, string $expectedEventName)
{
$converter = new SendgridPayloadConverter();

$event = $converter->convert([
'event' => $event,
'sg_message_id' => '123456',
'timestamp' => '123456789',
'email' => 'test@example.com',
]);

$this->assertInstanceOf(MailerEngagementEvent::class, $event);
$this->assertSame($expectedEventName, $event->getName());
$this->assertSame('123456', $event->getId());
}

public static function provideEngagementEvents(): iterable
{
yield ['click', MailerEngagementEvent::CLICK];
yield ['unsubscribe', MailerEngagementEvent::UNSUBSCRIBE];
yield ['open', MailerEngagementEvent::OPEN];
yield ['spamreport', MailerEngagementEvent::SPAM];
}

public function testUnsupportedEvent()
{
$converter = new SendgridPayloadConverter();

$this->expectException(ParseException::class);
$this->expectExceptionMessage('Unsupported event "unsupported".');

$converter->convert([
'event' => 'unsupported',
'sg_message_id' => '123456',
'timestamp' => '123456789',
'email' => 'test@example.com',
]);
}

public function testInvalidDate()
{
$converter = new SendgridPayloadConverter();

$this->expectException(ParseException::class);
$this->expectExceptionMessage('Invalid date "invalid".');

$converter->convert([
'event' => 'processed',
'sg_message_id' => '123456',
'timestamp' => 'invalid',
'email' => 'test@example.com',
]);
}
}