Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 969f19b

Browse filesBrowse files
committed
bug #48085 [Messenger] Tell about messenger:consume invalid limit options (MatTheCat)
This PR was merged into the 4.4 branch. Discussion ---------- [Messenger] Tell about messenger:consume invalid limit options | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #48082 | License | MIT | Doc PR | N/A Commits ------- cca8bcd Tell about messenger:consume invalid limit options
2 parents 1fd4b63 + cca8bcd commit 969f19b
Copy full SHA for 969f19b

File tree

4 files changed

+50
-2
lines changed
Filter options

4 files changed

+50
-2
lines changed

‎src/Symfony/Component/Console/Exception/InvalidOptionException.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Exception/InvalidOptionException.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Console\Exception;
1313

1414
/**
15-
* Represents an incorrect option name typed in the console.
15+
* Represents an incorrect option name or value typed in the console.
1616
*
1717
* @author Jérôme Tamarelle <jerome@tamarelle.net>
1818
*/

‎src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Container\ContainerInterface;
1515
use Psr\Log\LoggerInterface;
1616
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Exception\InvalidOptionException;
1718
use Symfony\Component\Console\Exception\RuntimeException;
1819
use Symfony\Component\Console\Input\InputArgument;
1920
use Symfony\Component\Console\Input\InputInterface;
@@ -163,7 +164,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
163164
}
164165

165166
$stopsWhen = [];
166-
if ($limit = $input->getOption('limit')) {
167+
if (null !== ($limit = $input->getOption('limit'))) {
168+
if (!is_numeric($limit) || 0 >= $limit) {
169+
throw new InvalidOptionException(sprintf('Option "limit" must be a positive integer, "%s" passed.', $limit));
170+
}
171+
167172
$stopsWhen[] = "processed {$limit} messages";
168173
$this->eventDispatcher->addSubscriber(new StopWorkerOnMessageLimitListener($limit, $this->logger));
169174
}
@@ -174,6 +179,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
174179
}
175180

176181
if (null !== ($timeLimit = $input->getOption('time-limit'))) {
182+
if (!is_numeric($timeLimit) || 0 >= $limit) {
183+
throw new InvalidOptionException(sprintf('Option "time-limit" must be a positive integer, "%s" passed.', $timeLimit));
184+
}
185+
177186
$stopsWhen[] = "been running for {$timeLimit}s";
178187
$this->eventDispatcher->addSubscriber(new StopWorkerOnTimeLimitListener($timeLimit, $this->logger));
179188
}

‎src/Symfony/Component/Messenger/EventListener/StopWorkerOnTimeLimitListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/EventListener/StopWorkerOnTimeLimitListener.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1616
use Symfony\Component\Messenger\Event\WorkerRunningEvent;
1717
use Symfony\Component\Messenger\Event\WorkerStartedEvent;
18+
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
1819

1920
/**
2021
* @author Simon Delicata <simon.delicata@free.fr>
@@ -30,6 +31,10 @@ public function __construct(int $timeLimitInSeconds, LoggerInterface $logger = n
3031
{
3132
$this->timeLimitInSeconds = $timeLimitInSeconds;
3233
$this->logger = $logger;
34+
35+
if ($timeLimitInSeconds <= 0) {
36+
throw new InvalidArgumentException('Time limit must be greater than zero.');
37+
}
3338
}
3439

3540
public function onWorkerStarted(): void

‎src/Symfony/Component/Messenger/Tests/Command/ConsumeMessagesCommandTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Tests/Command/ConsumeMessagesCommandTest.php
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Exception\InvalidOptionException;
1617
use Symfony\Component\Console\Tester\CommandTester;
1718
use Symfony\Component\DependencyInjection\ContainerInterface;
1819
use Symfony\Component\DependencyInjection\ServiceLocator;
@@ -172,4 +173,37 @@ public function testRunWithBusOptionAndBusLocator()
172173
$this->assertSame(0, $tester->getStatusCode());
173174
$this->assertStringContainsString('[OK] Consuming messages from transports "dummy-receiver"', $tester->getDisplay());
174175
}
176+
177+
/**
178+
* @dataProvider getInvalidOptions
179+
*/
180+
public function testRunWithInvalidOption(string $option, string $value, string $expectedMessage)
181+
{
182+
$receiverLocator = $this->createMock(ContainerInterface::class);
183+
$receiverLocator->expects($this->once())->method('has')->with('dummy-receiver')->willReturn(true);
184+
185+
$busLocator = $this->createMock(ContainerInterface::class);
186+
187+
$command = new ConsumeMessagesCommand(new RoutableMessageBus($busLocator), $receiverLocator, new EventDispatcher());
188+
189+
$application = new Application();
190+
$application->add($command);
191+
$tester = new CommandTester($application->get('messenger:consume'));
192+
193+
$this->expectException(InvalidOptionException::class);
194+
$this->expectExceptionMessage($expectedMessage);
195+
$tester->execute([
196+
'receivers' => ['dummy-receiver'],
197+
$option => $value,
198+
]);
199+
}
200+
201+
public function getInvalidOptions()
202+
{
203+
yield 'Zero message limit' => ['--limit', '0', 'Option "limit" must be a positive integer, "0" passed.'];
204+
yield 'Non-numeric message limit' => ['--limit', 'whatever', 'Option "limit" must be a positive integer, "whatever" passed.'];
205+
206+
yield 'Zero second time limit' => ['--time-limit', '0', 'Option "time-limit" must be a positive integer, "0" passed.'];
207+
yield 'Non-numeric time limit' => ['--time-limit', 'whatever', 'Option "time-limit" must be a positive integer, "whatever" passed.'];
208+
}
175209
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.