Skip to content

Commit d03f83b

Browse files
authored
Merge pull request php-amqplib#701 from ramunasd/fix_call_connection_null
fix call to a member function o null when connection was closed
2 parents 982923b + cc089c0 commit d03f83b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

PhpAmqpLib/Channel/AMQPChannel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace PhpAmqpLib\Channel;
33

44
use PhpAmqpLib\Exception\AMQPBasicCancelException;
5+
use PhpAmqpLib\Exception\AMQPChannelClosedException;
56
use PhpAmqpLib\Exception\AMQPConnectionClosedException;
67
use PhpAmqpLib\Exception\AMQPProtocolChannelException;
78
use PhpAmqpLib\Exception\AMQPRuntimeException;
@@ -1156,6 +1157,9 @@ public function basic_publish(
11561157
$immediate = false,
11571158
$ticket = null
11581159
) {
1160+
if ($this->connection === null) {
1161+
throw new AMQPChannelClosedException('Channel connection is closed.');
1162+
}
11591163
$pkt = new AMQPWriter();
11601164
$pkt->write($this->pre_publish($exchange, $routing_key, $mandatory, $immediate, $ticket));
11611165

tests/Functional/Connection/ConnectionClosedTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,18 @@ public function must_throw_exception_broken_pipe_write($type)
104104
} catch (\Exception $exception) {
105105
}
106106

107-
$this->assertInstanceOf('Exception', $exception);
108107
$this->assertInstanceOf('PhpAmqpLib\Exception\AMQPConnectionClosedException', $exception);
109108
$this->assertEquals(SOCKET_EPIPE, $exception->getCode());
110109
$this->assertChannelClosed($channel);
111110
$this->assertConnectionClosed($connection);
111+
112+
// 2nd publish call must return exception instantly cause connection is already closed
113+
$exception = null;
114+
try {
115+
$channel->basic_publish($message, $exchange_name, $queue_name);
116+
} catch (\Exception $exception) {
117+
}
118+
$this->assertInstanceOf('PhpAmqpLib\Exception\AMQPChannelClosedException', $exception);
112119
}
113120

114121
/**

0 commit comments

Comments
 (0)