From 1d5c7abbe818c13fc19c67899216f536623fe86b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4dlich?= Date: Sat, 6 Jul 2019 15:20:21 +0200 Subject: [PATCH] [Mailer] Introduces an InMemoryTransport to save messages in memory. --- src/Symfony/Component/Mailer/CHANGELOG.md | 1 + .../Tests/Transport/InMemoryTransportTest.php | 54 +++++++++++++++++++ .../Component/Mailer/Tests/TransportTest.php | 14 +++++ src/Symfony/Component/Mailer/Transport.php | 5 ++ .../Mailer/Transport/InMemoryTransport.php | 45 ++++++++++++++++ 5 files changed, 119 insertions(+) create mode 100644 src/Symfony/Component/Mailer/Tests/Transport/InMemoryTransportTest.php create mode 100644 src/Symfony/Component/Mailer/Transport/InMemoryTransport.php diff --git a/src/Symfony/Component/Mailer/CHANGELOG.md b/src/Symfony/Component/Mailer/CHANGELOG.md index 5b2c5c528fca5..4ef9f476c58ee 100644 --- a/src/Symfony/Component/Mailer/CHANGELOG.md +++ b/src/Symfony/Component/Mailer/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 4.4.0 ----- + * Added InMemoryTransport to save messages in memory. Use `in-memory://` as DSN. * [BC BREAK] Transports depend on `Symfony\Contracts\EventDispatcher\EventDispatcherInterface` instead of `Symfony\Component\EventDispatcher\EventDispatcherInterface`. diff --git a/src/Symfony/Component/Mailer/Tests/Transport/InMemoryTransportTest.php b/src/Symfony/Component/Mailer/Tests/Transport/InMemoryTransportTest.php new file mode 100644 index 0000000000000..603095e362985 --- /dev/null +++ b/src/Symfony/Component/Mailer/Tests/Transport/InMemoryTransportTest.php @@ -0,0 +1,54 @@ +createEmailMessage(); + + $inMemoryTransport = new InMemoryTransport(); + $inMemoryTransport->send($email); + + /** @var SentMessage[] $inMemoryMessages */ + $inMemoryMessages = $inMemoryTransport->get(); + + $this->assertCount(1, $inMemoryMessages); + $this->assertSame( + $email->getSender()->toString(), + $inMemoryMessages[0]->getEnvelope()->getSender()->toString() + ); + } + + public function testItShouldResetTransport() + { + $email = $this->createEmailMessage(); + + $inMemoryTransport = new InMemoryTransport(); + $inMemoryTransport->send($email); + + $this->assertCount(1, $inMemoryTransport->get()); + + $inMemoryTransport->reset(); + + $this->assertCount(0, $inMemoryTransport->get()); + } + + private function createEmailMessage(): Message + { + return (new Email()) + ->sender('schaedlich.jan@gmail.com') + ->to('jan.schaedlich@sensiolabs.de') + ->subject('Important Notification') + ->text('Lorem ipsum...'); + } +} diff --git a/src/Symfony/Component/Mailer/Tests/TransportTest.php b/src/Symfony/Component/Mailer/Tests/TransportTest.php index 8c7cd99d98960..8dab8ecf17bc3 100644 --- a/src/Symfony/Component/Mailer/Tests/TransportTest.php +++ b/src/Symfony/Component/Mailer/Tests/TransportTest.php @@ -40,6 +40,20 @@ public function testFromDsnNull() $this->assertSame($dispatcher, $p->getValue($transport)); } + public function testFromDsnInMemory() + { + $dispatcher = $this->createMock(EventDispatcherInterface::class); + $logger = $this->createMock(LoggerInterface::class); + $transport = Transport::fromDsn('in-memory://', $dispatcher, null, $logger); + + $this->assertInstanceOf(Transport\InMemoryTransport::class, $transport); + + $property = new \ReflectionProperty(Transport\AbstractTransport::class, 'dispatcher'); + $property->setAccessible(true); + + $this->assertSame($dispatcher, $property->getValue($transport)); + } + public function testFromDsnSendmail() { $dispatcher = $this->createMock(EventDispatcherInterface::class); diff --git a/src/Symfony/Component/Mailer/Transport.php b/src/Symfony/Component/Mailer/Transport.php index 42f545f9fd6f4..a2f2eea77b897 100644 --- a/src/Symfony/Component/Mailer/Transport.php +++ b/src/Symfony/Component/Mailer/Transport.php @@ -53,6 +53,11 @@ public static function fromDsn(string $dsn, EventDispatcherInterface $dispatcher return new Transport\RoundRobinTransport($transports); } + // in-memory + if (0 === strpos($dsn, 'in-memory://')) { + return new Transport\InMemoryTransport($dispatcher, $logger); + } + return self::createTransport($dsn, $dispatcher, $client, $logger); } diff --git a/src/Symfony/Component/Mailer/Transport/InMemoryTransport.php b/src/Symfony/Component/Mailer/Transport/InMemoryTransport.php new file mode 100644 index 0000000000000..a4a4c70b42ac6 --- /dev/null +++ b/src/Symfony/Component/Mailer/Transport/InMemoryTransport.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Mailer\Transport; + +use Symfony\Component\Mailer\SentMessage; + +/** + * Stores messages in memory. + * + * @author Jan Schädlich + */ +final class InMemoryTransport extends AbstractTransport +{ + /** + * @var SentMessage[] + */ + private $messages = []; + + protected function doSend(SentMessage $message): void + { + $this->messages[] = $message; + } + + public function get(): iterable + { + return $this->messages; + } + + /** + * Resets the transport and removes all messages. + */ + public function reset(): void + { + $this->messages = []; + } +}