Skip to content

Commit

Permalink
Replaced Swift Mailer with Symfony Mailer
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-newvisibility committed Dec 14, 2022
1 parent ca0a3cc commit 7e942e8
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 75 deletions.
46 changes: 38 additions & 8 deletions Mail/MailFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
namespace Sulu\Bundle\CommunityBundle\Mail;

use Sulu\Bundle\SecurityBundle\Entity\User;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
Expand All @@ -22,7 +25,7 @@
class MailFactory implements MailFactoryInterface
{
/**
* @var \Swift_Mailer
* @var MailerInterface
*/
protected $mailer;

Expand All @@ -36,7 +39,7 @@ class MailFactory implements MailFactoryInterface
*/
protected $translator;

public function __construct(\Swift_Mailer $mailer, Environment $twig, TranslatorInterface $translator)
public function __construct(MailerInterface $mailer, Environment $twig, TranslatorInterface $translator)
{
$this->mailer = $mailer;
$this->twig = $twig;
Expand Down Expand Up @@ -69,6 +72,7 @@ public function sendEmails(Mail $mail, User $user, array $parameters = []): void
}
}


/**
* Create and send email.
*
Expand All @@ -80,12 +84,38 @@ protected function sendEmail($from, $to, string $subject, string $template, arra
{
$body = $this->twig->render($template, $data);

$message = new \Swift_Message();
$message->setSubject($this->translator->trans($subject));
$message->setFrom($from);
$message->setTo($to);
$message->setBody($body, 'text/html');
$email = (new Email())
->subject($this->translator->trans($subject))
->from($this->getAddress($from))
->to($this->getAddress($to))
->html($body);

$this->mailer->send($email);
}

/**
* Convert string/array email address to an Address object
*
* @param $address
* @return Address
*/
protected function getAddress($address): ?Address
{
$name = '';

if (is_array($address)) {
if(empty($address)) {
return null;
} else if (!isset($address['email'])) {
$email = $address[array_keys($address)[0]];
} else {
$email = $address['email'];
$name = $address['name'] ?? '';
}
} else {
$email = $address;
}

$this->mailer->send($message);
return new Address($email, $name);
}
}
41 changes: 18 additions & 23 deletions Tests/Functional/Controller/RegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
use Sulu\Component\HttpKernel\SuluKernel;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\SwiftmailerBundle\DataCollector\MessageDataCollector;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Component\Mime\RawMessage;

/**
* This testcases covers the whole registration, confirmation and login process.
Expand Down Expand Up @@ -181,7 +181,7 @@ public function testRegistrationBlacklistedBlocked(): void
$this->assertNull($this->findUser());
}

public function testRegistrationBlacklistedRequested(): \Swift_Message
public function testRegistrationBlacklistedRequested(): RawMessage
{
$this->createBlacklistItem($this->getEntityManager(), '*@sulu.io', BlacklistItem::TYPE_REQUEST);

Expand Down Expand Up @@ -209,11 +209,10 @@ public function testRegistrationBlacklistedRequested(): \Swift_Message
$profile = $this->client->getProfile();
$this->assertNotFalse($profile, 'Could not found response profile, is profiler activated?');

/** @var MessageDataCollector $mailCollector */
$mailCollector = $profile->getCollector('swiftmailer');
$this->assertSame(1, $mailCollector->getMessageCount());
$message = $mailCollector->getMessages()[0];
$this->assertSame('admin@localhost', \key($message->getTo()));
$this->assertEmailCount(1);

$message = $this->getMailerMessage();
$this->assertSame('admin@localhost', $message->getTo()[0]->getAddress());

return $message;
}
Expand All @@ -223,7 +222,7 @@ public function testBlacklistConfirm(): void
$message = $this->testRegistrationBlacklistedRequested();

$emailCrawler = new Crawler();
$emailCrawler->addContent($message->getBody());
$emailCrawler->addContent($message->getHtmlBody());

$links = $emailCrawler->filter('a');
$firstLink = $links->first()->attr('href');
Expand All @@ -241,19 +240,18 @@ public function testBlacklistConfirm(): void
$profile = $this->client->getProfile();
$this->assertNotFalse($profile, 'Could not found response profile, is profiler activated?');

/** @var MessageDataCollector $mailCollector */
$mailCollector = $profile->getCollector('swiftmailer');
$this->assertSame(1, $mailCollector->getMessageCount());
$message = $mailCollector->getMessages()[0];
$this->assertSame('[email protected]', \key($message->getTo()));
$this->assertEmailCount(1);

$message = $this->getMailerMessage();
$this->assertSame('[email protected]', $message->getTo()[0]->getAddress());
}

public function testBlacklistBlocked(): void
{
$message = $this->testRegistrationBlacklistedRequested();

$emailCrawler = new Crawler();
$emailCrawler->addContent($message->getBody());
$emailCrawler->addContent($message->getHtmlBody());

$links = $emailCrawler->filter('a');
$lastLink = $links->last()->attr('href');
Expand All @@ -271,9 +269,7 @@ public function testBlacklistBlocked(): void
$profile = $this->client->getProfile();
$this->assertNotFalse($profile, 'Could not found response profile, is profiler activated?');

/** @var MessageDataCollector $mailCollector */
$mailCollector = $profile->getCollector('swiftmailer');
$this->assertSame(0, $mailCollector->getMessageCount());
$this->assertEmailCount(0);
}

