1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
<?php
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\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class AMQPChannelTest extends TestCase
{
#[Test]
public function blocked_connection_exception_on_publish()
{
$this->expectException(AMQPConnectionBlockedException::class);
$connection = new TestConnection('user', 'pass', '/', false, 'PLAIN', null, '', new BufferIO());
$connection->setIsBlocked(true);
$channel = new TestChannel($connection, 1);
$channel->basic_publish(new AMQPMessage());
}
/**
* @param mixed[] $arguments
* @param string $expectedException
*/
#[DataProvider('basic_consume_invalid_arguments_provider')]
#[Test]
public function basic_consume_invalid_arguments($arguments, $expectedException)
{
$this->expectException($expectedException);
$connection = new TestConnection('user', 'pass', '/', false, 'PLAIN', null, '', new BufferIO());
$channel = new TestChannel($connection, 1);
$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();
}
#[Test]
public function close_if_disconnected_false(): void
{
$mockBuilder = $this->getMockBuilder(TestConnection::class)
->disableOriginalConstructor();
$methodsToMock = [
'isConnected',
];
if (!method_exists($mockBuilder, 'onlyMethods')) {
$mockBuilder->setMethods($methodsToMock);
} else {
$mockBuilder->onlyMethods($methodsToMock);
}
/** @var MockObject&TestConnection $connectionMock */
$connectionMock = $mockBuilder->getMock();
$channel = new TestChannel($connectionMock, 1);
$connectionMock->expects(self::once())
->method('isConnected')
->willReturn(true);
// Should return `false` because $this->connection->isConnected()
$firstResult = $channel->closeIfDisconnected();
$this->assertFalse($firstResult);
}
#[Test]
public function close_if_disconnected_true(): void
{
$mockBuilder = $this->getMockBuilder(TestConnection::class)
->disableOriginalConstructor();
$methodsToMock = [
'isConnected',
];
if (!method_exists($mockBuilder, 'onlyMethods')) {
$mockBuilder->setMethods($methodsToMock);
} else {
$mockBuilder->onlyMethods($methodsToMock);
}
/** @var MockObject&TestConnection $connectionMock */
$connectionMock = $mockBuilder->getMock();
$channel = new TestChannel($connectionMock, 1);
$connectionMock->expects(self::once())
->method('isConnected')
->willReturn(false);
// Should return true
$firstResult = $channel->closeIfDisconnected();
$this->assertTrue($firstResult);
// Should return `false` because no $this->connection
$secondResult = $channel->closeIfDisconnected();
$this->assertFalse($secondResult);
}
public static function basic_consume_invalid_arguments_provider()
{
return [
[
[
'',
'',
false,
false,
false,
false,
'non_callable variable',
],
\InvalidArgumentException::class,
],
[
[
'',
'',
false,
false,
false,
true,
'sleep',
],
\InvalidArgumentException::class,
]
];
}
}
|