Skip to content

Commit

Permalink
feat: have TestTransport additional receiver interfaces
Browse files Browse the repository at this point in the history
- `ListableReceiverInterface`
- `MessageCountAwareInterface`
  • Loading branch information
kbond committed Jun 26, 2024
1 parent 7164fce commit ceff558
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
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.');
}

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

0 comments on commit ceff558

Please sign in to comment.