|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
15 | 15 | use Symfony\Component\Console\Application;
|
| 16 | +use Symfony\Component\Console\Exception\InvalidOptionException; |
16 | 17 | use Symfony\Component\Console\Tester\CommandCompletionTester;
|
17 | 18 | use Symfony\Component\Console\Tester\CommandTester;
|
18 | 19 | use Symfony\Component\DependencyInjection\ContainerInterface;
|
@@ -68,7 +69,7 @@ public function testBasicRun()
|
68 | 69 | ]);
|
69 | 70 |
|
70 | 71 | $tester->assertCommandIsSuccessful();
|
71 |
| - $this->assertStringContainsString('[OK] Consuming messages from transports "dummy-receiver"', $tester->getDisplay()); |
| 72 | + $this->assertStringContainsString('[OK] Consuming messages from transport "dummy-receiver"', $tester->getDisplay()); |
72 | 73 | }
|
73 | 74 |
|
74 | 75 | public function testRunWithBusOption()
|
@@ -101,7 +102,7 @@ public function testRunWithBusOption()
|
101 | 102 | ]);
|
102 | 103 |
|
103 | 104 | $tester->assertCommandIsSuccessful();
|
104 |
| - $this->assertStringContainsString('[OK] Consuming messages from transports "dummy-receiver"', $tester->getDisplay()); |
| 105 | + $this->assertStringContainsString('[OK] Consuming messages from transport "dummy-receiver"', $tester->getDisplay()); |
105 | 106 | }
|
106 | 107 |
|
107 | 108 | public function provideRunWithResetServicesOption(): iterable
|
@@ -146,7 +147,71 @@ public function testRunWithResetServicesOption(bool $shouldReset)
|
146 | 147 |
|
147 | 148 | $this->assertEquals($shouldReset, $receiver->hasBeenReset(), '$receiver->reset() should have been called');
|
148 | 149 | $tester->assertCommandIsSuccessful();
|
149 |
| - $this->assertStringContainsString('[OK] Consuming messages from transports "dummy-receiver"', $tester->getDisplay()); |
| 150 | + $this->assertStringContainsString('[OK] Consuming messages from transport "dummy-receiver"', $tester->getDisplay()); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * @dataProvider getInvalidOptions |
| 155 | + */ |
| 156 | + public function testRunWithInvalidOption(string $option, string $value, string $expectedMessage) |
| 157 | + { |
| 158 | + $receiverLocator = $this->createMock(ContainerInterface::class); |
| 159 | + $receiverLocator->expects($this->once())->method('has')->with('dummy-receiver')->willReturn(true); |
| 160 | + |
| 161 | + $busLocator = $this->createMock(ContainerInterface::class); |
| 162 | + |
| 163 | + $command = new ConsumeMessagesCommand(new RoutableMessageBus($busLocator), $receiverLocator, new EventDispatcher()); |
| 164 | + |
| 165 | + $application = new Application(); |
| 166 | + $application->add($command); |
| 167 | + $tester = new CommandTester($application->get('messenger:consume')); |
| 168 | + |
| 169 | + $this->expectException(InvalidOptionException::class); |
| 170 | + $this->expectExceptionMessage($expectedMessage); |
| 171 | + $tester->execute([ |
| 172 | + 'receivers' => ['dummy-receiver'], |
| 173 | + $option => $value, |
| 174 | + ]); |
| 175 | + } |
| 176 | + |
| 177 | + public function getInvalidOptions() |
| 178 | + { |
| 179 | + yield 'Zero message limit' => ['--limit', '0', 'Option "limit" must be a positive integer, "0" passed.']; |
| 180 | + yield 'Non-numeric message limit' => ['--limit', 'whatever', 'Option "limit" must be a positive integer, "whatever" passed.']; |
| 181 | + |
| 182 | + yield 'Zero second time limit' => ['--time-limit', '0', 'Option "time-limit" must be a positive integer, "0" passed.']; |
| 183 | + yield 'Non-numeric time limit' => ['--time-limit', 'whatever', 'Option "time-limit" must be a positive integer, "whatever" passed.']; |
| 184 | + } |
| 185 | + |
| 186 | + public function testRunWithTimeLimit() |
| 187 | + { |
| 188 | + $envelope = new Envelope(new \stdClass(), [new BusNameStamp('dummy-bus')]); |
| 189 | + |
| 190 | + $receiver = $this->createMock(ReceiverInterface::class); |
| 191 | + $receiver->method('get')->willReturn([$envelope]); |
| 192 | + |
| 193 | + $receiverLocator = $this->createMock(ContainerInterface::class); |
| 194 | + $receiverLocator->method('has')->with('dummy-receiver')->willReturn(true); |
| 195 | + $receiverLocator->method('get')->with('dummy-receiver')->willReturn($receiver); |
| 196 | + |
| 197 | + $bus = $this->createMock(MessageBusInterface::class); |
| 198 | + |
| 199 | + $busLocator = $this->createMock(ContainerInterface::class); |
| 200 | + $busLocator->method('has')->with('dummy-bus')->willReturn(true); |
| 201 | + $busLocator->method('get')->with('dummy-bus')->willReturn($bus); |
| 202 | + |
| 203 | + $command = new ConsumeMessagesCommand(new RoutableMessageBus($busLocator), $receiverLocator, new EventDispatcher()); |
| 204 | + |
| 205 | + $application = new Application(); |
| 206 | + $application->add($command); |
| 207 | + $tester = new CommandTester($application->get('messenger:consume')); |
| 208 | + $tester->execute([ |
| 209 | + 'receivers' => ['dummy-receiver'], |
| 210 | + '--time-limit' => 1, |
| 211 | + ]); |
| 212 | + |
| 213 | + $this->assertSame(0, $tester->getStatusCode()); |
| 214 | + $this->assertStringContainsString('[OK] Consuming messages from transport "dummy-receiver"', $tester->getDisplay()); |
150 | 215 | }
|
151 | 216 |
|
152 | 217 | /**
|
|
0 commit comments