Skip to content

Commit

Permalink
Throw connection exceptions on batch publish (php-amqplib#983)
Browse files Browse the repository at this point in the history
* Move connection check to the top of publish_batch method

* Add unit tests for publish_batch method and opened/closed connection

* Add support for older PHPUnit versions

Co-authored-by: Aleksandar Jaksic <aleksandar.jaksic@check24.de>
  • Loading branch information
foment and Aleksandar Jaksic authored Mar 31, 2022
1 parent d4da907 commit 1ddee2c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
3 changes: 2 additions & 1 deletion PhpAmqpLib/Channel/AMQPChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,8 @@ public function publish_batch()
return;
}

$this->checkConnection();

/** @var AMQPWriter $pkt */
$pkt = new AMQPWriter();

Expand Down Expand Up @@ -1265,7 +1267,6 @@ public function publish_batch()
}
}

$this->checkConnection();
$this->connection->write($pkt->getvalue());
$this->batch_messages = array();
}
Expand Down
62 changes: 62 additions & 0 deletions tests/Unit/Channel/AMQPChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace PhpAmqpLib\Tests\Unit\Channel;

use PhpAmqpLib\Exception\AMQPChannelClosedException;
use PhpAmqpLib\Exception\AMQPConnectionBlockedException;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Tests\Unit\Test\BufferIO;
use PhpAmqpLib\Tests\Unit\Test\TestChannel;
use PhpAmqpLib\Tests\Unit\Test\TestConnection;
use PhpAmqpLib\Wire\AMQPWriter;
use PHPUnit\Framework\TestCase;

class AMQPChannelTest extends TestCase
Expand Down Expand Up @@ -37,6 +39,66 @@ public function basic_consume_invalid_arguments($arguments, $expectedException)
$channel->basic_consume(...$arguments);
}

/**
* @test
*/
public function publish_batch_failed_connection(): void
{
$connection = new TestConnection('user', 'pass', '/', false, 'PLAIN', null, '', new BufferIO());

$channel = new TestChannel($connection, 1);
$channel->close_connection();

$message = new AMQPMessage();
$channel->batch_basic_publish($message, 'exchange', 'routing_key');

$this->expectException(AMQPChannelClosedException::class);
$this->expectExceptionMessage('Channel connection is closed.');

$channel->publish_batch();
}

/**
* @test
*/
public function publish_batch_opened_connection(): void
{
$mock_builder = $this->getMockBuilder(TestConnection::class)
->disableOriginalConstructor();

$methods_to_mock = [
'prepare_content',
'prepare_channel_method_frame',
'write',
];

if (!method_exists($mock_builder, 'onlyMethods')) {
$mock_builder->setMethods($methods_to_mock);
} else {
$mock_builder->onlyMethods($methods_to_mock);
}

$connection_mock = $mock_builder->getMock();
$channel = new TestChannel($connection_mock, 1);

$message = new AMQPMessage();
$writer = new AMQPWriter();

$channel->batch_basic_publish($message, 'exchange', 'routing_key');

$connection_mock->expects(self::once())
->method('prepare_content');

$connection_mock->expects(self::once())
->method('prepare_channel_method_frame')
->willReturn($writer);

$connection_mock->expects(self::once())
->method('write');

$channel->publish_batch();
}

public function basic_consume_invalid_arguments_provider()
{
return [
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/Test/TestChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public function __construct($connection, $channel_id = null, $auto_decode = true
$this->auto_decode = $auto_decode;
$this->channel_rpc_timeout = $channel_rpc_timeout;
}

public function close_connection(): void {
$this->do_close();
}
}

0 comments on commit 1ddee2c

Please sign in to comment.