bug #26956 [Messenger][AmqpExt] Allow disabling the auto-setup of the connection (sroze)
This PR was squashed before being merged into the 4.1-dev branch (closes #26956).
Discussion
----------
[Messenger][AmqpExt] Allow disabling the auto-setup of the connection
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #26937
| License | MIT
| Doc PR | ø
Allow disabling the auto-setup of the AMQP connection. Especially useful when the messages are published to another system (see #26937)
Commits
-------
b3039faa1a
[Messenger][AmqpExt] Allow disabling the auto-setup of the connection
This commit is contained in:
commit
4429c9b20f
@ -94,7 +94,7 @@ class Connection
|
||||
*/
|
||||
public function publish(string $body, array $headers = array()): void
|
||||
{
|
||||
if ($this->debug) {
|
||||
if ($this->debug && $this->shouldSetup()) {
|
||||
$this->setup();
|
||||
}
|
||||
|
||||
@ -108,7 +108,7 @@ class Connection
|
||||
*/
|
||||
public function get(): ?\AMQPEnvelope
|
||||
{
|
||||
if ($this->debug) {
|
||||
if ($this->debug && $this->shouldSetup()) {
|
||||
$this->setup();
|
||||
}
|
||||
|
||||
@ -117,7 +117,7 @@ class Connection
|
||||
return $message;
|
||||
}
|
||||
} catch (\AMQPQueueException $e) {
|
||||
if (404 === $e->getCode()) {
|
||||
if (404 === $e->getCode() && $this->shouldSetup()) {
|
||||
// If we get a 404 for the queue, it means we need to setup the exchange & queue.
|
||||
$this->setup();
|
||||
|
||||
@ -215,4 +215,9 @@ class Connection
|
||||
$this->amqpQueue = null;
|
||||
$this->amqpExchange = null;
|
||||
}
|
||||
|
||||
private function shouldSetup(): bool
|
||||
{
|
||||
return !array_key_exists('auto-setup', $this->connectionCredentials) || 'false' !== $this->connectionCredentials['auto-setup'];
|
||||
}
|
||||
}
|
||||
|
@ -147,6 +147,42 @@ class ConnectionTest extends TestCase
|
||||
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?persistent=true', array(), false, $factory);
|
||||
$connection->publish('body');
|
||||
}
|
||||
|
||||
public function testItSetupsTheConnectionWhenDebug()
|
||||
{
|
||||
$factory = new TestAmqpFactory(
|
||||
$amqpConnection = $this->getMockBuilder(\AMQPConnection::class)->disableOriginalConstructor()->getMock(),
|
||||
$amqpChannel = $this->getMockBuilder(\AMQPChannel::class)->disableOriginalConstructor()->getMock(),
|
||||
$amqpQueue = $this->getMockBuilder(\AMQPQueue::class)->disableOriginalConstructor()->getMock(),
|
||||
$amqpExchange = $this->getMockBuilder(\AMQPExchange::class)->disableOriginalConstructor()->getMock()
|
||||
);
|
||||
|
||||
$amqpExchange->method('getName')->willReturn('exchange_name');
|
||||
$amqpExchange->expects($this->once())->method('declareExchange');
|
||||
$amqpQueue->expects($this->once())->method('declareQueue');
|
||||
$amqpQueue->expects($this->once())->method('bind')->with('exchange_name', 'my_key');
|
||||
|
||||
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?queue[routing_key]=my_key', array(), true, $factory);
|
||||
$connection->publish('body');
|
||||
}
|
||||
|
||||
public function testItCanDisableTheSetup()
|
||||
{
|
||||
$factory = new TestAmqpFactory(
|
||||
$amqpConnection = $this->getMockBuilder(\AMQPConnection::class)->disableOriginalConstructor()->getMock(),
|
||||
$amqpChannel = $this->getMockBuilder(\AMQPChannel::class)->disableOriginalConstructor()->getMock(),
|
||||
$amqpQueue = $this->getMockBuilder(\AMQPQueue::class)->disableOriginalConstructor()->getMock(),
|
||||
$amqpExchange = $this->getMockBuilder(\AMQPExchange::class)->disableOriginalConstructor()->getMock()
|
||||
);
|
||||
|
||||
$amqpExchange->method('getName')->willReturn('exchange_name');
|
||||
$amqpExchange->expects($this->never())->method('declareExchange');
|
||||
$amqpQueue->expects($this->never())->method('declareQueue');
|
||||
$amqpQueue->expects($this->never())->method('bind');
|
||||
|
||||
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?queue[routing_key]=my_key', array('auto-setup' => 'false'), true, $factory);
|
||||
$connection->publish('body');
|
||||
}
|
||||
}
|
||||
|
||||
class TestAmqpFactory extends AmqpFactory
|
||||
|
Reference in New Issue
Block a user