diff --git a/src/TestEnvelope.php b/src/TestEnvelope.php index 6b3b6b7..ca6e68f 100644 --- a/src/TestEnvelope.php +++ b/src/TestEnvelope.php @@ -22,7 +22,7 @@ */ final class TestEnvelope { - public function __construct(private Envelope $envelope) + public function __construct(public readonly Envelope $envelope) { } diff --git a/src/Transport/TestTransport.php b/src/Transport/TestTransport.php index 2efd914..f7a790c 100644 --- a/src/Transport/TestTransport.php +++ b/src/Transport/TestTransport.php @@ -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 */ -final class TestTransport implements TransportInterface +final class TestTransport implements TransportInterface, ListableReceiverInterface, MessageCountAwareInterface { private const DEFAULT_OPTIONS = [ 'intercept' => true, @@ -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.'); + } + + 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 diff --git a/tests/InteractsWithMessengerTest.php b/tests/InteractsWithMessengerTest.php index 997b371..655d9e7 100644 --- a/tests/InteractsWithMessengerTest.php +++ b/tests/InteractsWithMessengerTest.php @@ -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));