Skip to content

Commit fe75920

Browse files
GaryPEGEOTsroze
authored andcommitted
Add a "null://" transport
1 parent 8977f74 commit fe75920

File tree

6 files changed

+258
-0
lines changed

6 files changed

+258
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

+1
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

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
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+
</service>
76+
7377
<!-- retry -->
7478
<service id="messenger.retry_strategy_locator">
7579
<tag name="container.service_locator" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\Transport\InMemoryTransport;
16+
use Symfony\Component\Messenger\Transport\InMemoryTransportFactory;
17+
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
18+
19+
/**
20+
* @internal
21+
*
22+
* @author Gary PEGEOT <garypegeot@gmail.com>
23+
*/
24+
class InMemoryTransportFactoryTest extends TestCase
25+
{
26+
/**
27+
* @var InMemoryTransportFactory
28+
*/
29+
private $factory;
30+
31+
protected function setUp()
32+
{
33+
$this->factory = new InMemoryTransportFactory();
34+
}
35+
36+
/**
37+
* @param string $dsn
38+
* @param bool $expected
39+
*
40+
* @dataProvider provideDSN
41+
*/
42+
public function testSupports(string $dsn, bool $expected = true)
43+
{
44+
$this->assertSame($expected, $this->factory->supports($dsn, []), 'InMemoryTransportFactory::supports returned unexpected result.');
45+
}
46+
47+
public function testCreateTransport()
48+
{
49+
/** @var SerializerInterface $serializer */
50+
$serializer = $this->createMock(SerializerInterface::class);
51+
52+
$this->assertInstanceOf(InMemoryTransport::class, $this->factory->createTransport('in-memory://', [], $serializer));
53+
}
54+
55+
public function provideDSN(): array
56+
{
57+
return [
58+
'Supported' => ['in-memory://foo'],
59+
'Unsupported' => ['amqp://bar', false],
60+
];
61+
}
62+
}
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+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
16+
class InMemoryTransportFactory implements TransportFactoryInterface
17+
{
18+
public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface
19+
{
20+
return new InMemoryTransport();
21+
}
22+
23+
public function supports(string $dsn, array $options): bool
24+
{
25+
return 0 === strpos($dsn, 'in-memory://');
26+
}
27+
}

0 commit comments

Comments
 (0)