Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,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 @@ -418,6 +418,62 @@ 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')
->willReturnCallback(function (...$args) {
static $series = [
['exchange0', 'binding_key0', ['x-match' => 'all']],
['exchange0', 'binding_key1', ['x-match' => 'all']],
['exchange1', 'binding_key2', ['x-match' => 'any']],
['exchange1', 'binding_key3', ['x-match' => 'any']],
];

$expectedArgs = array_shift($series);
$this->assertSame($expectedArgs, $args);
})
;
$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 testNoBindingKeysInExchangeBindings()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The "binding_keys" option must be set to a non-empty array for exchange "exchange0".');

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

$dsn = 'amqp://localhost?exchange[type]=headers'.
'&exchange[bindings][exchange0][binding_arguments][x-match]=all';

$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 @@ -79,6 +79,7 @@ class Connection
'default_publish_routing_key',
'flags',
'arguments',
'bindings',
];

private AmqpFactory $amqpFactory;
Expand Down Expand Up @@ -140,6 +141,9 @@ public function __construct(
* * 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 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 @@ -460,6 +464,15 @@ private function setupExchangeAndQueues(): void
$exchange->declareExchange();
}

foreach ($this->exchangeOptions['bindings'] ?? [] as $exchangeName => $exchangeConfig) {
if (!\is_array($exchangeConfig['binding_keys'] ?? false) || !$exchangeConfig['binding_keys']) {
throw new InvalidArgumentException(\sprintf('The "binding_keys" option must be set to a non-empty array for exchange "%s".', $exchangeName));
}
foreach ($exchangeConfig['binding_keys'] as $bindingKey) {
$this->exchange()->bind($exchangeName, $bindingKey, $exchangeConfig['binding_arguments'] ?? []);
}
}

foreach ($this->queuesOptions as $queueName => $queueConfig) {
$this->queue($queueName)->declareQueue();
if ('' !== $this->exchangeOptions['name']) {
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 @@ -7,6 +7,7 @@ CHANGELOG
* Add `--exclude-receivers` option to the `messenger:consume command`
* Allow any `ServiceResetterInterface` implementation in `ResetServicesListener`
* Add `Symfony\Component\Messenger\Middleware\AddDefaultStampsMiddleware` and `Symfony\Component\Messenger\Message\DefaultStampsProviderInterface`
* Add the possibility to configure exchange to exchange bindings in AMQP transport

7.3
---
Expand Down
Loading