8000 [Messenger] Add a "in-memory://" transport by GaryPEGEOT · Pull Request #29097 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Add a "in-memory://" transport #29097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ CHANGELOG
* Added support for PHP files with translations in translation commands.
* Added support for boolean container parameters within routes.
* Added the `messenger:setup-transports` command to setup messenger transports
* Added a `InMemoryTransport` to Messenger. Use it with a DSN starting with `in-memory://`.

4.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
<tag name="messenger.transport_factory" />
</service>

<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 -->
<service id="messenger.retry_strategy_locator">
<tag name="container.service_locator" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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;

/**
* @internal
*
* @author Gary PEGEOT <garypegeot@gmail.com>
*/
class InMemoryTransportFactoryTest extends TestCase
{
/**
* @var InMemoryTransportFactory
*/
private $factory;

protected function setUp()
{
$this->factory = new InMemoryTransportFactory();
}

/**
* @param string $dsn
* @param bool $expected
*
* @dataProvider provideDSN
*/
public function testSupports(string $dsn, bool $expected = true)
{
$this->assertSame($expected, $this->factory->supports($dsn, []), 'InMemoryTransportFactory::supports returned unexpected result.');
}

public function testCreateTransport()
{
/** @var SerializerInterface $serializer */
$serializer = $this->createMock(SerializerInterface::class);

$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 [
'Supported' => ['in-memory://foo'],
'Unsupported' => ['amqp://bar', false],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Tests\Transport;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\InMemoryTransport;

/**
* @internal
*
* @author Gary PEGEOT <garypegeot@gmail.com>
*/
class InMemoryTransportTest extends TestCase
{
/**
* @var InMemoryTransport
*/
private $transport;

protected function setUp()
{
$this->transport = new InMemoryTransport();
}

public function testSend()
{
$envelope = new Envelope(new \stdClass());
$this->transport->send($envelope);
$this->assertSame([$envelope], $this->transport->get());
}

public function testAck()
{
$envelope = new Envelope(new \stdClass());
$this->transport->ack($envelope);
$this->assertSame([$envelope], $this->transport->getAcknowledged());
}

public function testReject()
{
$envelope = new Envelope(new \stdClass());
$this->transport->reject($envelope);
$this->assertSame([$envelope], $this->transport->getRejected());
}

public function testReset()
{
$envelope = new Envelope(new \stdClass());
$this->transport->send($envelope);
$this->transport->ack($envelope);
$this->transport->reject($envelope);

$this->transport->reset();

$this->assertEmpty($this->transport->get(), 'Should be empty after reset');
$this->assertEmpty($this->transport->getAcknowledged(), 'Should be empty after reset');
$this->assertEmpty($this->transport->getRejected(), 'Should be empty after reset');
}
}
95 changes: 95 additions & 0 deletions src/Symfony/Component/Messenger/Transport/InMemoryTransport.php

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Transport;

use Symfony\Component\Messenger\Envelope;
use Symfony\Contracts\Service\ResetInterface;

/**
* Transport that stay in memory. Useful for testing purpose.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stays

*
* @author Gary PEGEOT <garypegeot@gmail.com>
*
* @experimental in 4.3
*/
class InMemoryTransport implements TransportInterface, ResetInterface
{
/**
* @var Envelope[]
*/
private $sent = [];

/**
* @var Envelope[]
*/
private $acknowledged = [];
/**
* @var Envelope[]
*/
private $rejected = [];

/**
* {@inheritdoc}
*/
public function get(): iterable
{
return $this->sent;
}

/**
* {@inheritdoc}
*/
public function ack(Envelope $envelope): void
{
$this->acknowledged[] = $envelope;
}

/**
* {@inheritdoc}
*/
public function reject(Envelope $envelope): void
{
$this->rejected[] = $envelope;
}

/**
* {@inheritdoc}
*/
public function send(Envelope $envelope): Envelope
{
$this->sent[] = $envelope;

return $envelope;
}

public function reset()
{
$this->sent = $this->rejected = $this->acknowledged = [];
}

/**
* @return Envelope[]
*/
public function getAcknowledged(): array
{
return $this->acknowledged;
}

/**
* @return Envelope[]
*/
public function getRejected(): array
{
return $this->rejected;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Transport;

use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* @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 $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();
}
}
}
0