public function testPasswordForget(): void
Expand All @@ -298,14 +294,13 @@ public function testPasswordForget(): void
$profile = $this->client->getProfile();
$this->assertNotFalse($profile, 'Could not found response profile, is profiler activated?');

/** @var MessageDataCollector $mailCollector */
$mailCollector = $profile->getCollector('swiftmailer');
$this->assertSame(1, $mailCollector->getMessageCount());
$message = $mailCollector->getMessages()[0];
$this->assertSame('[email protected]', \key($message->getTo()));
$this->assertEmailCount(1);

$message = $this->getMailerMessage();
$this->assertSame('[email protected]', $message->getTo()[0]->getAddress());

$emailCrawler = new Crawler();
$emailCrawler->addContent($message->getBody());
$emailCrawler->addContent($message->getHtmlBody());
$links = $emailCrawler->filter('a');

$firstLink = $links->first()->attr('href');
Expand Down
79 changes: 35 additions & 44 deletions Tests/Unit/Mail/MailFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
use Sulu\Bundle\CommunityBundle\Mail\Mail;
use Sulu\Bundle\CommunityBundle\Mail\MailFactory;
use Sulu\Bundle\SecurityBundle\Entity\User;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Translation\Translator;
use Twig\Environment;

class MailFactoryTest extends TestCase
{
/**
* @var ObjectProphecy<\Swift_Mailer>
* @var ObjectProphecy<MailerInterface>
*/
private $mailer;

Expand All @@ -49,10 +51,11 @@ class MailFactoryTest extends TestCase

protected function setUp(): void
{
$this->mailer = $this->prophesize(\Swift_Mailer::class);
$this->mailer = $this->prophesize(MailerInterface::class);
$this->twig = $this->prophesize(Environment::class);
$this->translator = $this->prophesize(Translator::class);
$this->translator->getLocale()->willReturn('en');
$this->translator->trans('testcase')->willReturn('Test case');
$this->user = $this->prophesize(User::class);
$this->user->getEmail()->willReturn('[email protected]');
$this->user->getLocale()->willReturn('de');
Expand All @@ -70,22 +73,18 @@ public function testSendEmails(): void
$this->twig->render('admin-template', Argument::any())->willReturn('Admin-Template');

$this->mailer->send(
Argument::that(
function (\Swift_Message $message) {
return 'User-Template' === $message->getBody()
&& $message->getFrom() === ['[email protected]' => null]
&& $message->getTo() === ['[email protected]' => null];
}
)
(new Email())
->subject('Test case')
->from('[email protected]')
->to('[email protected]')
->html('User-Template')
)->shouldBeCalledTimes(1);
$this->mailer->send(
Argument::that(
function (\Swift_Message $message) {
return 'Admin-Template' === $message->getBody()
&& $message->getFrom() === ['[email protected]' => null]
&& $message->getTo() === ['[email protected]' => null];
}
)
(new Email())
->subject('Test case')
->from('[email protected]')
->to('[email protected]')
->html('Admin-Template')
)->shouldBeCalledTimes(1);

$mail = new Mail('[email protected]', '[email protected]', 'testcase', 'user-template', 'admin-template');
Expand All @@ -100,22 +99,18 @@ public function testSendEmailsNoAdminTemplate(): void
$this->twig->render('admin-template', Argument::any())->willReturn('Admin-Template');

$this->mailer->send(
Argument::that(
function (\Swift_Message $message) {
return 'User-Template' === $message->getBody()
&& $message->getFrom() === ['[email protected]' => null]
&& $message->getTo() === ['[email protected]' => null];
}
)
(new Email())
->subject('Test case')
->from('[email protected]')
->to('[email protected]')
->html('User-Template')
)->shouldBeCalledTimes(1);
$this->mailer->send(
Argument::that(
function (\Swift_Message $message) {
return 'Admin-Template' === $message->getBody()
&& $message->getFrom() === ['[email protected]' => null]
&& $message->getTo() === ['[email protected]' => null];
}
)
(new Email())
->subject('Test case')
->from('[email protected]')
->to('[email protected]')
->html('Admin-Template')
)->shouldNotBeCalled();

$mail = new Mail('[email protected]', '[email protected]', 'testcase', 'user-template', null);
Expand All @@ -130,22 +125,18 @@ public function testSendEmailsNoUserTemplate(): void
$this->twig->render('admin-template', Argument::any())->willReturn('Admin-Template');

$this->mailer->send(
Argument::that(
function (\Swift_Message $message) {
return 'User-Template' === $message->getBody()
&& $message->getFrom() === ['[email protected]' => null]
&& $message->getTo() === ['[email protected]' => null];
}
)
(new Email())
->subject('Test case')
->from('[email protected]')
->to('[email protected]')
->html('User-Template')
)->shouldNotBeCalled();
$this->mailer->send(
Argument::that(
function (\Swift_Message $message) {
return 'Admin-Template' === $message->getBody()
&& $message->getFrom() === ['[email protected]' => null]
&& $message->getTo() === ['[email protected]' => null];
}
)
(new Email())
->subject('Test case')
->from('[email protected]')
->to('[email protected]')
->html('Admin-Template')
)->shouldBeCalledTimes(1);

$mail = new Mail('[email protected]', '[email protected]', 'testcase', null, 'admin-template');
Expand Down

0 comments on commit 7e942e8

Please sign in to comment.