Skip to content
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

Email modification after creation #192

Merged
merged 7 commits into from
Jan 9, 2023
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
6 changes: 6 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,11 @@
<file name="src/Bundle/Sender/Adapter/DefaultAdapter.php" />
</errorLevel>
</NonInvariantDocblockPropertyType>

<InvalidArgument>
<errorLevel type="suppress">
<file name="src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php" />
</errorLevel>
</InvalidArgument>
</issueHandlers>
</psalm>
8 changes: 8 additions & 0 deletions src/Bundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<argument type="service" id="sylius.email_sender.adapter" />
<argument type="service" id="sylius.email_provider" />
<argument type="service" id="sylius.mailer.default_settings_provider" />
<argument type="service" id="Sylius\Component\Mailer\Modifier\EmailModifierInterface" />
</service>
<service id="Sylius\Component\Mailer\Sender\SenderInterface" alias="sylius.email_sender" />

Expand Down Expand Up @@ -78,5 +79,12 @@
>
<argument type="service" id="mailer.mailer" />
</service>

<service
id="Sylius\Component\Mailer\Modifier\EmailModifierInterface"
class="Sylius\Component\Mailer\Modifier\CompositeEmailModifier"
>
<argument type="tagged" tag="sylius_mailer.email_modifier" />
</service>
</services>
</container>
5 changes: 5 additions & 0 deletions src/Bundle/test/config/packages/test/sylius_mailer.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
sylius_mailer:
sender:
name: Sender
address: [email protected]
sender_adapter: sylius.email_sender.adapter.symfony_mailer
emails:
test_email:
template: 'Email/test.html.twig'
test_email_with_data:
template: 'Email/testWithData.html.twig'
test_modified_email:
template: 'Email/test.html.twig'
4 changes: 4 additions & 0 deletions src/Bundle/test/config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
parameters:
locale: en_US
secret: "Three can keep a secret, if two of them are dead."

services:
App\Modifier\EmailSenderNameModifier:
tags: ['sylius_mailer.email_modifier']
29 changes: 29 additions & 0 deletions src/Bundle/test/src/Modifier/EmailSenderNameModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Modifier;

use Sylius\Component\Mailer\Model\EmailInterface;
use Sylius\Component\Mailer\Modifier\EmailModifierInterface;

final class EmailSenderNameModifier implements EmailModifierInterface
{
public function modify(EmailInterface $email, array $factors = []): EmailInterface
{
if ($email->getCode() === 'test_modified_email') {
$email->setSenderName('Modified sender name');
}

return $email;
}
}
35 changes: 34 additions & 1 deletion src/Bundle/tests/Functional/SymfonyMailerSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

namespace Sylius\Bundle\MailerBundle\tests\Functional;

use PHPUnit\Framework\ExpectationFailedException;
use Sylius\Component\Mailer\Sender\SenderInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\FrameworkBundle\Test\MailerAssertionsTrait;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\RawMessage;

final class SymfonyMailerSenderTest extends KernelTestCase
{
Expand All @@ -25,7 +28,7 @@ final class SymfonyMailerSenderTest extends KernelTestCase

protected function setUp(): void
{
self::bootKernel(['environment' => 'test_with_symfony_mailer']);
self::bootKernel(['environment' => 'test']);
$container = self::getContainer();

$this->sender = $container->get('sylius.email_sender');
Expand All @@ -41,6 +44,8 @@ public function it_sends_email_rendered_with_given_template(): void
$email = $this->getMailerMessage();
$this->assertEmailHtmlBodyContains($email, 'Test email body');
$this->assertEmailHasHeader($email, 'subject', 'Test email subject');
$this->assertEmailHasSenderName($email, 'Sender');
$this->assertEmailAddressContains($email, 'From', '[email protected]');
}

/** @test */
Expand All @@ -53,6 +58,8 @@ public function it_sends_email_rendered_with_given_template_and_data(): void
$email = $this->getMailerMessage();
$this->assertEmailHtmlBodyContains($email, 'Test email body. Data: Test data.');
$this->assertEmailHasHeader($email, 'subject', 'Test email with data subject');
$this->assertEmailHasSenderName($email, 'Sender');
$this->assertEmailAddressContains($email, 'From', '[email protected]');
}

/** @test */
Expand All @@ -71,4 +78,30 @@ public function it_sends_email_multiple_messages(): void
$this->assertEmailHtmlBodyContains($secondEmail, 'Test email body. Data: Test data.');
$this->assertEmailHasHeader($secondEmail, 'subject', 'Test email with data subject');
}

/** @test */
public function it_sends_conditionally_modified_email(): void
{
$this->sender->send('test_modified_email', ['[email protected]']);

$this->assertEmailCount(1);

$email = $this->getMailerMessage();
$this->assertEmailHtmlBodyContains($email, 'Test email body');
$this->assertEmailHasHeader($email, 'subject', 'Test email subject');
$this->assertEmailHasSenderName($email, 'Modified sender name');
$this->assertEmailAddressContains($email, 'From', '[email protected]');
}

private function assertEmailHasSenderName(?RawMessage $email, string $sender): void
{
/** @var Address $address */
foreach ($email->getHeaders()->get('from')->getAddresses() as $address) {
if ($address->getName() === $sender) {
return;
}
}

throw new ExpectationFailedException(sprintf('There is no email sent by sender with name "%s"', $sender));
}
}
36 changes: 36 additions & 0 deletions src/Component/Modifier/CompositeEmailModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Mailer\Modifier;

