From 6b7a3cb55109a1cf7a1f835a02f42e32ec8906ed Mon Sep 17 00:00:00 2001 From: Alexandre Balmes Date: Tue, 21 Apr 2020 01:41:47 +0200 Subject: [PATCH] Add an adapter to support the Symfony Mailer component and deprecate the Swiftmailer integration Co-authored-by: Michael Babker --- .gitignore | 2 + composer.json | 3 + phpstan.neon | 1 + .../Compiler/RegisterRendererAdapterPass.php | 45 +++++++ .../Compiler/RegisterSenderAdapterPass.php | 64 ++++++++++ .../SyliusMailerExtension.php | 61 ++------- src/Bundle/Resources/config/services.xml | 4 +- src/Bundle/Sender/Adapter/DefaultAdapter.php | 4 +- .../Sender/Adapter/SwiftMailerAdapter.php | 9 ++ .../Sender/Adapter/SymfonyMailerAdapter.php | 66 ++++++++++ src/Bundle/SyliusMailerBundle.php | 10 ++ .../Sender/Adapter/DefaultAdapterSpec.php | 6 +- .../Adapter/SymfonyMailerAdapterSpec.php | 117 ++++++++++++++++++ src/Bundle/test/app/config/config.yml | 3 +- .../RegisterRendererAdapterPassTest.php | 55 ++++++++ .../RegisterSenderAdapterPassTest.php | 65 ++++++++++ .../SyliusMailerExtensionTest.php | 68 ++-------- 17 files changed, 461 insertions(+), 122 deletions(-) create mode 100644 src/Bundle/DependencyInjection/Compiler/RegisterRendererAdapterPass.php create mode 100644 src/Bundle/DependencyInjection/Compiler/RegisterSenderAdapterPass.php create mode 100644 src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php create mode 100644 src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php create mode 100644 src/Bundle/test/src/Tests/DependencyInjection/Compiler/RegisterRendererAdapterPassTest.php create mode 100644 src/Bundle/test/src/Tests/DependencyInjection/Compiler/RegisterSenderAdapterPassTest.php diff --git a/.gitignore b/.gitignore index fd1773e7..ca16686b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ /composer.phar /composer.lock + +/.phpunit.result.cache diff --git a/composer.json b/composer.json index 895a1655..4281a817 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "sylius-labs/polyfill-symfony-event-dispatcher": "^1.0", "symfony/config": "^4.4 || ^5.4", "symfony/dependency-injection": "^4.4 || ^5.4", + "symfony/deprecation-contracts": "^2.1 || ^3.0", "symfony/framework-bundle": "^4.4 || ^5.4", "symfony/http-kernel": "^4.4 || ^5.4", "twig/twig": "^2.12 || ^3.3", @@ -45,6 +46,8 @@ "phpunit/phpunit": "^9.4", "sylius-labs/coding-standard": "^4.0", "symfony/console": "^4.4 || ^5.4", + "symfony/event-dispatcher": "^4.4 || ^5.4", + "symfony/mailer": "^4.4 || ^5.4", "symfony/swiftmailer-bundle": "^3.1", "symfony/twig-bundle": "^4.4 || ^5.4", "vimeo/psalm": "^4.22", diff --git a/phpstan.neon b/phpstan.neon index 9e2b0b46..14d21160 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -23,4 +23,5 @@ parameters: - '/Parameter \#1 \$event of method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) expects object, string given\./' - '/Method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) invoked with 2 parameters, 1 required\./' + - '/Cannot call method has\(\) on object\|null/' - '/Property Sylius\\Component\\Mailer\\Model\\Email\:\:\$id is never written\, only read\./' diff --git a/src/Bundle/DependencyInjection/Compiler/RegisterRendererAdapterPass.php b/src/Bundle/DependencyInjection/Compiler/RegisterRendererAdapterPass.php new file mode 100644 index 00000000..1e68bbb6 --- /dev/null +++ b/src/Bundle/DependencyInjection/Compiler/RegisterRendererAdapterPass.php @@ -0,0 +1,45 @@ +hasAlias('sylius.email_renderer.adapter')) { + return; + } + + if ($container->has('twig')) { + $twigAdapter = new ChildDefinition('sylius.email_renderer.adapter.abstract'); + $twigAdapter->setClass(EmailTwigAdapter::class); + $twigAdapter->setArguments([new Reference('twig'), new Reference('event_dispatcher', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]); + $twigAdapter->setPublic(true); + + $container->setDefinition('sylius.email_renderer.adapter.twig', $twigAdapter); + $container->setAlias('sylius.email_renderer.adapter', 'sylius.email_renderer.adapter.twig'); + + return; + } + + $container->setAlias('sylius.email_renderer.adapter', 'sylius.email_renderer.adapter.default'); + } +} diff --git a/src/Bundle/DependencyInjection/Compiler/RegisterSenderAdapterPass.php b/src/Bundle/DependencyInjection/Compiler/RegisterSenderAdapterPass.php new file mode 100644 index 00000000..e1a93973 --- /dev/null +++ b/src/Bundle/DependencyInjection/Compiler/RegisterSenderAdapterPass.php @@ -0,0 +1,64 @@ +hasAlias('sylius.email_sender.adapter')) { + return; + } + + if ($container->has('swiftmailer.mailer.default')) { + $swiftmailerAdapter = new ChildDefinition('sylius.email_sender.adapter.abstract'); + /** @psalm-suppress DeprecatedClass */ + $swiftmailerAdapter->setClass(SwiftMailerAdapter::class); + $swiftmailerAdapter->setArguments([new Reference('swiftmailer.mailer.default'), new Reference('event_dispatcher', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]); + $swiftmailerAdapter->setPublic(true); + $swiftmailerAdapter->setDeprecated( + 'sylius/mailer-bundle', + '1.8', + 'The "%service_id%" service is deprecated, use the Symfony Mailer integration instead.' + ); + + $container->setDefinition('sylius.email_sender.adapter.swiftmailer', $swiftmailerAdapter); + $container->setAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.swiftmailer'); + + return; + } + + if ($container->has('mailer.mailer')) { + $symfonyMailerAdapter = new ChildDefinition('sylius.email_sender.adapter.abstract'); + $symfonyMailerAdapter->setClass(SymfonyMailerAdapter::class); + $symfonyMailerAdapter->setArguments([new Reference('mailer.mailer')]); + $symfonyMailerAdapter->setPublic(true); + + $container->setDefinition('sylius.email_sender.adapter.symfony_mailer', $symfonyMailerAdapter); + $container->setAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.symfony_mailer'); + + return; + } + + $container->setAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.default'); + } +} diff --git a/src/Bundle/DependencyInjection/SyliusMailerExtension.php b/src/Bundle/DependencyInjection/SyliusMailerExtension.php index c55b5e67..102fe4c6 100644 --- a/src/Bundle/DependencyInjection/SyliusMailerExtension.php +++ b/src/Bundle/DependencyInjection/SyliusMailerExtension.php @@ -13,14 +13,10 @@ namespace Sylius\Bundle\MailerBundle\DependencyInjection; -use Sylius\Bundle\MailerBundle\Renderer\Adapter\EmailTwigAdapter; -use Sylius\Bundle\MailerBundle\Sender\Adapter\SwiftMailerAdapter; use Symfony\Component\Config\FileLocator; -use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; -use Symfony\Component\DependencyInjection\Reference; final class SyliusMailerExtension extends Extension { @@ -40,8 +36,13 @@ public function load(array $configs, ContainerBuilder $container): void $loader->load($configFile); } - $this->configureSenderAdapter($container, $config); - $this->configureRendererAdapter($container, $config); + if (isset($config['sender_adapter'])) { + $container->setAlias('sylius.email_sender.adapter', $config['sender_adapter']); + } + + if (isset($config['renderer_adapter'])) { + $container->setAlias('sylius.email_renderer.adapter', $config['renderer_adapter']); + } $container->setParameter('sylius.mailer.sender_name', $config['sender']['name']); $container->setParameter('sylius.mailer.sender_address', $config['sender']['address']); @@ -51,52 +52,4 @@ public function load(array $configs, ContainerBuilder $container): void $container->setParameter('sylius.mailer.emails', $config['emails']); $container->setParameter('sylius.mailer.templates', $templates); } - - /** - * {@inheritdoc} - */ - public function getConfiguration(array $config, ContainerBuilder $container): Configuration - { - $configuration = new Configuration(); - - $container->addObjectResource($configuration); - - return $configuration; - } - - private function configureSenderAdapter(ContainerBuilder $container, array $config): void - { - $bundles = (array) $container->getParameter('kernel.bundles'); - - $defaultSenderAdapter = 'sylius.email_sender.adapter.default'; - if (array_key_exists('SwiftmailerBundle', $bundles)) { - $swiftmailerAdapter = new ChildDefinition('sylius.email_sender.adapter.abstract'); - $swiftmailerAdapter->setClass(SwiftMailerAdapter::class); - $swiftmailerAdapter->setArguments([new Reference('mailer'), new Reference('event_dispatcher')]); - $swiftmailerAdapter->setPublic(true); - - $container->setDefinition('sylius.email_sender.adapter.swiftmailer', $swiftmailerAdapter); - $defaultSenderAdapter = 'sylius.email_sender.adapter.swiftmailer'; - } - - $container->setAlias('sylius.email_sender.adapter', $config['sender_adapter'] ?? $defaultSenderAdapter); - } - - private function configureRendererAdapter(ContainerBuilder $container, array $config): void - { - $bundles = (array) $container->getParameter('kernel.bundles'); - - $defaultRendererAdapter = 'sylius.email_renderer.adapter.default'; - if (array_key_exists('TwigBundle', $bundles)) { - $twigAdapter = new ChildDefinition('sylius.email_renderer.adapter.abstract'); - $twigAdapter->setClass(EmailTwigAdapter::class); - $twigAdapter->setArguments([new Reference('twig'), new Reference('event_dispatcher')]); - $twigAdapter->setPublic(true); - - $container->setDefinition('sylius.email_renderer.adapter.twig', $twigAdapter); - $defaultRendererAdapter = 'sylius.email_renderer.adapter.twig'; - } - - $container->setAlias('sylius.email_renderer.adapter', $config['renderer_adapter'] ?? $defaultRendererAdapter); - } } diff --git a/src/Bundle/Resources/config/services.xml b/src/Bundle/Resources/config/services.xml index d4eed9ef..ef8a0d8c 100644 --- a/src/Bundle/Resources/config/services.xml +++ b/src/Bundle/Resources/config/services.xml @@ -40,7 +40,7 @@ - + - + mailer = $mailer; $this->dispatcher = $dispatcher; } diff --git a/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php b/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php new file mode 100644 index 00000000..8d411ac7 --- /dev/null +++ b/src/Bundle/Sender/Adapter/SymfonyMailerAdapter.php @@ -0,0 +1,66 @@ +mailer = $mailer; + } + + /** + * {@inheritdoc} + */ + public function send( + array $recipients, + string $senderAddress, + string $senderName, + RenderedEmail $renderedEmail, + EmailInterface $email, + array $data, + array $attachments = [], + array $replyTo = [] + ): void { + $message = (new Email()) + ->subject($renderedEmail->getSubject()) + ->from(new Address($senderAddress, $senderName)) + ->to(...$recipients) + ->replyTo(...$replyTo) + ->html($renderedEmail->getBody()); + + foreach ($attachments as $attachment) { + $message->attachFromPath($attachment); + } + + $emailSendEvent = new EmailSendEvent($message, $email, $data, $recipients, $replyTo); + + $this->dispatcher?->dispatch($emailSendEvent, SyliusMailerEvents::EMAIL_PRE_SEND); + + $this->mailer->send($message); + + $this->dispatcher?->dispatch($emailSendEvent, SyliusMailerEvents::EMAIL_POST_SEND); + } +} diff --git a/src/Bundle/SyliusMailerBundle.php b/src/Bundle/SyliusMailerBundle.php index d126bc84..c6b1e6a8 100644 --- a/src/Bundle/SyliusMailerBundle.php +++ b/src/Bundle/SyliusMailerBundle.php @@ -13,8 +13,18 @@ namespace Sylius\Bundle\MailerBundle; +use Sylius\Bundle\MailerBundle\DependencyInjection\Compiler\RegisterRendererAdapterPass; +use Sylius\Bundle\MailerBundle\DependencyInjection\Compiler\RegisterSenderAdapterPass; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; final class SyliusMailerBundle extends Bundle { + public function build(ContainerBuilder $container): void + { + parent::build($container); + + $container->addCompilerPass(new RegisterRendererAdapterPass()); + $container->addCompilerPass(new RegisterSenderAdapterPass()); + } } diff --git a/src/Bundle/spec/Sender/Adapter/DefaultAdapterSpec.php b/src/Bundle/spec/Sender/Adapter/DefaultAdapterSpec.php index c6f35cde..75464e3e 100644 --- a/src/Bundle/spec/Sender/Adapter/DefaultAdapterSpec.php +++ b/src/Bundle/spec/Sender/Adapter/DefaultAdapterSpec.php @@ -14,7 +14,7 @@ namespace spec\Sylius\Bundle\MailerBundle\Sender\Adapter; use PhpSpec\ObjectBehavior; -use Sylius\Bundle\MailerBundle\Sender\Adapter\SwiftMailerAdapter; +use Sylius\Bundle\MailerBundle\Sender\Adapter\SymfonyMailerAdapter; use Sylius\Component\Mailer\Model\EmailInterface; use Sylius\Component\Mailer\Renderer\RenderedEmail; use Sylius\Component\Mailer\Sender\Adapter\AbstractAdapter; @@ -32,8 +32,8 @@ function it_throws_an_exception_about_not_configured_email_sender_adapter( ): void { $this ->shouldThrow(new \RuntimeException(sprintf( - 'You need to configure an adapter to send the email. Take a look at %s (requires "symfony/swiftmailer-bundle" library).', - SwiftMailerAdapter::class + 'You need to configure an adapter to send the email. Take a look at %s (requires "symfony/mailer" library).', + SymfonyMailerAdapter::class ))) ->during('send', [['pawel@sylius.com'], 'arnaud@sylius.com', 'arnaud', $renderedEmail, $email, []]) ; diff --git a/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php b/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php new file mode 100644 index 00000000..890a415a --- /dev/null +++ b/src/Bundle/spec/Sender/Adapter/SymfonyMailerAdapterSpec.php @@ -0,0 +1,117 @@ +beConstructedWith($mailer); + } + + function it_is_an_adapter(): void + { + $this->shouldHaveType(AbstractAdapter::class); + } + + function it_sends_an_email_with_events( + MailerInterface $mailer, + EmailInterface $email, + EventDispatcherInterface $dispatcher, + RenderedEmail $renderedEmail + ): void { + $this->setEventDispatcher($dispatcher); + + $renderedEmail->getSubject()->willReturn('subject'); + $renderedEmail->getBody()->willReturn('body'); + + $dispatcher + ->dispatch(Argument::type(EmailSendEvent::class), SyliusMailerEvents::EMAIL_PRE_SEND) + ->shouldBeCalled() + ; + + $mailer->send(Argument::type(RawMessage::class))->shouldBeCalled(); + + $dispatcher + ->dispatch(Argument::type(EmailSendEvent::class), SyliusMailerEvents::EMAIL_POST_SEND) + ->shouldBeCalled() + ; + + $this->send( + ['pawel@sylius.com'], + 'arnaud@sylius.com', + 'arnaud', + $renderedEmail, + $email, + [] + ); + } + + function it_sends_an_email_with_attachments( + MailerInterface $mailer, + EmailInterface $email, + RenderedEmail $renderedEmail + ): void { + $renderedEmail->getSubject()->willReturn('subject'); + $renderedEmail->getBody()->willReturn('body'); + + $mailer->send(Argument::type(RawMessage::class))->shouldBeCalled(); + + $this->send( + ['pawel@sylius.com'], + 'arnaud@sylius.com', + 'arnaud', + $renderedEmail, + $email, + ['/path/to/file1.txt', '/path/to/file2.txt'] + ); + } + + function it_does_not_handle_exceptions_from_the_mailer( + MailerInterface $mailer, + EmailInterface $email, + RenderedEmail $renderedEmail + ): void { + $exception = new TransportException('Testing'); + + $renderedEmail->getSubject()->willReturn('subject'); + $renderedEmail->getBody()->willReturn('body'); + + $mailer->send(Argument::type(RawMessage::class))->willThrow($exception); + + $this->shouldThrow($exception)->during( + 'send', + [ + ['pawel@sylius.com'], + 'arnaud@sylius.com', + 'arnaud', + $renderedEmail, + $email, + [], + ] + ); + } +} diff --git a/src/Bundle/test/app/config/config.yml b/src/Bundle/test/app/config/config.yml index 7b2fce2b..9c7573e7 100644 --- a/src/Bundle/test/app/config/config.yml +++ b/src/Bundle/test/app/config/config.yml @@ -7,7 +7,8 @@ framework: default_locale: "%locale%" http_method_override: true test: ~ - + mailer: + dsn: 'smtp://localhost' twig: debug: "%kernel.debug%" strict_variables: "%kernel.debug%" diff --git a/src/Bundle/test/src/Tests/DependencyInjection/Compiler/RegisterRendererAdapterPassTest.php b/src/Bundle/test/src/Tests/DependencyInjection/Compiler/RegisterRendererAdapterPassTest.php new file mode 100644 index 00000000..667dc67e --- /dev/null +++ b/src/Bundle/test/src/Tests/DependencyInjection/Compiler/RegisterRendererAdapterPassTest.php @@ -0,0 +1,55 @@ +container->setAlias('sylius.email_renderer.adapter', 'sylius.email_renderer.adapter.custom'); + + $this->compile(); + + $this->assertContainerBuilderHasAlias('sylius.email_renderer.adapter', 'sylius.email_renderer.adapter.custom'); + } + + /** @test */ + public function it_registers_the_twig_adapter_when_its_dependencies_are_available(): void + { + $this->registerService('twig', Environment::class); + + $this->compile(); + + $this->assertContainerBuilderHasAlias('sylius.email_renderer.adapter', 'sylius.email_renderer.adapter.twig'); + } + + /** @test */ + public function it_registers_the_default_adapter_when_twig_is_not_available(): void + { + $this->compile(); + + $this->assertContainerBuilderHasAlias('sylius.email_renderer.adapter', 'sylius.email_renderer.adapter.default'); + } + + protected function registerCompilerPass(ContainerBuilder $container): void + { + $container->addCompilerPass(new RegisterRendererAdapterPass()); + } +} diff --git a/src/Bundle/test/src/Tests/DependencyInjection/Compiler/RegisterSenderAdapterPassTest.php b/src/Bundle/test/src/Tests/DependencyInjection/Compiler/RegisterSenderAdapterPassTest.php new file mode 100644 index 00000000..d0e5f066 --- /dev/null +++ b/src/Bundle/test/src/Tests/DependencyInjection/Compiler/RegisterSenderAdapterPassTest.php @@ -0,0 +1,65 @@ +container->setAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.custom'); + + $this->compile(); + + $this->assertContainerBuilderHasAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.custom'); + } + + /** @test */ + public function it_registers_the_swiftmailer_adapter_when_its_dependencies_are_available(): void + { + $this->registerService('swiftmailer.mailer.default', \Swift_Mailer::class); + + $this->compile(); + + $this->assertContainerBuilderHasAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.swiftmailer'); + } + + /** @test */ + public function it_registers_the_symfony_mailer_adapter_when_its_dependencies_are_available(): void + { + $this->registerService('mailer.mailer', Mailer::class); + + $this->compile(); + + $this->assertContainerBuilderHasAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.symfony_mailer'); + } + + /** @test */ + public function it_registers_the_default_adapter_when_no_other_adapters_are_available(): void + { + $this->compile(); + + $this->assertContainerBuilderHasAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.default'); + } + + protected function registerCompilerPass(ContainerBuilder $container): void + { + $container->addCompilerPass(new RegisterSenderAdapterPass()); + } +} diff --git a/src/Bundle/test/src/Tests/DependencyInjection/SyliusMailerExtensionTest.php b/src/Bundle/test/src/Tests/DependencyInjection/SyliusMailerExtensionTest.php index 88e9e01f..3f4d9da1 100644 --- a/src/Bundle/test/src/Tests/DependencyInjection/SyliusMailerExtensionTest.php +++ b/src/Bundle/test/src/Tests/DependencyInjection/SyliusMailerExtensionTest.php @@ -15,92 +15,40 @@ use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; use Sylius\Bundle\MailerBundle\DependencyInjection\SyliusMailerExtension; -use Sylius\Bundle\MailerBundle\Renderer\Adapter\EmailDefaultAdapter; -use Sylius\Bundle\MailerBundle\Renderer\Adapter\EmailTwigAdapter; -use Sylius\Bundle\MailerBundle\Sender\Adapter\DefaultAdapter; -use Sylius\Bundle\MailerBundle\Sender\Adapter\SwiftMailerAdapter; -use Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle; -use Symfony\Bundle\TwigBundle\TwigBundle; -use Symfony\Component\DependencyInjection\Definition; -use Symfony\Component\EventDispatcher\EventDispatcher; -use Twig\Environment; final class SyliusMailerExtensionTest extends AbstractExtensionTestCase { /** @test */ - public function it_configures_mailer_adapters_and_sender_with_default_data(): void + public function it_configures_the_bundle_with_the_default_configuration(): void { - $this->mockService('event_dispatcher', EventDispatcher::class); - - $this->container->setParameter('kernel.bundles', []); - $this->load(); - $this->compile(); $this->assertContainerBuilderHasParameter('sylius.mailer.sender_name', 'Example.com Store'); $this->assertContainerBuilderHasParameter('sylius.mailer.sender_address', 'no-reply@example.com'); - - $this->assertContainerBuilderHasService('sylius.email_sender.adapter.default', DefaultAdapter::class); - $this->assertContainerBuilderHasAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.default'); - - $this->assertContainerBuilderHasService('sylius.email_renderer.adapter.default', EmailDefaultAdapter::class); - $this->assertContainerBuilderHasAlias('sylius.email_renderer.adapter', 'sylius.email_renderer.adapter.default'); + $this->assertContainerBuilderHasParameter('sylius.mailer.emails'); + $this->assertContainerBuilderHasParameter('sylius.mailer.templates'); } /** @test */ - public function it_configures_mailer_adapters_and_sender_with_custom_sender_data(): void + public function it_configures_the_bundle_with_custom_sender_data(): void { - $this->mockService('event_dispatcher', EventDispatcher::class); - $this->container->setParameter('kernel.bundles', []); - $this->load(['sender' => ['name' => 'John Doe', 'address' => 'john@doe.com']]); - $this->compile(); $this->assertContainerBuilderHasParameter('sylius.mailer.sender_name', 'John Doe'); $this->assertContainerBuilderHasParameter('sylius.mailer.sender_address', 'john@doe.com'); - - $this->assertContainerBuilderHasService('sylius.email_sender.adapter.default', DefaultAdapter::class); - $this->assertContainerBuilderHasAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.default'); - - $this->assertContainerBuilderHasService('sylius.email_renderer.adapter.default', EmailDefaultAdapter::class); - $this->assertContainerBuilderHasAlias('sylius.email_renderer.adapter', 'sylius.email_renderer.adapter.default'); } /** @test */ - public function it_configures_twig_and_swiftmailer_adapters_if_they_are_available(): void + public function it_configures_the_bundle_with_custom_adapter_services(): void { - $this->mockService('event_dispatcher', EventDispatcher::class); - $this->mockService('mailer', \Swift_Mailer::class); - $this->mockService('twig', Environment::class); + $this->load(['sender_adapter' => 'sylius.email_sender.adapter.custom', 'renderer_adapter' => 'sylius.email_renderer.adapter.custom']); - $this->container->setParameter( - 'kernel.bundles', - ['SwiftmailerBundle' => SwiftmailerBundle::class, 'TwigBundle' => TwigBundle::class] - ); - - $this->load(); - $this->compile(); - - $this->assertContainerBuilderHasParameter('sylius.mailer.sender_name', 'Example.com Store'); - $this->assertContainerBuilderHasParameter('sylius.mailer.sender_address', 'no-reply@example.com'); - - $this->assertContainerBuilderHasService('sylius.email_sender.adapter.swiftmailer', SwiftMailerAdapter::class); - $this->assertContainerBuilderHasAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.swiftmailer'); - - $this->assertContainerBuilderHasService('sylius.email_renderer.adapter.twig', EmailTwigAdapter::class); - $this->assertContainerBuilderHasAlias('sylius.email_renderer.adapter', 'sylius.email_renderer.adapter.twig'); + $this->assertContainerBuilderHasAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.custom'); + $this->assertContainerBuilderHasAlias('sylius.email_renderer.adapter', 'sylius.email_renderer.adapter.custom'); } protected function getContainerExtensions(): array { return [new SyliusMailerExtension()]; } - - private function mockService(string $id, string $class): void - { - $this->container->setDefinition( - $id, - (new Definition())->setClass(self::getMockClass($class))->setAutoconfigured(true) - ); - } }