8000 [Mailer] Introduces an InMemoryTransport to save messages in memory. · symfony/symfony@787a563 · GitHub
[go: up one dir, main page]

Skip to content

Commit 787a563

Browse files
committed
[Mailer] Introduces an InMemoryTransport to save messages in memory.
1 parent 6811aaa commit 787a563

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

src/Symfony/Component/Mailer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.4.0
55
-----
66

7+
* Added InMemoryTransport to save messages in memory. Use `in-memory://` as DSN.
78
* [BC BREAK] Transports depend on `Symfony\Contracts\EventDispatcher\EventDispatcherInterface`
89
instead of `Symfony\Component\EventDispatcher\EventDispatcherInterface`.
910

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Mailer\Tests\Transport;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Mailer\SentMessage;
9+
use Symfony\Component\Mailer\Transport\InMemoryTransport;
10+
use Symfony\Component\Mime\Email;
11+
use Symfony\Component\Mime\Message;
12+
13+
class InMemoryTransportTest extends TestCase
14+
{
15+
public function testItShouldSaveMessages()
16+
{
17+
$email = $this->createEmailMessage();
18+
19+
$inMemoryTransport = new InMemoryTransport();
20+
$inMemoryTransport->send($email);
21+
22+
/** @var SentMessage[] $inMemoryMessages */
23+
$inMemoryMessages = $inMemoryTransport->get();
24+
25+
$this->assertCount(1, $inMemoryMessages);
26+
$this->assertSame(
27+
$email->getSender()->toString(),
28+
$inMemoryMessages[0]->getEnvelope()->getSender()->toString()
29+
);
30+
}
31+
32+
public function testItShouldResetTransport()
33+
{
34+
$email = $this->createEmailMessage();
35+
36+
$inMemoryTransport = new InMemoryTransport();
37+
$inMemoryTransport->send($email);
38+
39+
$this->assertCount(1, $inMemoryTransport->get());
40+
41+
$inMemoryTransport->reset();
42+
43+
$this->assertCount(0, $inMemoryTransport->get());
44+
}
45+
46+
private function createEmailMessage(): Message
47+
{
48+
return (new Email())
49+
->sender('schaedlich.jan@gmail.com')
50+
->to('jan.schaedlich@sensiolabs.de')
51+
->subject('Important Notification')
52+
->text('Lorem ipsum...');
53+
}
54+
}

src/Symfony/Component/Mailer/Tests/TransportTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ public function testFromDsnNull()
4040
$this->assertSame($dispatcher, $p->getValue($transport));
4141
}
4242

43+
public function testFromDsnInMemory()
44+
{
45+
$dispatcher = $this->createMock(EventDispatcherInterface::class);
46+
$logger = $this->createMock(LoggerInterface::class);
47+
$transport = Transport::fromDsn('in-memory://', $dispatcher, null, $logger);
48+
49+
$this->assertInstanceOf(Transport\InMemoryTransport::class, $transport);
50+
51+
$property = new \ReflectionProperty(Transport\AbstractTransport::class, 'dispatcher');
52+
$property->setAccessible(true);
53+
54+
$this->assertSame($dispatcher, $property->getValue($transport));
55+
}
56+
4357
public function testFromDsnSendmail()
4458
{
4559
$dispatcher = $this->createMock(EventDispatcherInterface::class);

src/Symfony/Component/Mailer/Transport.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Mailer\Bridge\Sendgrid;
2121
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
2222
use Symfony\Component\Mailer\Exception\LogicException;
23+
use Symfony\Component\Mailer\Transport\InMemoryTransport;
2324
use Symfony\Component\Mailer\Transport\TransportInterface;
2425
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2526
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -53,6 +54,11 @@ public static function fromDsn(string $dsn, EventDispatcherInterface $dispatcher
5354
return new Transport\RoundRobinTransport($transports);
5455
}
5556

57+
// in-memory
58+
if (0 === strpos($dsn, 'in-memory://')) {
59+
return new Transport\InMemoryTransport($dispatcher, $logger);
60+
}
61+
5662
return self::createTransport($dsn, $dispatcher, $client, $logger);
5763
}
5864

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\Transport;
13+
14+
use Symfony\Component\Mailer\SentMessage;
15+
16+
/**
17+
* Stores messages in memory.
18+
*
19+
* @author Jan Schädlich <jan.schaedlich@gmail.com>
20+
*/
21+
final class InMemoryTransport extends AbstractTransport
22+
{
23+
/**
24+
* @var SentMessage[]
25+
*/
26+
private $messages = [];
27+
28+
protected function doSend(SentMessage $message): void
29+
{
30+
$this->messages[] = $message;
31+
}
32+
33+
public function get(): iterable
34+
{
35+
return $this->messages;
36+
}
37+
38+
/**
39+
* Resets the transport and removes all messages.
40+
*/
41+
public function reset(): void
42+
{
43+
$this->messages = [];
44+
}
45+
}

0 commit comments

Comments
 (0)
0