Skip to content
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

feat: have TestTransport implement additional receiver interfaces #84

Merged
merged 1 commit into from
Jun 26, 2024
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
2 changes: 1 addition & 1 deletion src/TestEnvelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
final class TestEnvelope
{
public function __construct(private Envelope $envelope)
public function __construct(public readonly Envelope $envelope)
{
}

Expand Down
23 changes: 22 additions & 1 deletion src/Transport/TestTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;
use Symfony\Component\Messenger\Worker;
use Zenstruck\Assert;
use Zenstruck\Messenger\Test\Stamp\AvailableAtStamp;
use Zenstruck\Messenger\Test\TestEnvelope;

/**
* @author Kevin Bond <[email protected]>
*/
final class TestTransport implements TransportInterface
final class TestTransport implements TransportInterface, ListableReceiverInterface, MessageCountAwareInterface
{
private const DEFAULT_OPTIONS = [
'intercept' => true,
Expand Down Expand Up @@ -279,6 +282,24 @@ public function reject(Envelope $envelope): void
$this->collectMessage(self::$rejected, $envelope);
}

public function all(?int $limit = null): iterable
{
return \array_map(
fn(TestEnvelope $envelope) => $envelope->envelope,
\array_slice($this->queue()->all(), 0, $limit),
);
}

public function find(mixed $id): ?Envelope
{
throw new \BadMethodCallException('Not supported.');
kbond marked this conversation as resolved.
Show resolved Hide resolved
}

public function getMessageCount(): int
{
return $this->queue()->count();
}

/**
* @param Envelope|object|array{headers?:mixed[],body:string} $what object: will be wrapped in envelope
* array: will be decoded into envelope
Expand Down
46 changes: 46 additions & 0 deletions tests/InteractsWithMessengerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,52 @@ public function process_empty_queue(): void
;
}

/**
* @test
*/
public function transport_is_message_count_aware(): void
{
self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());

$this->assertSame(2, $this->transport()->getMessageCount());

$this->transport()->process();

$this->assertSame(0, $this->transport()->getMessageCount());
}

/**
* @test
*/
public function transport_is_listable(): void
{
self::getContainer()->get(MessageBusInterface::class)->dispatch($msgA = new MessageA());
self::getContainer()->get(MessageBusInterface::class)->dispatch($msgB = new MessageB());

$messages = \array_map(fn(Envelope $e) => $e->getMessage(), $this->transport()->all());

$this->assertSame([$msgA, $msgB], $messages);

$messages = \array_map(fn(Envelope $e) => $e->getMessage(), $this->transport()->all(1));

$this->assertSame([$msgA], $messages);

$this->transport()->process();

$this->assertEmpty($this->transport()->all());
}

/**
* @test
*/
public function cannot_use_find_on_transport(): void
{
$this->expectException(\BadMethodCallException::class);

$this->transport()->find(1);
}

protected static function bootKernel(array $options = []): KernelInterface // @phpstan-ignore-line
{
return parent::bootKernel(\array_merge(['environment' => 'single_transport'], $options));
Expand Down
Loading