8000 [Messenger][AmqpExt] Allow disabling the auto-setup of the connection by sroze · Pull Request #26956 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger][AmqpExt] Allow disabling the auto-setup of the connection #26956

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

Merged
merged 1 commit into from
Apr 17, 2018
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
11 changes: 8 additions & 3 deletions src/Symfony/Component/Messenger/Adapter/AmqpExt/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static function fromDsn(string $dsn, array $options = array(), bool $debu
*/
public function publish(string $body, array $headers = array()): void
{
if ($this->debug) {
if ($this->debug && $this->shouldSetup()) {
$this->setup();
}

Expand All @@ -108,7 +108,7 @@ public function publish(string $body, array $headers = array()): void
*/
public function get(): ?\AMQPEnvelope
{
if ($this->debug) {
if ($this->debug && $this->shouldSetup()) {
$this->setup();
}

Expand All @@ -117,7 +117,7 @@ public function get(): ?\AMQPEnvelope
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();

Expand Down Expand Up @@ -215,4 +215,9 @@ private function clear(): void
$this->amqpQueue = null;
$this->amqpExchange = null;
}

private function shouldSetup(): bool
{
return !array_key_exists('auto-setup', $this->connectionCredentials) || 'false' !== $this->connectionCredentials['auto-setup'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,42 @@ public function testItAllowsToUseAPersistentConnection()
$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
Expand Down
0