-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/Symfony/Component/Messenger/Tests/Transport/InMemoryTransportFactoryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
]; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/Symfony/Component/Messenger/Tests/Transport/InMemoryTransportTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @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
95
src/Symfony/Component/Messenger/Transport/InMemoryTransport.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stays |
||
* | ||
* @author Gary PEGEOT <garypegeot@gmail.com> | ||
GaryPEGEOT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @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; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Symfony/Component/Messenger/Transport/InMemoryTransportFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; | ||
sroze marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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(); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.