Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add header support for Symfony's produce command #965

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/enqueue/Symfony/Client/ProduceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Enqueue\Symfony\Client;

use Enqueue\Client\Message;
use Enqueue\Client\ProducerInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
Expand Down Expand Up @@ -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')
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
20 changes: 15 additions & 5 deletions pkg/enqueue/Tests/Symfony/Client/ProduceCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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())
Expand All @@ -129,7 +134,8 @@ public function testShouldSendEventToDefaultTransport()

$tester = new CommandTester($command);
$tester->execute([
'message' => 'theMessage',
'message' => $payload,
'--header' => $header,
'--topic' => 'theTopic',
]);
}
Expand Down Expand Up @@ -160,6 +166,9 @@ public function testShouldSendCommandToDefaultTransport()

public function testShouldSendEventToFooTransport()
{
$header = 'Content-Type: text/plain';
$payload = 'theMessage';

$defaultProducerMock = $this->createProducerMock();
$defaultProducerMock
->expects($this->never())
Expand All @@ -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())
Expand All @@ -188,7 +197,8 @@ public function testShouldSendEventToFooTransport()

$tester = new CommandTester($command);
$tester->execute([
'message' => 'theMessage',
'message' => $payload,
'--header' => $header,
'--topic' => 'theTopic',
'--client' => 'foo',
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down