From 8dc2d692173145453b52d216b6cb51f8c7375808 Mon Sep 17 00:00:00 2001 From: Noelie Date: Wed, 12 Jun 2024 17:03:54 +0200 Subject: [PATCH 1/2] Update Configuration.php for TreeBuilder fix for symfony 5 --- DependencyInjection/Configuration.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 6ad9d07..d31e265 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -14,8 +14,13 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('donkey_code_mail'); + $treeBuilder = new TreeBuilder('donkey_code_mail'); + + if (method_exists($treeBuilder, 'getRootNode')) { + $rootNode = $treeBuilder->getRootNode(); + } else { + $rootNode = $treeBuilder->root('donkey_code_mail'); + } $rootNode ->children() From 97eefe1b7cc37b0606288c52bc0a267c9e3e0f51 Mon Sep 17 00:00:00 2001 From: Laetitia Rodriguez Date: Fri, 19 Jul 2024 18:20:57 +0200 Subject: [PATCH 2/2] update sf6 --- Mailer/Mailer.php | 67 +++++++++++++++++------------------ Resources/config/services.xml | 2 +- Tests/Mailer/MailerTest.php | 42 +++++++++++++--------- composer.json | 7 ++-- phpunit.xml | 13 ++----- 5 files changed, 66 insertions(+), 65 deletions(-) diff --git a/Mailer/Mailer.php b/Mailer/Mailer.php index 3e68291..7804df3 100644 --- a/Mailer/Mailer.php +++ b/Mailer/Mailer.php @@ -2,21 +2,23 @@ namespace DonkeyCode\MailBundle\Mailer; +use Symfony\Component\Mailer\MailerInterface; +use Symfony\Component\Mime\Email; +use Symfony\Component\Mime\Address; +use Twig\Environment; + class Mailer { - private $swift; + private $mailer; private $twig; private $from; - private $logo_url; + private $replyTo; private $message; private $options; - /** - * $twig : Twig_Environment or \Twig\Environment - */ - public function __construct(\Swift_Mailer $swift, $twig, $from, $replyTo, array $options) + public function __construct(MailerInterface $mailer, Environment $twig, string $from, string $replyTo, array $options) { - $this->swift = $swift; + $this->mailer = $mailer; $this->twig = $twig; $this->from = $from; $this->replyTo = $replyTo; @@ -28,18 +30,17 @@ public function __construct(\Swift_Mailer $swift, $twig, $from, $replyTo, array */ public function createMessage() { - $this->message = new \Swift_Message(); - - $this->message->setFrom($this->from); - $this->message->setReplyTo($this->replyTo); + $this->message = (new Email()) + ->from($this->from) + ->replyTo($this->replyTo); return $this; } /** - * @return \Swift_Message + * @return Email */ - public function getMessage() + public function getMessage(): Email { if (!$this->message) { $this->createMessage(); @@ -56,7 +57,7 @@ public function getMessage() * * @return $this */ - public function setTemplate($templateName, array $vars = array()) + public function setTemplate(string $templateName, array $vars = []): self { $vars = array_merge($vars, $this->twig->getGlobals(), [ 'donkeycode_mail' => $this->options ]); @@ -64,15 +65,15 @@ public function setTemplate($templateName, array $vars = array()) $template = $this->twig->load($templateName); if ($template->hasBlock('subject', [])) { - $this->getMessage()->setSubject(trim($template->renderBlock('subject', $vars))); + $this->getMessage()->subject(trim($template->renderBlock('subject', $vars))); } if ($template->hasBlock('body', [])) { $body = $template->renderBlock('body', $vars); - $this->getMessage()->setBody($body, 'text/html'); + $this->getMessage()->html($body); if (!$template->hasBlock('text', [])) { - $this->getMessage()->addPart($this->html2txt($body), 'text/plain'); + $this->getMessage()->text($this->html2txt($body)); } } @@ -80,70 +81,68 @@ public function setTemplate($templateName, array $vars = array()) $text = $template->renderBlock('text', $vars); if (!$template->hasBlock('body', [])) { - $this->getMessage()->setBody($text, 'text/plain'); + $this->getMessage()->text($text); } else { - $this->getMessage()->addPart($this->html2txt($text), 'text/plain'); + $this->getMessage()->text($this->html2txt($text)); } } if ($template->hasBlock('from_email', [])) { if ($template->hasBlock('from_name', [])) { - $this->getMessage()->setFrom([ - $template->renderBlock('from_email', $vars) => $template->renderBlock('from_name', $vars), - ]); + $this->getMessage()->from(new Address($template->renderBlock('from_email', $vars), $template->renderBlock('from_name', $vars))); } else { - $this->getMessage()->setFrom($template->renderBlock('from_email', $vars)); + $this->getMessage()->from($template->renderBlock('from_email', $vars)); } } if ($template->hasBlock('reply_to', [])) { - $this->getMessage()->setReplyTo($template->renderBlock('reply_to', $vars)); + $this->getMessage()->replyTo($template->renderBlock('reply_to', $vars)); } return $this; } /** - * @param array[\Swift_Attachment] $attachments + * @param array $attachments * * @return $this */ - public function attachArray(array $attachments) + public function attachArray(array $attachments): self { foreach ($attachments as $attachment) { - $this->getMessage()->attach($attachment); + $this->getMessage()->attachFromPath($attachment); } return $this; } /** - * @see \Swift_Mailer::send() + * @see send() */ public function send() { - return $this->swift->send($this->getMessage()); + $this->mailer->send($this->getMessage()); } /** - * Give the hand to \Swift_Message functions. + * Give the hand to Email functions. * * @param string $method * @param array $args * * @return $this */ - public function __call($method, array $args = array()) + public function __call(string $method, array $args = []) { - call_user_func_array(array($this->getMessage(), $method), $args); + call_user_func_array([$this->getMessage(), $method], $args); return $this; } - private function html2txt($document) + private function html2txt(string $document): string { $parts = explode('', $document, 2); - return str_replace(' ', '', strip_tags($parts[sizeof($parts) - 1])); + return str_replace(' ', '', strip_tags($parts[count($parts) - 1])); } } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 9497c8f..ff1acdb 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -10,7 +10,7 @@ - + diff --git a/Tests/Mailer/MailerTest.php b/Tests/Mailer/MailerTest.php index f976864..50fb191 100644 --- a/Tests/Mailer/MailerTest.php +++ b/Tests/Mailer/MailerTest.php @@ -3,32 +3,34 @@ namespace DonkeyCode\MailBundle\Tests\Mailer; use DonkeyCode\MailBundle\Mailer\Mailer; - -use DonkeyCode\MailBundle\Tests\TestCase; -use Swift_Mailer; -use Swift_Transport_NullTransport; -use Swift_Events_SimpleEventDispatcher; -use Twig_Environment; -use Twig_Loader_Array; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Mailer\MailerInterface; +use Symfony\Component\Mime\Email; +use Symfony\Component\Mime\Part\TextPart; +use Twig\Environment; +use Twig\Loader\ArrayLoader; class MailerTest extends TestCase { - public function setUp() + private $twigLoader; + private $mailer; + + public function setUp(): void { - $swift = new Swift_Mailer(new Swift_Transport_NullTransport(new Swift_Events_SimpleEventDispatcher())); - $this->twigLoader = new Twig_Loader_Array([]); - $twig = new Twig_Environment($this->twigLoader); + $mailer = $this->createMock(MailerInterface::class); + $this->twigLoader = new ArrayLoader([]); + $twig = new Environment($this->twigLoader); - $this->mailer = new Mailer($swift, $twig, "from@mail.com", "reply@mail.com"); + $this->mailer = new Mailer($mailer, $twig, "from@mail.com", "reply@mail.com", []); } public function testCreateMessage() { $this->mailer->createMessage(); - $this->assertInstanceOf('Swift_Message', $this->mailer->getMessage(), 'The message is a Swift_Message'); + $this->assertInstanceOf(Email::class, $this->mailer->getMessage(), 'The message is not an instance of Email'); - $this->mailer->getMessage()->setSubject('Test first'); + $this->mailer->getMessage()->subject('Test first'); $this->mailer->createMessage(); $this->assertNotEquals('Test first', $this->mailer->getMessage()->getSubject(), 'Create message always return a new message'); @@ -36,7 +38,7 @@ public function testCreateMessage() public function testGetMessage() { - $this->mailer->getMessage()->setSubject('Test first'); + $this->mailer->getMessage()->subject('Test first'); $this->assertEquals('Test first', $this->mailer->getMessage()->getSubject(), 'getMessage return the last message created'); } @@ -54,7 +56,13 @@ public function testSetTemplate1() ]); $this->assertEquals('The subject of mail with simple var', $this->mailer->getMessage()->getSubject(), 'Subject extracted from twig block subject'); - $this->assertEquals('The textblock simple var', $this->mailer->getMessage()->getBody(), 'Body in text mode extracted from twig block text'); - $this->assertEquals('The textblock simple var', $this->mailer->getMessage()->getBody(), 'Body in text mode extracted from twig block text'); + + $textBody = $this->mailer->getMessage()->getTextBody(); + if ($textBody instanceof TextPart) { + $textBody = $textBody->getBody(); + } + + $this->assertNotNull($textBody, 'Text body should not be null'); + $this->assertStringContainsString('The textblock simple var', $textBody, 'Body in text mode extracted from twig block text'); } } diff --git a/composer.json b/composer.json index aa75de7..5933f93 100644 --- a/composer.json +++ b/composer.json @@ -1,10 +1,10 @@ { "name": "donkeycode/mail-bundle", - "description": "An easy way to send mail with swiftmailer and twig", + "description": "An easy way to send mail with mailer and twig", "type": "symfony-bundle", "require": { "twig/twig": "*", - "swiftmailer/swiftmailer": "*" + "symfony/mailer": "*" }, "license": "MIT", "authors": [ @@ -17,5 +17,8 @@ "psr-4": { "DonkeyCode\\MailBundle\\": "" } + }, + "require-dev": { + "phpunit/phpunit": "^10.5" } } diff --git a/phpunit.xml b/phpunit.xml index 6b2853e..92601bb 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,15 +1,7 @@ - + stopOnFailure="false"> @@ -21,5 +13,4 @@ Tests -