use Sylius\Component\Mailer\Model\EmailInterface;

final class CompositeEmailModifier implements EmailModifierInterface
{
/**
* @param EmailModifierInterface[] $emailModifiers
*/
public function __construct(
private iterable $emailModifiers = [],
) {
}

public function modify(EmailInterface $email, array $factors = []): EmailInterface
{
foreach ($this->emailModifiers as $modifier) {
$email = $modifier->modify($email, $factors);
}

return $email;
}
}
21 changes: 21 additions & 0 deletions src/Component/Modifier/EmailModifierInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Mailer\Modifier;

use Sylius\Component\Mailer\Model\EmailInterface;

interface EmailModifierInterface
{
public function modify(EmailInterface $email, array $factors = []): EmailInterface;
}
10 changes: 10 additions & 0 deletions src/Component/Sender/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sylius\Component\Mailer\Sender;

use Sylius\Component\Mailer\Modifier\EmailModifierInterface;
use Sylius\Component\Mailer\Provider\DefaultSettingsProviderInterface;
use Sylius\Component\Mailer\Provider\EmailProviderInterface;
use Sylius\Component\Mailer\Renderer\Adapter\AdapterInterface as RendererAdapterInterface;
Expand All @@ -27,7 +28,13 @@ public function __construct(
private SenderAdapterInterface $senderAdapter,
private EmailProviderInterface $provider,
private DefaultSettingsProviderInterface $defaultSettingsProvider,
private ?EmailModifierInterface $emailModifier = null,
) {
if ($this->emailModifier === null) {
@trigger_error(
'Not passing EmailModifierInterface is deprecated since 2.1 and will not be possible in 3.0',
);
}
}

/**
Expand All @@ -45,6 +52,9 @@ public function send(
Assert::allStringNotEmpty($recipients);

$email = $this->provider->getEmail($code);
if ($this->emailModifier !== null) {
$email = $this->emailModifier->modify($email, $data);
}

if (!$email->isEnabled()) {
return;
Expand Down
42 changes: 42 additions & 0 deletions src/Component/spec/Modifier/CompositeEmailModifierSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Component\Mailer\Modifier;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Mailer\Model\EmailInterface;
use Sylius\Component\Mailer\Modifier\EmailModifierInterface;

final class CompositeEmailModifierSpec extends ObjectBehavior
{
function let(EmailModifierInterface $firstEmailModifier, EmailModifierInterface $secondEmailModifier): void
{
$this->beConstructedWith([$firstEmailModifier, $secondEmailModifier]);
}

function it_implements_email_modifier_interface(): void
{
$this->shouldImplement(EmailModifierInterface::class);
}

function it_uses_all_email_modifiers_to_modify_the_email(
EmailModifierInterface $firstEmailModifier,
EmailModifierInterface $secondEmailModifier,
EmailInterface $email,
) {
$firstEmailModifier->modify($email, ['factor' => 'value'])->shouldBeCalled()->willReturn($email);
$secondEmailModifier->modify($email, ['factor' => 'value'])->shouldBeCalled()->willReturn($email);

$this->modify($email, ['factor' => 'value'])->shouldReturn($email);
}
}
39 changes: 39 additions & 0 deletions src/Component/spec/Sender/SenderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Mailer\Model\EmailInterface;
use Sylius\Component\Mailer\Modifier\EmailModifierInterface;
use Sylius\Component\Mailer\Provider\DefaultSettingsProviderInterface;
use Sylius\Component\Mailer\Provider\EmailProviderInterface;
use Sylius\Component\Mailer\Renderer\Adapter\AdapterInterface as RendererAdapterInterface;
Expand Down Expand Up @@ -93,6 +94,44 @@ function it_sends_an_email_with_cc_and_bcc_through_the_adapter(
$this->send('bar', ['[email protected]'], $data, [], [], ['[email protected]'], ['[email protected]']);
}

function it_sends_a_modified_email_with_cc_and_bcc_through_the_adapter(
EmailInterface $email,
EmailProviderInterface $provider,
RenderedEmail $renderedEmail,
RendererAdapterInterface $rendererAdapter,
SenderAdapterInterface $senderAdapter,
DefaultSettingsProviderInterface $defaultSettingsProvider,
EmailModifierInterface $emailModifier,
): void {
$this->beConstructedWith($rendererAdapter, $senderAdapter, $provider, $defaultSettingsProvider, $emailModifier);

$provider->getEmail('bar')->willReturn($email);

$data = ['foo' => 2];

$emailModifier->modify($email, $data)->shouldBeCalled()->willReturn($email);

$email->isEnabled()->willReturn(true);
$email->getSenderAddress()->willReturn('[email protected]');
$email->getSenderName()->willReturn('Modified sender');

$rendererAdapter->render($email, ['foo' => 2])->willReturn($renderedEmail);
$senderAdapter->sendWithCC(
['[email protected]'],
'[email protected]',
'Modified sender',
$renderedEmail,
$email,
$data,
[],
[],
['[email protected]'],
['[email protected]'],
)->shouldBeCalled();

$this->send('bar', ['[email protected]'], $data, [], [], ['[email protected]'], ['[email protected]']);
}

function it_does_not_send_disabled_emails(
EmailInterface $email,
EmailProviderInterface $provider,
Expand Down