Make the in-memory transport resettable
This commit is contained in:
parent
fe759205c5
commit
8f8c82e009
@ -72,6 +72,7 @@
|
||||
|
||||
<service id="messenger.transport.in_memory.factory" class="Symfony\Component\Messenger\Transport\InMemoryTransportFactory">
|
||||
<tag name="messenger.transport_factory" />
|
||||
<tag name="kernel.reset" method="reset" />
|
||||
</service>
|
||||
|
||||
<!-- retry -->
|
||||
|
@ -12,6 +12,8 @@
|
||||
namespace Symfony\Component\Messenger\Tests\Transport;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
|
||||
use Symfony\Component\Messenger\Transport\InMemoryTransport;
|
||||
use Symfony\Component\Messenger\Transport\InMemoryTransportFactory;
|
||||
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
|
||||
@ -52,6 +54,16 @@ class InMemoryTransportFactoryTest extends TestCase
|
||||
$this->assertInstanceOf(InMemoryTransport::class, $this->factory->createTransport('in-memory://', [], $serializer));
|
||||
}
|
||||
|
||||
public function testResetCreatedTransports()
|
||||
{
|
||||
$transport = $this->factory->createTransport('in-memory://', [], $this->createMock(SerializerInterface::class));
|
||||
$transport->send(Envelope::wrap(new DummyMessage('Hello.')));
|
||||
|
||||
$this->assertCount(1, $transport->get());
|
||||
$this->factory->reset();
|
||||
$this->assertCount(0, $transport->get());
|
||||
}
|
||||
|
||||
public function provideDSN(): array
|
||||
{
|
||||
return [
|
||||
|
@ -12,16 +12,34 @@
|
||||
namespace Symfony\Component\Messenger\Transport;
|
||||
|
||||
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
|
||||
use Symfony\Contracts\Service\ResetInterface;
|
||||
|
||||
class InMemoryTransportFactory implements TransportFactoryInterface
|
||||
/**
|
||||
* @author Gary PEGEOT <garypegeot@gmail.com>
|
||||
*
|
||||
* @experimental in 4.3
|
||||
*/
|
||||
class InMemoryTransportFactory implements TransportFactoryInterface, ResetInterface
|
||||
{
|
||||
/**
|
||||
* @var InMemoryTransport[]
|
||||
*/
|
||||
private $createdTransports = [];
|
||||
|
||||
public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface
|
||||
{
|
||||
return new InMemoryTransport();
|
||||
return $this->createdTransports[] = new InMemoryTransport();
|
||||
}
|
||||
|
||||
public function supports(string $dsn, array $options): bool
|
||||
{
|
||||
return 0 === strpos($dsn, 'in-memory://');
|
||||
}
|
||||
|
||||
public function reset()
|
||||
{
|
||||
foreach ($this->createdTransports as $transport) {
|
||||
$transport->reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user