Skip to content

[Mailer] Introduces an InMemoryTransport to save messages in memory. #32409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Mailer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\Mailer\Tests\Transport;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\InMemoryTransport;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Message;

class InMemoryTransportTest extends TestCase
{
public function testItShouldSaveMessages()
{
$email = $this->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...');
}
}
14 changes: 14 additions & 0 deletions src/Symfony/Component/Mailer/Tests/TransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
45 changes: 45 additions & 0 deletions src/Symfony/Component/Mailer/Transport/InMemoryTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <schaedlich.jan@gmail.com>
*/
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 = [];
}
}