Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 839af20

Browse filesBrowse files
committed
[Mailer] Introduces an InMemoryTransport to save messages in memory.
1 parent 6811aaa commit 839af20
Copy full SHA for 839af20

File tree

5 files changed

+119
-0
lines changed
Filter options

5 files changed

+119
-0
lines changed

‎src/Symfony/Component/Mailer/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/CHANGELOG.md
+1Lines 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

+54Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Tests/TransportTest.php
+14Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Transport.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public static function fromDsn(string $dsn, EventDispatcherInterface $dispatcher
5353
return new Transport\RoundRobinTransport($transports);
5454
}
5555

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

+45Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.