diff --git a/Command/ConsumeMessagesCommand.php b/Command/ConsumeMessagesCommand.php index 1a84381..9fcb6a5 100644 --- a/Command/ConsumeMessagesCommand.php +++ b/Command/ConsumeMessagesCommand.php @@ -313,7 +313,7 @@ private function convertToBytes(string $memoryLimit): int } elseif (str_starts_with($max, '0')) { $max = \intval($max, 8); } else { - $max = (int) $max; + $max = (float) $max; } switch (substr(rtrim($memoryLimit, 'b'), -1)) { @@ -326,6 +326,6 @@ private function convertToBytes(string $memoryLimit): int case 'k': $max *= 1024; } - return $max; + return (int) $max; } } diff --git a/Command/FailedMessagesRemoveCommand.php b/Command/FailedMessagesRemoveCommand.php index de2b6f3..e7fdb75 100644 --- a/Command/FailedMessagesRemoveCommand.php +++ b/Command/FailedMessagesRemoveCommand.php @@ -35,7 +35,7 @@ protected function configure(): void new InputArgument('id', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Specific message id(s) to remove'), new InputOption('all', null, InputOption::VALUE_NONE, 'Remove all failed messages from the transport'), new InputOption('force', null, InputOption::VALUE_NONE, 'Force the operation without confirmation'), - new InputOption('transport', null, InputOption::VALUE_OPTIONAL, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION), + new InputOption('transport', null, InputOption::VALUE_REQUIRED, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION), new InputOption('show-messages', null, InputOption::VALUE_NONE, 'Display messages before removing it (if multiple ids are given)'), new InputOption('class-filter', null, InputOption::VALUE_REQUIRED, 'Filter by a specific class name'), ]) @@ -146,7 +146,7 @@ private function getMessageIdsByClassFilter(string $classFilter, ListableReceive } $ids[] = $this->getMessageId($envelope); - }; + } } finally { $this->phpSerializer?->rejectPhpIncompleteClass(); } diff --git a/Command/FailedMessagesRetryCommand.php b/Command/FailedMessagesRetryCommand.php index 15dbe84..32f5357 100644 --- a/Command/FailedMessagesRetryCommand.php +++ b/Command/FailedMessagesRetryCommand.php @@ -65,8 +65,8 @@ protected function configure(): void ->setDefinition([ new InputArgument('id', InputArgument::IS_ARRAY, 'Specific message id(s) to retry'), new InputOption('force', null, InputOption::VALUE_NONE, 'Force action without confirmation'), - new InputOption('transport', null, InputOption::VALUE_OPTIONAL, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION), - new InputOption('keepalive', null, InputOption::VALUE_OPTIONAL, 'Whether to use the transport\'s keepalive mechanism if implemented', self::DEFAULT_KEEPALIVE_INTERVAL), + new InputOption('transport', null, InputOption::VALUE_REQUIRED, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION), + new InputOption('keepalive', null, InputOption::VALUE_REQUIRED, 'Whether to use the transport\'s keepalive mechanism if implemented', self::DEFAULT_KEEPALIVE_INTERVAL), ]) ->setHelp(<<<'EOF' The %command.name% retries message in the failure transport. diff --git a/Command/FailedMessagesShowCommand.php b/Command/FailedMessagesShowCommand.php index f052f86..927e670 100644 --- a/Command/FailedMessagesShowCommand.php +++ b/Command/FailedMessagesShowCommand.php @@ -35,7 +35,7 @@ protected function configure(): void ->setDefinition([ new InputArgument('id', InputArgument::OPTIONAL, 'Specific message id to show'), new InputOption('max', null, InputOption::VALUE_REQUIRED, 'Maximum number of messages to list', 50), - new InputOption('transport', null, InputOption::VALUE_OPTIONAL, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION), + new InputOption('transport', null, InputOption::VALUE_REQUIRED, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION), new InputOption('stats', null, InputOption::VALUE_NONE, 'Display the message count by class'), new InputOption('class-filter', null, InputOption::VALUE_REQUIRED, 'Filter by a specific class name'), ]) diff --git a/Middleware/RouterContextMiddleware.php b/Middleware/RouterContextMiddleware.php index 9a234df..cf5105b 100644 --- a/Middleware/RouterContextMiddleware.php +++ b/Middleware/RouterContextMiddleware.php @@ -56,7 +56,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope $currentPathInfo = $context->getPathInfo(); $currentQueryString = $context->getQueryString(); - /* @var RouterContextStamp $contextStamp */ + /** @var RouterContextStamp $contextStamp */ $context ->setBaseUrl($contextStamp->getBaseUrl()) ->setMethod($contextStamp->getMethod()) diff --git a/Tests/Command/ConsumeMessagesCommandTest.php b/Tests/Command/ConsumeMessagesCommandTest.php index 7790e07..e6ec50c 100644 --- a/Tests/Command/ConsumeMessagesCommandTest.php +++ b/Tests/Command/ConsumeMessagesCommandTest.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Messenger\Tests\Command; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; +use Psr\Log\LoggerTrait; use Symfony\Component\Console\Application; use Symfony\Component\Console\Exception\InvalidOptionException; use Symfony\Component\Console\Tester\CommandCompletionTester; @@ -205,6 +207,48 @@ public function testRunWithTimeLimit() $this->assertStringContainsString('[OK] Consuming messages from transport "dummy-receiver"', $tester->getDisplay()); } + public function testRunWithMemoryLimit() + { + $envelope = new Envelope(new \stdClass(), [new BusNameStamp('dummy-bus')]); + + $receiver = $this->createMock(ReceiverInterface::class); + $receiver->method('get')->willReturn([$envelope]); + + $receiverLocator = new Container(); + $receiverLocator->set('dummy-receiver', $receiver); + + $bus = $this->createMock(MessageBusInterface::class); + + $busLocator = new Container(); + $busLocator->set('dummy-bus', $bus); + + $logger = new class implements LoggerInterface { + use LoggerTrait; + + public array $logs = []; + + public function log(...$args): void + { + $this->logs[] = $args; + } + }; + $command = new ConsumeMessagesCommand(new RoutableMessageBus($busLocator), $receiverLocator, new EventDispatcher(), $logger); + + $application = new Application(); + $application->add($command); + $tester = new CommandTester($application->get('messenger:consume')); + $tester->execute([ + 'receivers' => ['dummy-receiver'], + '--memory-limit' => '1.5M', + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertStringContainsString('[OK] Consuming messages from transport "dummy-receiver"', $tester->getDisplay()); + $this->assertStringContainsString('The worker will automatically exit once it has exceeded 1.5M of memory', $tester->getDisplay()); + + $this->assertSame(1572864, $logger->logs[1][2]['limit']); + } + public function testRunWithAllOption() { $envelope1 = new Envelope(new \stdClass(), [new BusNameStamp('dummy-bus')]); diff --git a/Tests/Command/FailedMessagesRemoveCommandTest.php b/Tests/Command/FailedMessagesRemoveCommandTest.php index ee83c39..21df1d6 100644 --- a/Tests/Command/FailedMessagesRemoveCommandTest.php +++ b/Tests/Command/FailedMessagesRemoveCommandTest.php @@ -221,7 +221,7 @@ public function testRemoveMessagesFilteredByClassMessage() ); $tester = new CommandTester($command); - $tester->execute(['--class-filter' => "stdClass", '--force' => true, '--show-messages' => true]); + $tester->execute(['--class-filter' => 'stdClass', '--force' => true, '--show-messages' => true]); $this->assertStringContainsString('Can you confirm you want to remove 2 messages? (yes/no)', $tester->getDisplay()); $this->assertStringContainsString('Failed Message Details', $tester->getDisplay()); diff --git a/Tests/EventListener/SendFailedMessageToFailureTransportListenerTest.php b/Tests/EventListener/SendFailedMessageToFailureTransportListenerTest.php index 9060ff5..a6473e6 100644 --- a/Tests/EventListener/SendFailedMessageToFailureTransportListenerTest.php +++ b/Tests/EventListener/SendFailedMessageToFailureTransportListenerTest.php @@ -26,7 +26,7 @@ public function testItSendsToTheFailureTransportWithSenderLocator() $receiverName = 'my_receiver'; $sender = $this->createMock(SenderInterface::class); $sender->expects($this->once())->method('send')->with($this->callback(function ($envelope) use ($receiverName) { - /* @var Envelope $envelope */ + /** @var Envelope $envelope */ $this->assertInstanceOf(Envelope::class, $envelope); /** @var SentToFailureTransportStamp $sentToFailureTransportStamp */ @@ -101,7 +101,7 @@ public function testItSendsToTheFailureTransportWithMultipleFailedTransports() $receiverName = 'my_receiver'; $sender = $this->createMock(SenderInterface::class); $sender->expects($this->once())->method('send')->with($this->callback(function ($envelope) use ($receiverName) { - /* @var Envelope $envelope */ + /** @var Envelope $envelope */ $this->assertInstanceOf(Envelope::class, $envelope); /** @var SentToFailureTransportStamp $sentToFailureTransportStamp */ diff --git a/Tests/Middleware/SendMessageMiddlewareTest.php b/Tests/Middleware/SendMessageMiddlewareTest.php index 762e591..eb8af4c 100644 --- a/Tests/Middleware/SendMessageMiddlewareTest.php +++ b/Tests/Middleware/SendMessageMiddlewareTest.php @@ -41,7 +41,7 @@ public function testItSendsTheMessageToAssignedSender() $envelope = $middleware->handle($envelope, $this->getStackMock(false)); - /* @var SentStamp $stamp */ + /** @var SentStamp $stamp */ $this->assertInstanceOf(SentStamp::class, $stamp = $envelope->last(SentStamp::class), 'it adds a sent stamp'); $this->assertSame('my_sender', $stamp->getSenderAlias()); $this->assertSame($sender::class, $stamp->getSenderClass());