From 2d439c59b4f63c57096bb477c641fd28020797c1 Mon Sep 17 00:00:00 2001 From: TiMESPLiNTER Date: Thu, 10 Oct 2019 08:47:47 +0200 Subject: [PATCH 1/5] Add header support --- pkg/enqueue/Symfony/Client/ProduceCommand.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/enqueue/Symfony/Client/ProduceCommand.php b/pkg/enqueue/Symfony/Client/ProduceCommand.php index 61be6a87d..e96735ec3 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 = $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) { From 5bfc1290acbc3a98255fa7985ccfd371ee583b87 Mon Sep 17 00:00:00 2001 From: TiMESPLiNTER Date: Thu, 10 Oct 2019 15:42:26 +0200 Subject: [PATCH 2/5] Add tests --- .../Symfony/Client/ProduceCommandTest.php | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/pkg/enqueue/Tests/Symfony/Client/ProduceCommandTest.php b/pkg/enqueue/Tests/Symfony/Client/ProduceCommandTest.php index 8e9b750a3..f6aea3656 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; @@ -112,11 +113,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,18 +133,22 @@ public function testShouldSendEventToDefaultTransport() $tester = new CommandTester($command); $tester->execute([ - 'message' => 'theMessage', + 'message' => $payload, + '--header' => $header, '--topic' => 'theTopic', ]); } public function testShouldSendCommandToDefaultTransport() { + $header = 'Content-Type: text/plain'; + $payload = 'theMessage'; + $producerMock = $this->createProducerMock(); $producerMock ->expects($this->once()) ->method('sendCommand') - ->with('theCommand', 'theMessage') + ->with('theCommand', new Message($payload, [], [$header])) ; $producerMock ->expects($this->never()) @@ -153,13 +161,17 @@ public function testShouldSendCommandToDefaultTransport() $tester = new CommandTester($command); $tester->execute([ - 'message' => 'theMessage', + 'message' => $payload, + '--header' => $header, '--command' => 'theCommand', ]); } public function testShouldSendEventToFooTransport() { + $header = 'Content-Type: text/plain'; + $payload = 'theMessage'; + $defaultProducerMock = $this->createProducerMock(); $defaultProducerMock ->expects($this->never()) @@ -174,7 +186,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 +200,8 @@ public function testShouldSendEventToFooTransport() $tester = new CommandTester($command); $tester->execute([ - 'message' => 'theMessage', + 'message' => $payload, + '--header' => $header, '--topic' => 'theTopic', '--client' => 'foo', ]); From 43713009334a4ce94803dd092e1da9d9c60fdd49 Mon Sep 17 00:00:00 2001 From: TiMESPLiNTER Date: Thu, 10 Oct 2019 15:59:29 +0200 Subject: [PATCH 3/5] Fix tests --- pkg/enqueue/Symfony/Client/ProduceCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/enqueue/Symfony/Client/ProduceCommand.php b/pkg/enqueue/Symfony/Client/ProduceCommand.php index e96735ec3..51a365967 100644 --- a/pkg/enqueue/Symfony/Client/ProduceCommand.php +++ b/pkg/enqueue/Symfony/Client/ProduceCommand.php @@ -57,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int $topic = $input->getOption('topic'); $command = $input->getOption('command'); $message = $input->getArgument('message'); - $headers = $input->getOption('header'); + $headers = (array) $input->getOption('header'); $client = $input->getOption('client'); if ($topic && $command) { From fc36bdcdf359e580d3fe0fd1f0eab4d776cd2cc0 Mon Sep 17 00:00:00 2001 From: TiMESPLiNTER Date: Thu, 10 Oct 2019 16:11:14 +0200 Subject: [PATCH 4/5] Fix tests --- .../Tests/Symfony/Client/ProduceCommandTest.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pkg/enqueue/Tests/Symfony/Client/ProduceCommandTest.php b/pkg/enqueue/Tests/Symfony/Client/ProduceCommandTest.php index f6aea3656..7c50ae02c 100644 --- a/pkg/enqueue/Tests/Symfony/Client/ProduceCommandTest.php +++ b/pkg/enqueue/Tests/Symfony/Client/ProduceCommandTest.php @@ -43,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() @@ -141,14 +142,11 @@ public function testShouldSendEventToDefaultTransport() public function testShouldSendCommandToDefaultTransport() { - $header = 'Content-Type: text/plain'; - $payload = 'theMessage'; - $producerMock = $this->createProducerMock(); $producerMock ->expects($this->once()) ->method('sendCommand') - ->with('theCommand', new Message($payload, [], [$header])) + ->with('theCommand', 'theMessage') ; $producerMock ->expects($this->never()) @@ -161,8 +159,7 @@ public function testShouldSendCommandToDefaultTransport() $tester = new CommandTester($command); $tester->execute([ - 'message' => $payload, - '--header' => $header, + 'message' => 'theMessage', '--command' => 'theCommand', ]); } From 288e90e43b8f60ef745ba82902030a63bfc3a050 Mon Sep 17 00:00:00 2001 From: TiMESPLiNTER Date: Thu, 10 Oct 2019 16:13:20 +0200 Subject: [PATCH 5/5] Fix tests --- pkg/enqueue/Tests/Symfony/Client/SimpleProduceCommandTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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()