8000 feature #29097 [Messenger] Add a "in-memory://" transport (GaryPEGEOT… · symfony/symfony@ec18af4 · GitHub
[go: up one dir, main page]

Skip to content

Commit ec18af4

Browse files
committed
feature #29097 [Messenger] Add a "in-memory://" transport (GaryPEGEOT, sroze)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Messenger] Add a "in-memory://" transport | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29040 | License | MIT | Doc PR | Todo Add a new `InMemoryTransport` for test purpose, usable by starting your DSN by `in-memory://` Commits ------- 8f8c82e Make the in-memory transport resettable fe75920 Add a "null://" transport
2 parents a45235e + 8f8c82e commit ec18af4

File tree

6 files changed

+289
-0
lines changed

6 files changed

+289
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ CHANGELOG
2929
* Added support for PHP files with translations in translation commands.
3030
* Added support for boolean container parameters within routes.
3131
* Added the `messenger:setup-transports` command to setup messenger transports
32+
* Added a `InMemoryTransport` to Messenger. Use it with a DSN starting with `in-memory://`.
3233

3334
4.2.0
3435
-----

src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
<tag name="messenger.transport_factory" />
7171
</service>
7272

73+
<service id="messenger.transport.in_memory.factory" class="Symfony\Component\Messenger\Transport\InMemoryTransportFactory">
74+
<tag name="messenger.transport_factory" />
75+
<tag name="kernel.reset" method="reset" />
76+
</service>
77+
7378
<!-- retry -->
7479
<service id="messenger.retry_strategy_locator">
7580
<tag name="container.service_locator" />
Lines changed: 74 additions & 0 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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\Messenger\Tests\Transport;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Messenger\Envelope;
16+
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
17+
use Symfony\Component\Messenger\Transport\InMemoryTransport;
18+
use Symfony\Component\Messenger\Transport\InMemoryTransportFactory;
19+
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
20+
21+
/**
22+
* @internal
23+
*
24+
* @author Gary PEGEOT <garypegeot@gmail.com>
25+
*/
26+
class InMemoryTransportFactoryTest extends TestCase
27+
{
28+
/**
29+
* @var InMemoryTransportFactory
30+
*/
31+
private $factory;
32+
33+
protected function setUp()
34+
{
35+
$this->factory = new InMemoryTransportFactory();
36+
}
37+
38+
/**
39+
* @param string $dsn
40+
* @param bool $expected
41+
*
42+
* @dataProvider provideDSN
43+
*/
44+
public function testSupports(string $dsn, bool $expected = true)
45+
{
46+
$this->assertSame($expected, $this->factory->supports($dsn, []), 'InMemoryTransportFactory::supports returned unexpected result.');
47+
}
48+
49+
public function testCreateTransport()
50+
{
51+
/** @var SerializerInterface $serializer */
52+
$serializer = $this->createMock(SerializerInterface::class);
53+
54+
$this->assertInstanceOf(InMemoryTransport::class, $this->factory->createTransport('in-memory://', [], $serializer));
55+
}
56+
57+
public function testResetCreatedTransports()
58+
{
59+
$transport = $this->factory->createTransport('in-memory://', [], $this->createMock(SerializerInterface::class));
60+
$transport->send(Envelope::wrap(new DummyMessage('Hello.')));
61+
62+
$this->assertCount(1, $transport->get());
63+
$this->factory->reset();
64+
$this->assertCount(0, $transport->get());
65+
}
66+
67+
public function provideDSN(): array
68+
{
69+
return [
70+
'Supported' => ['in-memory://foo'],
71+
'Unsupported' => ['amqp://bar', false],
72+
];
73+
}
74+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\Messenger\Tests\Transport;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Messenger\Envelope;
16+
use Symfony\Component\Messenger\Transport\InMemoryTransport;
17+
18+
/**
19+
* @internal
20+
*
21+
* @author Gary PEGEOT <garypegeot@gmail.com>
22+
*/
23+
class InMemoryTransportTest extends TestCase
24+
{
25+
/**
26+
* @var InMemoryTransport
27+
*/
28+
private $transport;
29+
30+
protected function setUp()
31+
{
32+
$this->transport = new InMemoryTransport();
33+
}
34+
35+
public function testSend()
36+
{
37+
$envelope = new Envelope(new \stdClass());
38+
$this->transport->send($envelope);
39+
$this->assertSame([$envelope], $this->transport->get());
40+
}
41+
42+
public function testAck()
43+
{
44+
$envelope = new Envelope(new \stdClass());
45+
$this->transport->ack($envelope);
46+
$this->assertSame([$envelope], $this->transport->getAcknowledged());
47+
}
48+
49+
public function testReject()
50+
{
51+
$envelope = new Envelope(new \stdClass());
52+
$this->transport->reject($envelope);
53+
$this->assertSame([$envelope], $this->transport->getRejected());
54+
}
55+
56+
public function testReset()
57+
{
58+
$envelope = new Envelope(new \stdClass());
59+
$this->transport->send($envelope);
60+
$this->transport->ack($envelope);
61+
$this->transport->reject($envelope);
62+
63+
$this->transport->reset();
64+
65+
$this->assertEmpty($this->transport->get(), 'Should be empty after reset');
66+
$this->assertEmpty($this->transport->getAcknowledged(), 'Should be empty after reset');
67+
$this->assertEmpty($this->transport->getRejected(), 'Should be empty after reset');
68+
}
69+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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\Messenger\Transport;
13+
14+
use Symfony\Component\Messenger\Envelope;
15+
use Symfony\Contracts\Service\ResetInterface;
16+
17+
/**
18+
* Transport that stay in memory. Useful for testing purpose.
19+
*
20+
* @author Gary PEGEOT <garypegeot@gmail.com>
21+
*
22+
* @experimental in 4.3
23+
*/
24+
class InMemoryTransport implements TransportInterface, ResetInterface
25+
{
26+
/**
27+
* @var Envelope[]
28+
*/
29+
private $sent = [];
30+
31+
/**
32+
* @var Envelope[]
33+
*/
34+
private $acknowledged = [];
35+
36+
/**
37+
* @var Envelope[]
38+
*/
39+
private $rejected = [];
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function get(): iterable
45+
{
46+
return $this->sent;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function ack(Envelope $envelope): void
53+
{
54+
$this->acknowledged[] = $envelope;
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function reject(Envelope $envelope): void
61+
{
62+
$this->rejected[] = $envelope;
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function send(Envelope $envelope): Envelope
69+
{
70+
$this->sent[] = $envelope;
71+
72+
return $envelope;
73+
}
74+
75+
public function reset()
76+
{
77+
$this->sent = $this->rejected = $this->acknowledged = [];
78+
}
79+
80+
/**
81+
* @return Envelope[]
82+
*/
83+
public function getAcknowledged(): array
84+
{
85+
return $this->acknowledged;
86+
}
87+
88+
/**
89+
* @return Envelope[]
90+
*/
91+
public function getRejected(): array
92+
{
93+
return $this->rejected;
94+
}
95+
}
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\Messenger\Transport;
13+
14+
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
15+
use Symfony\Contracts\Service\ResetInterface;
16+
17+
/**
18+
* @author Gary PEGEOT <garypegeot@gmail.com>
19+
*
20+
* @experimental in 4.3
21+
*/
22+
class InMemoryTransportFactory implements TransportFactoryInterface, ResetInterface
23+
{
24+
/**
25+
* @var InMemoryTransport[]
26+
*/
27+
private $createdTransports = [];
28+
29+
public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface
30+
{
31+
return $this->createdTransports[] = new InMemoryTransport();
32+
}
33+
34+
public function supports(string $dsn, array $options): bool
35+
{
36+
return 0 === strpos($dsn, 'in-memory://');
37+
}
38+
39+
public function reset()
40+
{
41+
foreach ($this->createdTransports as $transport) {
42+
$transport->reset();
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)
0