Skip to content

[Messenger] Add AMQP exchange to exchange bindings #46257

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public function testItSetupsTheTTLConnection()
$connection->publish('body');
}

public function testBindingArguments()
public function testQueueBindingArguments()
{
$amqpConnection = $this->createMock(\AMQPConnection::class);
$amqpChannel = $this->createMock(\AMQPChannel::class);
Expand All @@ -383,6 +383,36 @@ public function testBindingArguments()
$connection->publish('body');
}

public function testExchangeBindingArguments()
{
$factory = new TestAmqpFactory(
$this->createMock(\AMQPConnection::class),
$this->createMock(\AMQPChannel::class),
$this->createMock(\AMQPQueue::class),
$amqpExchange = $this->createMock(\AMQPExchange::class)
);

$amqpExchange->expects($this->once())->method('declareExchange');
$amqpExchange->expects($this->exactly(4))->method('bind')->withConsecutive(
['exchange0', 'binding_key0', ['x-match' => 'all']],
['exchange0', 'binding_key1', ['x-match' => 'all']],
['exchange1', 'binding_key2', ['x-match' => 'any']],
['exchange1', 'binding_key3', ['x-match' => 'any']],
);
$amqpExchange->expects($this->once())->method('publish')->with('body', null, \AMQP_NOPARAM, ['headers' => [], 'delivery_mode' => 2, 'timestamp' => time()]);

$dsn = 'amqp://localhost?exchange[type]=headers'.
'&exchange[bindings][exchange0][binding_arguments][x-match]=all'.
'&exchange[bindings][exchange0][binding_keys][0]=binding_key0'.
'&exchange[bindings][exchange0][binding_keys][1]=binding_key1'.
'&exchange[bindings][exchange1][binding_arguments][x-match]=any'.
'&exchange[bindings][exchange1][binding_keys][0]=binding_key2'.
'&exchange[bindings][exchange1][binding_keys][1]=binding_key3';

$connection = Connection::fromDsn($dsn, [], $factory);
$connection->publish('body');
}

public function testItCanDisableTheSetup()
{
$factory = new TestAmqpFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Connection
'default_publish_routing_key',
'flags',
'arguments',
'bindings',
];

private array $connectionOptions;
Expand Down Expand Up @@ -139,6 +140,9 @@ public function __construct(array $connectionOptions, array $exchangeOptions, ar
* * default_publish_routing_key: Routing key to use when publishing, if none is specified on the message
* * flags: Exchange flags (Default: AMQP_DURABLE)
* * arguments: Extra arguments
* * bindings[name]: An array of the source exchanges to bind this exchange to, keyed by the name. Binding direction: source exchange -> this exchange
* * binding_keys: The binding/routing keys (if any) to be used for the binding
* * binding_arguments: Additional binding arguments
* * delay:
* * queue_name_pattern: Pattern to use to create the queues (Default: "delay_%exchange_name%_%routing_key%_%delay%")
* * exchange_name: Name of the exchange to be used for the delayed/retried messages (Default: "delays")
Expand Down Expand Up @@ -455,6 +459,12 @@ private function setupExchangeAndQueues(): void
{
$this->exchange()->declareExchange();

foreach ($this->exchangeOptions['bindings'] ?? [] as $exchangeName => $exchangeConfig) {
foreach ($exchangeConfig['binding_keys'] ?? [] as $bindingKey) {
$this->exchange()->bind($exchangeName, $bindingKey, $exchangeConfig['binding_arguments'] ?? []);
}
}

foreach ($this->queuesOptions as $queueName => $queueConfig) {
$this->queue($queueName)->declareQueue();
foreach ($queueConfig['binding_keys'] ?? [null] as $bindingKey) {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
`Symfony\Component\Messenger\Transport\InMemoryTransportFactory` in favor of
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport` and
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory`
* Add possibility to configure exchange to exchange bindings in AMQP transport

6.2
---
Expand Down