Skip to content

Commit 12ff52d

Browse files
committed
Add a mailer:test command
1 parent 7f7d839 commit 12ff52d

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
102102
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
103103
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueTransportFactory;
104+
use Symfony\Component\Mailer\Command\MailerTestCommand;
104105
use Symfony\Component\Mailer\Mailer;
105106
use Symfony\Component\Mercure\HubRegistry;
106107
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
@@ -386,6 +387,10 @@ public function load(array $configs, ContainerBuilder $container)
386387
$this->registerMailerConfiguration($config['mailer'], $container, $loader);
387388
}
388389

390+
if (!$this->mailerConfigEnabled || !class_exists(MailerTestCommand::class)) {
391+
$container->removeDefinition('console.command.mailer_test');
392+
}
393+
389394
$propertyInfoEnabled = $this->isConfigEnabled($container, $config['property_info']);
390395
$this->registerHttpCacheConfiguration($config['http_cache'], $container, $config['http_method_override']);
391396
$this->registerEsiConfiguration($config['esi'], $container, $loader);

src/Symfony/Bundle/FrameworkBundle/Resources/config/mailer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

14+
use Symfony\Component\Mailer\Command\MailerTestCommand;
1415
use Symfony\Component\Mailer\EventListener\EnvelopeListener;
1516
use Symfony\Component\Mailer\EventListener\MessageListener;
1617
use Symfony\Component\Mailer\EventListener\MessageLoggerListener;
@@ -72,5 +73,11 @@
7273
->set('mailer.message_logger_listener', MessageLoggerListener::class)
7374
->tag('kernel.event_subscriber')
7475
->tag('kernel.reset', ['method' => 'reset'])
76+
77+
->set('console.command.mailer_test', MailerTestCommand::class)
78+
->args([
79+
service('mailer'),
80+
])
81+
->tag('console.command')
7582
;
7683
};

src/Symfony/Component/Mailer/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Add a `mailer:test` command
8+
49
6.1
510
---
611

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Tests\Command;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Tester\CommandTester;
16+
use Symfony\Component\Mailer\Command\MailerTestCommand;
17+
use Symfony\Component\Mailer\MailerInterface;
18+
use Symfony\Component\Mime\Email;
19+
20+
class MailerTestCommandTest extends TestCase
21+
{
22+
public function testSendsEmail()
23+
{
24+
$from = 'from@example.com';
25+
$to = 'to@example.com';
26+
$subject = 'Foobar';
27+
$body = 'Lorem ipsum dolor sit amet.';
28+
29+
$mailer = $this->createMock(MailerInterface::class);
30+
$mailer
31+
->expects($this->once())
32+
->method('send')
33+
->with(self::callback(static function (Email $message) use ($from, $to, $subject, $body): bool {
34+
return
35+
$message->getFrom()[0]->getAddress() === $from &&
36+
$message->getTo()[0]->getAddress() === $to &&
37+
$message->getSubject() === $subject &&
38+
$message->getTextBody() === $body
39+
;
40+
}))
41+
;
42+
43+
$tester = new CommandTester(new MailerTestCommand($mailer));
44+
$tester->execute([
45+
'to' => $to,
46+
'--from' => $from,
47+
'--subject' => $subject,
48+
'--body' => $body,
49+
]);
50+
}
51+
52+
public function testUsesCustomTransport()
53+
{
54+
$transport = 'foobar';
55+
56+
$mailer = $this->createMock(MailerInterface::class);
57+
$mailer
58+
->expects($this->once())
59+
->method('send')
60+
->with(self::callback(static function (Email $message) use ($transport): bool {
61+
return $message->getHeaders()->getHeaderBody('X-Transport') === $transport;
62+
}))
63+
;
64+
65+
$tester = new CommandTester(new MailerTestCommand($mailer));
66+
$tester->execute([
67+
'to' => 'to@example.com',
68+
'--transport' => $transport,
69+
]);
70+
}
71+
}

0 commit comments

Comments
 (0)