|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Messenger\Tests\Command; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Console\Tester\CommandTester; |
| 16 | +use Symfony\Component\DependencyInjection\ServiceLocator; |
| 17 | +use Symfony\Component\Messenger\Command\StatsCommand; |
| 18 | +use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface; |
| 19 | +use Symfony\Component\Messenger\Transport\TransportInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Kévin Thérage <[email protected]> |
| 23 | + */ |
| 24 | +class StatsCommandTest extends TestCase |
| 25 | +{ |
| 26 | + private StatsCommand $command; |
| 27 | + |
| 28 | + public function setUp(): void |
| 29 | + { |
| 30 | + $messageCountableTransport = $this->createMock(MessageCountAwareInterface::class); |
| 31 | + $messageCountableTransport->method('getMessageCount')->willReturn(6); |
| 32 | + |
| 33 | + $simpleTransport = $this->createMock(TransportInterface::class); |
| 34 | + |
| 35 | + // mock a service locator |
| 36 | + /** @var MockObject&ServiceLocator $serviceLocator */ |
| 37 | + $serviceLocator = $this->createMock(ServiceLocator::class); |
| 38 | + $serviceLocator |
| 39 | + ->method('get') |
| 40 | + ->willReturnCallback(function (string $transportName) use ($messageCountableTransport, $simpleTransport) { |
| 41 | + if (\in_array($transportName, ['message_countable', 'another_message_countable'], true)) { |
| 42 | + return $messageCountableTransport; |
| 43 | + } |
| 44 | + |
| 45 | + return $simpleTransport; |
| 46 | + }); |
| 47 | + $serviceLocator |
| 48 | + ->method('has') |
| 49 | + ->willReturnCallback(function (string $transportName) { |
| 50 | + return \in_array($transportName, ['message_countable', 'simple', 'another_message_countable'], true); |
| 51 | + }) |
| 52 | + ; |
| 53 | + |
| 54 | + $this->command = new StatsCommand($serviceLocator, [ |
| 55 | + 'message_countable', |
| 56 | + 'simple', |
| 57 | + 'another_message_countable', |
| 58 | + 'unexisting' |
| 59 | + ]); |
| 60 | + } |
| 61 | + |
| 62 | + public function testWithoutArgument(): void |
| 63 | + { |
| 64 | + $tester = new CommandTester($this->command); |
| 65 | + $tester->execute([]); |
| 66 | + $display = $tester->getDisplay(); |
| 67 | + |
| 68 | + $this->assertStringContainsString('[WARNING] The "unexisting" transport does not exist.', $display); |
| 69 | + $this->assertStringContainsString('message_countable 6', $display); |
| 70 | + $this->assertStringContainsString('another_message_countable 6', $display); |
| 71 | + $this->assertStringContainsString('! [NOTE] Unable to get message count for the following transports: "simple".', $display); |
| 72 | + } |
| 73 | + |
| 74 | + public function testWithOneExistingMessageCountableTransport(): void |
| 75 | + { |
| 76 | + $tester = new CommandTester($this->command); |
| 77 | + $tester->execute(['transport_names' => ['message_countable']]); |
| 78 | + $display = $tester->getDisplay(); |
| 79 | + |
| 80 | + $this->assertStringNotContainsString('[WARNING] The "unexisting" transport does not exist.', $display); |
| 81 | + $this->assertStringContainsString('message_countable 6', $display); |
| 82 | + $this->assertStringNotContainsString('another_message_countable', $display); |
| 83 | + $this->assertStringNotContainsString(' ! [NOTE] Unable to get message count for the following transports: "simple".', $display); |
| 84 | + } |
| 85 | + |
| 86 | + public function testWithMultipleExistingMessageCountableTransport(): void |
| 87 | + { |
| 88 | + $tester = new CommandTester($this->command); |
| 89 | + $tester->execute(['transport_names' => ['message_countable', 'another_message_countable']]); |
| 90 | + $display = $tester->getDisplay(); |
| 91 | + |
| 92 | + $this->assertStringNotContainsString('[WARNING] The "unexisting" transport does not exist.', $display); |
| 93 | + $this->assertStringContainsString('message_countable 6', $display); |
| 94 | + $this->assertStringContainsString('another_message_countable 6', $display); |
| 95 | + $this->assertStringNotContainsString('! [NOTE] Unable to get message count for the following transports: "simple".', $display); |
| 96 | + } |
| 97 | + |
| 98 | + public function testWithNotMessageCountableTransport(): void |
| 99 | + { |
| 100 | + $tester = new CommandTester($this->command); |
| 101 | + $tester->execute(['transport_names' => ['simple']]); |
| 102 | + $display = $tester->getDisplay(); |
| 103 | + |
| 104 | + $this->assertStringNotContainsString('[WARNING] The "unexisting" transport does not exist.', $display); |
| 105 | + $this->assertStringNotContainsString('message_countable', $display); |
| 106 | + $this->assertStringNotContainsString('another_message_countable', $display); |
| 107 | + $this->assertStringContainsString('! [NOTE] Unable to get message count for the following transports: "simple".', $display); |
| 108 | + } |
| 109 | + |
| 110 | + public function testWithNotExistingTransport(): void |
| 111 | + { |
| 112 | + $tester = new CommandTester($this->command); |
| 113 | + $tester->execute(['transport_names' => ['unexisting']]); |
| 114 | + $display = $tester->getDisplay(); |
| 115 | + |
| 116 | + $this->assertStringContainsString('[WARNING] The "unexisting" transport does not exist.', $display); |
| 117 | + $this->assertStringNotContainsString('message_countable', $display); |
| 118 | + $this->assertStringNotContainsString('another_message_countable', $display); |
| 119 | + $this->assertStringNotContainsString('! [NOTE] Unable to get message count for the following transports: "simple".', $display); |
| 120 | + } |
| 121 | +} |
0 commit comments