Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1bf2589

Browse files
committed
Allow to disable the auto-setup of the connection
1 parent 507989d commit 1bf2589

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/Symfony/Component/Messenger/Adapter/AmqpExt/Connection.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static function fromDsn(string $dsn, array $options = array(), bool $debu
9494
*/
9595
public function publish(string $body, array $headers = array()): void
9696
{
97-
if ($this->debug) {
97+
if ($this->debug && $this->shouldSetup()) {
9898
$this->setup();
9999
}
100100

@@ -108,7 +108,7 @@ public function publish(string $body, array $headers = array()): void
108108
*/
109109
public function get(): ?\AMQPEnvelope
110110
{
111-
if ($this->debug) {
111+
if ($this->debug && $this->shouldSetup()) {
112112
$this->setup();
113113
}
114114

@@ -117,7 +117,7 @@ public function get(): ?\AMQPEnvelope
117117
return $message;
118118
}
119119
} catch (\AMQPQueueException $e) {
120-
if (404 === $e->getCode()) {
120+
if (404 === $e->getCode() && $this->shouldSetup()) {
121121
// If we get a 404 for the queue, it means we need to setup the exchange & queue.
122122
$this->setup();
123123

@@ -215,4 +215,9 @@ private function clear(): void
215215
$this->amqpQueue = null;
216216
$this->amqpExchange = null;
217217
}
218+
219+
private function shouldSetup(): bool
220+
{
221+
return array_key_exists('auto-setup', $this->connectionCredentials) ? 'false' !== $this->connectionCredentials['auto-setup'] : true;
222+
}
218223
}

src/Symfony/Component/Messenger/Tests/Adapter/AmqpExt/ConnectionTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,42 @@ public function testItAllowsToUseAPersistentConnection()
147147
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?persistent=true', array(), false, $factory);
148148
$connection->publish('body');
149149
}
150+
151+
public function testItSetupsTheConnectionWhenDebug()
152+
{
153+
$factory = new TestAmqpFactory(
154+
$amqpConnection = $this->getMockBuilder(\AMQPConnection::class)->disableOriginalConstructor()->getMock(),
155+
$amqpChannel = $this->getMockBuilder(\AMQPChannel::class)->disableOriginalConstructor()->getMock(),
156+
$amqpQueue = $this->getMockBuilder(\AMQPQueue::class)->disableOriginalConstructor()->getMock(),
157+
$amqpExchange = $this->getMockBuilder(\AMQPExchange::class)->disableOriginalConstructor()->getMock()
158+
);
159+
160+
$amqpExchange->method('getName')->willReturn('exchange_name');
161+
$amqpExchange->expects($this->once())->method('declareExchange');
162+
$amqpQueue->expects($this->once())->method('declareQueue');
163+
$amqpQueue->expects($this->once())->method('bind')->with('exchange_name', 'my_key');
164+
165+
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?queue[routing_key]=my_key', array(), true, $factory);
166+
$connection->publish('body');
167+
}
168+
169+
public function testItCanDisableTheSetup()
170+
{
171+
$factory = new TestAmqpFactory(
172+
$amqpConnection = $this->getMockBuilder(\AMQPConnection::class)->disableOriginalConstructor()->getMock(),
173+
$amqpChannel = $this->getMockBuilder(\AMQPChannel::class)->disableOriginalConstructor()->getMock(),
174+
$amqpQueue = $this->getMockBuilder(\AMQPQueue::class)->disableOriginalConstructor()->getMock(),
175+
$amqpExchange = $this->getMockBuilder(\AMQPExchange::class)->disableOriginalConstructor()->getMock()
176+
);
177+
178+
$amqpExchange->method('getName')->willReturn('exchange_name');
179+
$amqpExchange->expects($this->never())->method('declareExchange');
180+
$amqpQueue->expects($this->never())->method('declareQueue');
181+
$amqpQueue->expects($this->never())->method('bind');
182+
183+
$connection = Connection::fromDsn('amqp://localhost/%2f/messages?queue[routing_key]=my_key', array('auto-setup' => 'false'), true, $factory);
184+
$connection->publish('body');
185+
}
150186
}
151187

152188
class TestAmqpFactory extends AmqpFactory

0 commit comments

Comments
 (0)