diff --git a/pkg/enqueue/Symfony/Client/ProduceCommand.php b/pkg/enqueue/Symfony/Client/ProduceCommand.php index 61be6a87d..51a365967 100644 --- a/pkg/enqueue/Symfony/Client/ProduceCommand.php +++ b/pkg/enqueue/Symfony/Client/ProduceCommand.php @@ -2,6 +2,7 @@ namespace Enqueue\Symfony\Client; +use Enqueue\Client\Message; use Enqueue\Client\ProducerInterface; use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; @@ -44,6 +45,7 @@ protected function configure(): void $this ->setDescription('Sends an event to the topic') ->addArgument('message', InputArgument::REQUIRED, 'A message') + ->addOption('header', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The message headers') ->addOption('client', 'c', InputOption::VALUE_OPTIONAL, 'The client to consume messages from.', $this->defaultClient) ->addOption('topic', null, InputOption::VALUE_OPTIONAL, 'The topic to send a message to') ->addOption('command', null, InputOption::VALUE_OPTIONAL, 'The command to send a message to') @@ -55,6 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int $topic = $input->getOption('topic'); $command = $input->getOption('command'); $message = $input->getArgument('message'); + $headers = (array) $input->getOption('header'); $client = $input->getOption('client'); if ($topic && $command) { @@ -68,7 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int } if ($topic) { - $producer->sendEvent($topic, $message); + $producer->sendEvent($topic, new Message($message, [], $headers)); $output->writeln('An event is sent'); } elseif ($command) { diff --git a/pkg/enqueue/Tests/Symfony/Client/ProduceCommandTest.php b/pkg/enqueue/Tests/Symfony/Client/ProduceCommandTest.php index 8e9b750a3..7c50ae02c 100644 --- a/pkg/enqueue/Tests/Symfony/Client/ProduceCommandTest.php +++ b/pkg/enqueue/Tests/Symfony/Client/ProduceCommandTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Tests\Symfony\Client; +use Enqueue\Client\Message; use Enqueue\Client\ProducerInterface; use Enqueue\Container\Container; use Enqueue\Symfony\Client\ProduceCommand; @@ -42,10 +43,11 @@ public function testShouldHaveExpectedOptions() $command = new ProduceCommand($this->createMock(ContainerInterface::class), 'default'); $options = $command->getDefinition()->getOptions(); - $this->assertCount(3, $options); + $this->assertCount(4, $options); $this->assertArrayHasKey('client', $options); $this->assertArrayHasKey('topic', $options); $this->assertArrayHasKey('command', $options); + $this->assertArrayHasKey('header', $options); } public function testShouldHaveExpectedAttributes() @@ -112,11 +114,14 @@ public function testThrowIfBothTopicAndCommandOptionsAreSet() public function testShouldSendEventToDefaultTransport() { + $header = 'Content-Type: text/plain'; + $payload = 'theMessage'; + $producerMock = $this->createProducerMock(); $producerMock ->expects($this->once()) ->method('sendEvent') - ->with('theTopic', 'theMessage') + ->with('theTopic', new Message($payload, [], [$header])) ; $producerMock ->expects($this->never()) @@ -129,7 +134,8 @@ public function testShouldSendEventToDefaultTransport() $tester = new CommandTester($command); $tester->execute([ - 'message' => 'theMessage', + 'message' => $payload, + '--header' => $header, '--topic' => 'theTopic', ]); } @@ -160,6 +166,9 @@ public function testShouldSendCommandToDefaultTransport() public function testShouldSendEventToFooTransport() { + $header = 'Content-Type: text/plain'; + $payload = 'theMessage'; + $defaultProducerMock = $this->createProducerMock(); $defaultProducerMock ->expects($this->never()) @@ -174,7 +183,7 @@ public function testShouldSendEventToFooTransport() $fooProducerMock ->expects($this->once()) ->method('sendEvent') - ->with('theTopic', 'theMessage') + ->with('theTopic', new Message($payload, [], [$header])) ; $fooProducerMock ->expects($this->never()) @@ -188,7 +197,8 @@ public function testShouldSendEventToFooTransport() $tester = new CommandTester($command); $tester->execute([ - 'message' => 'theMessage', + 'message' => $payload, + '--header' => $header, '--topic' => 'theTopic', '--client' => 'foo', ]); diff --git a/pkg/enqueue/Tests/Symfony/Client/SimpleProduceCommandTest.php b/pkg/enqueue/Tests/Symfony/Client/SimpleProduceCommandTest.php index 82a7e0089..84391cd3e 100644 --- a/pkg/enqueue/Tests/Symfony/Client/SimpleProduceCommandTest.php +++ b/pkg/enqueue/Tests/Symfony/Client/SimpleProduceCommandTest.php @@ -40,10 +40,11 @@ public function testShouldHaveExpectedOptions() $command = new SimpleProduceCommand($this->createProducerMock()); $options = $command->getDefinition()->getOptions(); - $this->assertCount(3, $options); + $this->assertCount(4, $options); $this->assertArrayHasKey('client', $options); $this->assertArrayHasKey('topic', $options); $this->assertArrayHasKey('command', $options); + $this->assertArrayHasKey('header', $options); } public function testShouldHaveExpectedAttributes()