diff --git a/tests/Functional/AbstractPublishConsumeTest.php b/tests/Functional/AbstractPublishConsumeTest.php deleted file mode 100644 index 2278f6b96..000000000 --- a/tests/Functional/AbstractPublishConsumeTest.php +++ /dev/null @@ -1,134 +0,0 @@ -connection = $this->createConnection(); - $this->channel = $this->connection->channel(); - - $this->channel->exchange_declare($this->exchangeName, 'direct', false, false, false); - list($this->queueName, ,) = $this->channel->queue_declare(); - $this->channel->queue_bind($this->queueName, $this->exchangeName, $this->queueName); - } - - abstract protected function createConnection(); - - public function testPublishConsume() - { - $this->msgBody = 'foo bar baz äëïöü'; - - $msg = new AMQPMessage($this->msgBody, array( - 'content_type' => 'text/plain', - 'delivery_mode' => AMQPMessage::DELIVERY_MODE_NON_PERSISTENT, - 'correlation_id' => 'my_correlation_id', - 'reply_to' => 'my_reply_to' - )); - - $this->channel->basic_publish($msg, $this->exchangeName, $this->queueName); - - $this->channel->basic_consume( - $this->queueName, - getmypid(), - false, - false, - false, - false, - array($this, 'process_msg') - ); - - while (count($this->channel->callbacks)) { - $this->channel->wait(); - } - } - - public function testPublishPacketSizeLongMessage() - { - // Connection frame_max; - $frame_max = 131072; - - // Publish 3 messages with sizes: packet size - 1, equal packet size and packet size + 1 - for ($length = $frame_max - 9; $length <= $frame_max - 7; $length++) { - $this->msgBody = str_repeat('1', $length); - - $msg = new AMQPMessage($this->msgBody, array( - 'content_type' => 'text/plain', - 'delivery_mode' => AMQPMessage::DELIVERY_MODE_NON_PERSISTENT, - 'correlation_id' => 'my_correlation_id', - 'reply_to' => 'my_reply_to' - )); - - $this->channel->basic_publish($msg, $this->exchangeName, $this->queueName); - } - } - - public function process_msg($msg) - { - $delivery_info = $msg->delivery_info; - - $delivery_info['channel']->basic_ack($delivery_info['delivery_tag']); - $delivery_info['channel']->basic_cancel($delivery_info['consumer_tag']); - - $this->assertEquals($this->msgBody, $msg->body); - - //delivery tests - $this->assertEquals(getmypid(), $delivery_info['consumer_tag']); - $this->assertEquals($this->queueName, $delivery_info['routing_key']); - $this->assertEquals($this->exchangeName, $delivery_info['exchange']); - $this->assertEquals(false, $delivery_info['redelivered']); - - //msg property tests - $this->assertEquals('text/plain', $msg->get('content_type')); - $this->assertEquals('my_correlation_id', $msg->get('correlation_id')); - $this->assertEquals('my_reply_to', $msg->get('reply_to')); - - $this->setExpectedException('OutOfBoundsException'); - $msg->get('no_property'); - } - - public function tearDown() - { - if ($this->channel) { - $this->channel->exchange_delete($this->exchangeName); - $this->channel->queue_delete($this->queueName); - $this->channel->close(); - } - - if ($this->connection) { - $this->connection->close(); - } - } -} diff --git a/tests/Functional/Bug256Test.php b/tests/Functional/Bug/Bug256Test.php similarity index 81% rename from tests/Functional/Bug256Test.php rename to tests/Functional/Bug/Bug256Test.php index 50a0bc694..27aa48640 100644 --- a/tests/Functional/Bug256Test.php +++ b/tests/Functional/Bug/Bug256Test.php @@ -1,6 +1,6 @@ channel2->queue_bind($this->queueName, $this->exchangeName, $this->queueName); } - public function testFrameOrder() + public function tearDown() + { + if ($this->channel) { + $this->channel->exchange_delete($this->exchangeName); + $this->channel->close(); + } + if ($this->connection) { + $this->connection->close(); + } + if ($this->channel2) { + $this->channel2->close(); + } + if ($this->connection2) { + $this->connection2->close(); + } + } + + /** + * @test + */ + public function frame_order() { $msg = new AMQPMessage(''); - $hdrs = new AMQPTable(array('x-foo' => 'bar')); + $hdrs = new AMQPTable(['x-foo' => 'bar']); $msg->set('application_headers', $hdrs); for ($i = 0; $i < $this->messageCount; $i++) { @@ -83,7 +79,7 @@ public function testFrameOrder() true, false, false, - array($this, 'processMessage') + [$this, 'processMessage'] ); while (count($this->channel2->callbacks)) { @@ -95,31 +91,11 @@ public function processMessage(AMQPMessage $message) { $this->consumedCount++; - $this->assertEquals(array('x-foo' => 'bar'), $message->get('application_headers')->getNativeData()); + $this->assertEquals(['x-foo' => 'bar'], $message->get('application_headers')->getNativeData()); if ($this->consumedCount >= $this->messageCount) { $delivery_info = $message->delivery_info; $delivery_info['channel']->basic_cancel($delivery_info['consumer_tag']); } } - - public function tearDown() - { - if ($this->channel) { - $this->channel->exchange_delete($this->exchangeName); - $this->channel->close(); - } - - if ($this->connection) { - $this->connection->close(); - } - - if ($this->channel2) { - $this->channel2->close(); - } - - if ($this->connection2) { - $this->connection2->close(); - } - } } diff --git a/tests/Functional/Bug40Test.php b/tests/Functional/Bug/Bug40Test.php similarity index 86% rename from tests/Functional/Bug40Test.php rename to tests/Functional/Bug/Bug40Test.php index c3616892f..b94a3a14f 100644 --- a/tests/Functional/Bug40Test.php +++ b/tests/Functional/Bug/Bug40Test.php @@ -1,6 +1,6 @@ channel->queue_bind($this->queueName2, $this->exchangeName, $this->queueName2); } - public function testFrameOrder() + public function tearDown() + { + if ($this->channel) { + $this->channel->exchange_delete($this->exchangeName); + $this->channel->close(); + } + if ($this->connection) { + $this->connection->close(); + } + } + + /** + * @test + */ + public function frame_order() { $msg = new AMQPMessage('test message'); $this->channel->basic_publish($msg, $this->exchangeName, $this->queueName1); @@ -71,7 +64,7 @@ public function testFrameOrder() true, false, false, - array($this, 'processMessage1') + [$this, 'processMessage1'] ); while (count($this->channel->callbacks)) { @@ -92,7 +85,7 @@ public function processMessage1($msg) true, false, false, - array($this, 'processMessage2') + [$this, 'processMessage2'] ); } @@ -103,7 +96,6 @@ public function processMessage1($msg) if ($this->queue1Messages == 2) { $delivery_info['channel']->basic_cancel($delivery_info['consumer_tag']); } - } public function processMessage2($msg) @@ -111,16 +103,4 @@ public function processMessage2($msg) $delivery_info = $msg->delivery_info; $delivery_info['channel']->basic_cancel($delivery_info['consumer_tag']); } - - public function tearDown() - { - if ($this->channel) { - $this->channel->exchange_delete($this->exchangeName); - $this->channel->close(); - } - - if ($this->connection) { - $this->connection->close(); - } - } } diff --git a/tests/Functional/Bug49Test.php b/tests/Functional/Bug/Bug49Test.php similarity index 86% rename from tests/Functional/Bug49Test.php rename to tests/Functional/Bug/Bug49Test.php index 685996ea0..f3ef8dc00 100644 --- a/tests/Functional/Bug49Test.php +++ b/tests/Functional/Bug/Bug49Test.php @@ -1,6 +1,6 @@ channel2 = $this->connection->channel(); } - public function testDeclaration() + public function tearDown() + { + if ($this->channel2) { + $this->channel2->close(); + } + if ($this->connection) { + $this->connection->close(); + } + } + + /** + * @test + */ + public function declaration() { try { $this->channel->queue_declare('pretty.queue', true, true); @@ -45,15 +49,4 @@ public function testDeclaration() } } } - - public function tearDown() - { - if ($this->channel2) { - $this->channel2->close(); - } - - if ($this->connection) { - $this->connection->close(); - } - } } diff --git a/tests/Functional/Channel/ChannelTestCase.php b/tests/Functional/Channel/ChannelTestCase.php new file mode 100644 index 000000000..40f91112d --- /dev/null +++ b/tests/Functional/Channel/ChannelTestCase.php @@ -0,0 +1,45 @@ +connection = new AMQPSocketConnection(HOST, PORT, USER, PASS, VHOST); + + $this->channel = $this->connection->channel(); + + $this->exchange = (object) [ + 'name' => null, + ]; + + $this->queue = (object) [ + 'name' => null, + ]; + + $this->message = (object) [ + 'body' => null, + 'properties' => null, + ]; + } + + public function tearDown() + { + $this->channel->close(); + $this->connection->close(); + } +} diff --git a/tests/Functional/Channel/DirectExchangeTest.php b/tests/Functional/Channel/DirectExchangeTest.php new file mode 100644 index 000000000..7927a4516 --- /dev/null +++ b/tests/Functional/Channel/DirectExchangeTest.php @@ -0,0 +1,92 @@ +exchange->name = 'test_direct_exchange'; + } + + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPRuntimeException + * @expectedExceptionMessage Channel connection is closed. + */ + public function exchange_declare_with_closed_connection() + { + $this->connection->close(); + + $this->channel->exchange_declare($this->exchange->name, 'direct', false, true, false); + } + + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPRuntimeException + * @expectedExceptionMessage Channel connection is closed. + */ + public function exchange_declare_with_closed_channel() + { + $this->channel->close(); + + $this->channel->exchange_declare($this->exchange->name, 'direct', false, true, false); + } + + /** + * @test + */ + public function basic_consume_foo() + { + $this->channel->exchange_declare($this->exchange->name, 'direct', false, false, false); + list($this->queue->name, ,) = $this->channel->queue_declare(); + $this->channel->queue_bind($this->queue->name, $this->exchange->name, $this->queue->name); + + $this->message = (object) [ + 'body' => 'foo', + 'properties' => [ + 'content_type' => 'text/plain', + 'delivery_mode' => AMQPMessage::DELIVERY_MODE_NON_PERSISTENT, + 'correlation_id' => 'my_correlation_id', + 'reply_to' => 'my_reply_to', + ], + ]; + + $msg = new AMQPMessage($this->message->body, $this->message->properties); + + $this->channel->basic_publish($msg, $this->exchange->name, $this->queue->name); + + $callback = function ($msg) + { + $this->assertEquals($this->message->body, $msg->body); + $this->assertEquals(getmypid(), $msg->delivery_info['consumer_tag']); + $this->assertEquals($this->queue->name, $msg->delivery_info['routing_key']); + $this->assertEquals($this->exchange->name, $msg->delivery_info['exchange']); + $this->assertEquals(false, $msg->delivery_info['redelivered']); + $this->assertEquals($this->message->properties['content_type'], $msg->get('content_type')); + $this->assertEquals($this->message->properties['correlation_id'], $msg->get('correlation_id')); + $this->assertEquals($this->message->properties['reply_to'], $msg->get('reply_to')); + $this->setExpectedException('OutOfBoundsException'); + $msg->get('no_property'); + }; + + $this->channel->basic_consume( + $this->queue->name, + getmypid(), + false, + false, + false, + false, + $callback + ); + + while (count($this->channel->callbacks)) { + $this->channel->wait(); + } + } +} diff --git a/tests/Functional/Channel/TopicExchangeTest.php b/tests/Functional/Channel/TopicExchangeTest.php new file mode 100644 index 000000000..fd8fe7278 --- /dev/null +++ b/tests/Functional/Channel/TopicExchangeTest.php @@ -0,0 +1,84 @@ +exchange->name = 'test_topic_exchange'; + } + + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPRuntimeException + * @expectedExceptionMessage Channel connection is closed. + */ + public function exchange_declare_with_closed_connection() + { + $this->connection->close(); + + $this->channel->exchange_declare($this->exchange->name, 'topic', false, true, false); + } + + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPRuntimeException + * @expectedExceptionMessage Channel connection is closed. + */ + public function exchange_declare_with_closed_channel() + { + $this->channel->close(); + + $this->channel->exchange_declare($this->exchange->name, 'topic', false, true, false); + } + + /** + * @test + */ + public function publish_with_confirm() + { + $this->channel->exchange_declare($this->exchange->name, 'topic', false); + + $deliveryTags = []; + + $this->channel->set_ack_handler(function (AMQPMessage $message) use (&$deliveryTags) { + $deliveryTags[] = (int) $message->get('delivery_tag'); + return false; + }); + + $this->channel->confirm_select(); + + $connection2 = new AMQPSocketConnection(HOST, PORT, USER, PASS, VHOST); + $channel2 = $connection2->channel(); + + $channel2->queue_declare('tst.queue3'); + $channel2->queue_bind('tst.queue3', $this->exchange->name, '#'); + + $this->channel->basic_publish(new AMQPMessage('foo'), $this->exchange->name); + $this->channel->basic_publish(new AMQPMessage('bar'), $this->exchange->name); + + $publishedMessagesProperty = new \ReflectionProperty(get_class($this->channel), 'published_messages'); + $publishedMessagesProperty->setAccessible(true); + + $this->channel->wait_for_pending_acks_returns(1); + + $msg1 = $channel2->basic_get('tst.queue3'); + $msg2 = $channel2->basic_get('tst.queue3'); + + $this->assertInstanceOf('PhpAmqpLib\Message\AMQPMessage', $msg1); + $this->assertInstanceOf('PhpAmqpLib\Message\AMQPMessage', $msg2); + $this->assertSame('foo', $msg1->getBody()); + $this->assertSame('bar', $msg2->getBody()); + $this->assertSame([1, 2], $deliveryTags); + + $channel2->close(); + $connection2->close(); + } +} diff --git a/tests/Functional/ChannelTest.php b/tests/Functional/ChannelTest.php deleted file mode 100644 index c1c7c8b4a..000000000 --- a/tests/Functional/ChannelTest.php +++ /dev/null @@ -1,85 +0,0 @@ -createConnection(); - $channel = $connection->channel(); - - try { - $channel->exchange_declare('tst.exchange2', 'topic', true); - } catch (AMQPProtocolChannelException $e) { - // Do Nothing - } - - $this->setExpectedException('\PhpAmqpLib\Exception\AMQPRuntimeException', 'Channel connection is closed.'); - $channel->exchange_declare('tst.exchange2', 'topic', false, true, false); - - $channel->close(); - $connection->close(); - } - - public function testPublishWithConfirm() - { - $connection = $this->createConnection(); - $connection->connectOnConstruct(); - $channel = $connection->channel(); - - try { - $channel->exchange_declare('tst.exchange3', 'topic', false); - } catch (AMQPProtocolChannelException $e) { - // Do Nothing - } - - $deliveryTags = array(); - - $channel->set_ack_handler(function (AMQPMessage $message) use (&$deliveryTags) { - $deliveryTags[] = (int) $message->get('delivery_tag'); - return false; - }); - - $channel->confirm_select(); - - $connection2 = $this->createConnection(); - $channel2 = $connection2->channel(); - - $channel2->queue_declare('tst.queue3'); - $channel2->queue_bind('tst.queue3', 'tst.exchange3', '#'); - - $channel->basic_publish(new AMQPMessage('foo'), 'tst.exchange3'); - $channel->basic_publish(new AMQPMessage('bar'), 'tst.exchange3'); - - $publishedMessagesProperty = new \ReflectionProperty(get_class($this->channel), 'published_messages'); - $publishedMessagesProperty->setAccessible(true); - - $channel->wait_for_pending_acks_returns(1); - - $msg1 = $channel2->basic_get('tst.queue3'); - $this->assertInstanceOf('PhpAmqpLib\Message\AMQPMessage', $msg1); - $msg2 = $channel2->basic_get('tst.queue3'); - $this->assertInstanceOf('PhpAmqpLib\Message\AMQPMessage', $msg2); - - $this->assertSame('foo', $msg1->getBody()); - $this->assertSame('bar', $msg2->getBody()); - - $this->assertSame(array(1,2), $deliveryTags); - - $channel2->queue_delete('tst.queue3'); - $channel->exchange_delete('tst.exchange3'); - } -} diff --git a/tests/Functional/FileTransferTest.php b/tests/Functional/FileTransferTest.php index 7bceafdfe..76c028ead 100644 --- a/tests/Functional/FileTransferTest.php +++ b/tests/Functional/FileTransferTest.php @@ -9,63 +9,44 @@ class FileTransferTest extends TestCase { - /** - * @var string - */ protected $exchangeName = 'test_exchange'; - /** - * @var string - */ protected $queueName = null; - /** - * @var AMQPConnection - */ protected $connection; - /** - * @var AMQPChannel - */ protected $channel; - /** - * @var string - */ protected $messageBody; public function setUp() { $this->connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST); $this->channel = $this->connection->channel(); - $this->channel->exchange_declare($this->exchangeName, 'direct', false, false, false); list($this->queueName, ,) = $this->channel->queue_declare(); $this->channel->queue_bind($this->queueName, $this->exchangeName, $this->queueName); } - private function generateRandomBytes($num_bytes) + public function tearDown() { - // If random_bytes exists (PHP 7) or has been polyfilled, use it - if ( function_exists('random_bytes') ) { - return random_bytes($num_bytes); + if ($this->channel) { + $this->channel->exchange_delete($this->exchangeName); + $this->channel->close(); } - // Otherwise, just make some noise quickly - else { - $data = ''; - for ($i = 0; $i < $num_bytes; $i++) { - $data .= chr(rand(0, 255)); - } - - return $data; + if ($this->connection) { + $this->connection->close(); } } - public function testSendFile() + /** + * @test + */ + public function send_file() { $this->messageBody = $this->generateRandomBytes(1024 * 1024); - $msg = new AMQPMessage($this->messageBody, array('delivery_mode' => AMQPMessage::DELIVERY_MODE_NON_PERSISTENT)); + $msg = new AMQPMessage($this->messageBody, ['delivery_mode' => AMQPMessage::DELIVERY_MODE_NON_PERSISTENT]); $this->channel->basic_publish($msg, $this->exchangeName, $this->queueName); @@ -76,7 +57,7 @@ public function testSendFile() false, false, false, - array($this, 'processMessage') + [$this, 'processMessage'] ); while (count($this->channel->callbacks)) { @@ -87,22 +68,26 @@ public function testSendFile() public function processMessage($msg) { $delivery_info = $msg->delivery_info; - $delivery_info['channel']->basic_ack($delivery_info['delivery_tag']); $delivery_info['channel']->basic_cancel($delivery_info['consumer_tag']); $this->assertEquals($this->messageBody, $msg->body); } - public function tearDown() + private function generateRandomBytes($num_bytes) { - if ($this->channel) { - $this->channel->exchange_delete($this->exchangeName); - $this->channel->close(); + // If random_bytes exists (PHP 7) or has been polyfilled, use it + if ( function_exists('random_bytes') ) { + return random_bytes($num_bytes); } + // Otherwise, just make some noise quickly + else { + $data = ''; + for ($i = 0; $i < $num_bytes; $i++) { + $data .= chr(rand(0, 255)); + } - if ($this->connection) { - $this->connection->close(); + return $data; } } } diff --git a/tests/Functional/ReconnectConnectionTest.php b/tests/Functional/ReconnectConnectionTest.php index c14c4695d..18b2614ac 100644 --- a/tests/Functional/ReconnectConnectionTest.php +++ b/tests/Functional/ReconnectConnectionTest.php @@ -11,206 +11,141 @@ class ReconnectConnectionTest extends TestCase { - /** - * Hold onto the connection - * @var \PhpAmqpLib\Connection\AbstractConnection - */ protected $connection = null; - /** - * Hold onto the channel - * @var \PhpAmqpLib\Channel\AbstractChannel|AMQPChannel - */ protected $channel = null; - /** - * Exchange name - * @var string - */ protected $exchange = 'reconnect_exchange'; - /** - * Queue name - * @var string - */ protected $queue = 'reconnect_queue'; - /** - * Queue message body - * @var string - */ protected $msgBody = 'foo bar baz äëïöü'; - /** - * Test the reconnect logic on the lazy connection, which is also stream connection - */ - public function testLazyConnectionReconnect() + public function tearDown() { - $this->connection = $this->getLazyConnection(); - $this->performTest(); + if ($this->channel) { + $this->channel->exchange_delete($this->exchange); + $this->channel->queue_delete($this->queue); + $this->channel->close(); + } + if ($this->connection) { + $this->connection->close(); + } } /** - * Get a new lazy connection - * @return AMQPLazyConnection + * @test */ - protected function getLazyConnection() + public function lazy_connection_reconnect() { - return new AMQPLazyConnection(HOST, PORT, USER, PASS, VHOST); - } + $this->connection = $this->getLazyConnection(); - /** - * @return AMQPLazySocketConnection - */ - protected function getLazySocketConnection() - { - return new AMQPLazySocketConnection(HOST, PORT, USER, PASS, VHOST); + $this->doTest(); } /** - * Perform the test after the connection has already been setup + * @test */ - protected function performTest() + public function lazy_connection_close_reconnect() { - // We need to a channel and exchange/queue + $this->connection = $this->getLazyConnection(); $this->setupChannel(); - - // Ensure normal publish then get works - $this->assertEquals($this->msgBody, $this->publishGet()->body); - - // Reconnect the socket/stream connection + $this->connection->close(); $this->connection->reconnect(); - - // Setup the channel and declarations again $this->setupChannel(); - // Ensure normal publish then get works (after reconnect attempt) $this->assertEquals($this->msgBody, $this->publishGet()->body); } /** - * Setup the exchanges, and queues and channel + * @test */ - protected function setupChannel() + public function socket_connection_reconnect() { - $this->channel = $this->connection->channel(); + $this->connection = $this->getSocketConnection(); - $this->channel->exchange_declare($this->exchange, 'direct', false, false, false); - $this->channel->queue_declare($this->queue); - $this->channel->queue_bind($this->queue, $this->exchange, $this->queue); + $this->doTest(); } /** - * Publish a message, then get it immediately - * @return \PhpAmqpLib\Message\AMQPMessage + * @test */ - protected function publishGet() + public function socket_connection_close_reconnect() { - $msg = new AMQPMessage($this->msgBody, array( - 'content_type' => 'text/plain', - 'delivery_mode' => AMQPMessage::DELIVERY_MODE_NON_PERSISTENT, - 'correlation_id' => 'my_correlation_id', - 'reply_to' => 'my_reply_to' - )); - - $this->channel->basic_publish($msg, $this->exchange, $this->queue); + $this->connection = $this->getSocketConnection(); + $this->connection->close(); + $this->connection->reconnect(); + $this->setupChannel(); - return $this->channel->basic_get($this->queue); + $this->assertEquals($this->msgBody, $this->publishGet()->body); } /** - * Test the reconnect logic on the lazy connection, which is also stream connection after its been closed + * @test */ - public function testLazyConnectionCloseReconnect() + public function lazy_socket_connection_close_reconnect() { - $this->connection = $this->getLazyConnection(); - - // Force connection - $this->setupChannel(); - - // Manually close the connection + $this->connection = $this->getLazySocketConnection(); $this->connection->close(); - - // Attempt the reconnect after its been manually closed $this->connection->reconnect(); $this->setupChannel(); - // Ensure normal publish then get works $this->assertEquals($this->msgBody, $this->publishGet()->body); } /** - * Test the reconnect logic on the socket connection + * @test */ - public function testSocketConnectionReconnect() + public function lazy_connection_socket_reconnect() { - $this->connection = $this->getSocketConnection(); - $this->performTest(); + $this->connection = $this->getLazySocketConnection(); + + $this->doTest(); } - /** - * Get a new socket connection - * @return AMQPSocketConnection - */ protected function getSocketConnection() { return new AMQPSocketConnection(HOST, PORT, USER, PASS, VHOST); } - /** - * Test the reconnect logic on the socket connection after its been closed - */ - public function testSocketConnectionCloseReconnect() + protected function getLazyConnection() { - $this->connection = $this->getSocketConnection(); - $this->connection->close(); - - // Attempt the reconnect after its been manually closed - $this->connection->reconnect(); - $this->setupChannel(); - - // Ensure normal publish then get works - $this->assertEquals($this->msgBody, $this->publishGet()->body); + return new AMQPLazyConnection(HOST, PORT, USER, PASS, VHOST); } - /** - * Test the reconnect logic on the lazy connection, which is also socket connection after its been closed - */ - public function testLazySocketConnectionCloseReconnect() + protected function getLazySocketConnection() { - $this->connection = $this->getLazySocketConnection(); - $this->connection->close(); + return new AMQPLazySocketConnection(HOST, PORT, USER, PASS, VHOST); + } - // Attempt the reconnect after its been manually closed + protected function doTest() + { + $this->setupChannel(); + $this->assertEquals($this->msgBody, $this->publishGet()->body); $this->connection->reconnect(); $this->setupChannel(); - // Ensure normal publish then get works $this->assertEquals($this->msgBody, $this->publishGet()->body); } - /** - * Test the reconnect logic on the lazy connection, which is also socket connection - */ - public function testLazyConnectionSocketReconnect() + protected function setupChannel() { - $this->connection = $this->getLazySocketConnection(); - $this->performTest(); + $this->channel = $this->connection->channel(); + $this->channel->exchange_declare($this->exchange, 'direct', false, false, false); + $this->channel->queue_declare($this->queue); + $this->channel->queue_bind($this->queue, $this->exchange, $this->queue); } - /** - * Shut it down, delete exchanges, queues, close connections and channels - */ - public function tearDown() + protected function publishGet() { - if ($this->channel) { - $this->channel->exchange_delete($this->exchange); - $this->channel->queue_delete($this->queue); - $this->channel->close(); - } + $msg = new AMQPMessage($this->msgBody, [ + 'content_type' => 'text/plain', + 'delivery_mode' => AMQPMessage::DELIVERY_MODE_NON_PERSISTENT, + 'correlation_id' => 'my_correlation_id', + 'reply_to' => 'my_reply_to' + ]); + $this->channel->basic_publish($msg, $this->exchange, $this->queue); - if ($this->connection) { - $this->connection->close(); - } + return $this->channel->basic_get($this->queue); } } diff --git a/tests/Functional/StreamIOTest.php b/tests/Functional/StreamIOTest.php index ec611a971..06b880b56 100644 --- a/tests/Functional/StreamIOTest.php +++ b/tests/Functional/StreamIOTest.php @@ -7,9 +7,11 @@ class StreamIOTest extends TestCase { - public function testErrorHandlerIsRestoredOnFailedConnection() + /** + * @test + */ + public function error_handler_is_restored_on_failed_connection() { - // Ignore the notice for this test. error_reporting(~E_NOTICE); $exceptionThrown = false; @@ -21,7 +23,7 @@ public function testErrorHandlerIsRestoredOnFailedConnection() $this->assertTrue($exceptionThrown, 'Custom error handler was not set.'); $exceptionThrown = false; - $arr = array(); + $arr = []; try { $notice = $arr['second-key-that-does-not-exist-and-should-generate-a-notice']; @@ -30,7 +32,6 @@ public function testErrorHandlerIsRestoredOnFailedConnection() } $this->assertFalse($exceptionThrown, 'Default error handler was not restored.'); - // Set error reporting level back error_reporting(E_ALL); } } diff --git a/tests/Unit/Channel/AMQPChannelTest.php b/tests/Unit/Channel/AMQPChannelTest.php deleted file mode 100644 index a4abcb783..000000000 --- a/tests/Unit/Channel/AMQPChannelTest.php +++ /dev/null @@ -1,19 +0,0 @@ -getMockBuilder('\PhpAmqpLib\Channel\AMQPChannel') - ->setMethods(array('queue_bind')) - ->disableOriginalConstructor() - ->getMock(); - - /* @var $mockChannel \PhpAmqpLib\Channel\AMQPChannel */ - $mockChannel->close(); - } -} diff --git a/tests/Unit/Helper/MiscHelperTest.php b/tests/Unit/Helper/MiscHelperTest.php index 10838f242..c0ef64c11 100644 --- a/tests/Unit/Helper/MiscHelperTest.php +++ b/tests/Unit/Helper/MiscHelperTest.php @@ -8,52 +8,63 @@ class MiscHelperTest extends TestCase { /** - * @dataProvider getInputOutputForSplitSecondsMicroseconds - * @param mixed $input - * @param array $expected + * @dataProvider splitSecondsMicrosecondsData + * @test */ - public function testSplitSecondsMicroseconds($input, $expected) + public function split_seconds_microseconds($input, $expected) { $this->assertEquals($expected, MiscHelper::splitSecondsMicroseconds($input)); } - public function getInputOutputForSplitSecondsMicroseconds() + /** + * @dataProvider hexdumpData + * @test + */ + public function hexdump($args, $expected) + { + $this->assertRegExp($expected, MiscHelper::hexdump($args[0], $args[1], $args[2], $args[3])); + } + + /** + * @test + */ + public function method_sig() { - return array( - array(0, array(0, 0)), - array(0.3, array(0, 300000)), - array('0.3', array(0, 300000)), - array(3, array(3, 0)), - array('3', array(3, 0)), - array(3.0, array(3, 0)), - array('3.0', array(3, 0)), - array(3.1, array(3, 100000)), - array('3.1', array(3, 100000)), - array(3.123456, array(3, 123456)), - array('3.123456', array(3, 123456)), - ); + $this->assertEquals('test', MiscHelper::methodSig('test')); } - public function testHexDump() + /** + * @test + */ + public function save_bytes() { - $htmlOutput = false; - $uppercase = false; - $return = true; - $res = MiscHelper::hexdump('FM', $htmlOutput, $uppercase, $return); - $this->assertRegExp('/000\s+46 4d\s+FM/', $res); - $uppercase = true; - $res = MiscHelper::hexdump('FM', $htmlOutput, $uppercase, $return); - $this->assertRegExp('/000\s+46 4D\s+FM/', $res); + MiscHelper::saveBytes('bytes'); + + $this->assertStringEqualsFile('/tmp/bytes', 'bytes'); } - public function testMethodSigForString() + public function splitSecondsMicrosecondsData() { - $this->assertEquals('test',MiscHelper::methodSig('test')); + return [ + [0, [0, 0]], + [0.3, [0, 300000]], + ['0.3', [0, 300000]], + [3, [3, 0]], + ['3', [3, 0]], + [3.0, [3, 0]], + ['3.0', [3, 0]], + [3.1, [3, 100000]], + ['3.1', [3, 100000]], + [3.123456, [3, 123456]], + ['3.123456', [3, 123456]], + ]; } - public function testSaveBytes() + public function hexdumpData() { - MiscHelper::saveBytes('bytes'); - $this->assertStringEqualsFile('/tmp/bytes','bytes'); + return [ + [['FM', false, false, true], '/000\s+46 4d\s+FM/'], + [['FM', false, true, true], '/000\s+46 4D\s+FM/'], + ]; } } diff --git a/tests/Unit/Helper/Protocol/Protocol091Test.php b/tests/Unit/Helper/Protocol/Protocol091Test.php index eed6e8741..600398da0 100644 --- a/tests/Unit/Helper/Protocol/Protocol091Test.php +++ b/tests/Unit/Helper/Protocol/Protocol091Test.php @@ -7,9 +7,6 @@ class Protocol091Test extends TestCase { - /** - * @var Protocol091 - */ protected $protocol091; public function setUp() @@ -17,12 +14,22 @@ public function setUp() $this->protocol091 = new Protocol091(); } - public function testChannelClose() + /** + * @test + */ + public function channel_close() { $expected = "\x00\x00\x00\x00\x00\x00\x00"; list($class_id, $method_id, $args) = $this->protocol091->channelClose(0, '', 0, 0); + $this->assertEquals($expected, $args->getvalue()); + } + /** + * @test + */ + public function channel_close_error() + { $expected = "\x00\x00\x05error\x00\x00\x00\x00"; list($class_id, $method_id, $args) = $this->protocol091->channelClose(0, 'error', 0, 0); $this->assertEquals($expected, $args->getvalue()); @@ -32,179 +39,299 @@ public function testChannelClose() $this->assertEquals($expected, $args->getvalue()); } - public function testFlow() + /** + * @test + */ + public function channel_flow_true() { $expected = "\x01"; list($class_id, $method_id, $args) = $this->protocol091->channelFlow(true); + $this->assertEquals($expected, $args->getvalue()); + } + /** + * @test + */ + public function channel_flow_false() + { $expected = "\x00"; list($class_id, $method_id, $args) = $this->protocol091->channelFlow(false); + $this->assertEquals($expected, $args->getvalue()); } - public function testXOpen() + /** + * @test + */ + public function channel_open_foo() { $expected = "\x03foo"; list($class_id, $method_id, $args) = $this->protocol091->channelOpen('foo'); + $this->assertEquals($expected, $args->getvalue()); + } + /** + * @test + */ + public function channel_open_empty_string() + { $expected = "\x00"; list($class_id, $method_id, $args) = $this->protocol091->channelOpen(''); + $this->assertEquals($expected, $args->getvalue()); } - public function testAccessRequest() + /** + * @test + */ + public function access_request() { $expected = "\x01/\x00"; list($class_id, $method_id, $args) = $this->protocol091->accessRequest('/', false, false, false, false, false); + $this->assertEquals($expected, $args->getvalue()); + } + /** + * @test + */ + public function access_request_foo() + { $expected = "\x04/foo\x00"; - list($class_id, $method_id, $args) = $this->protocol091->accessRequest('/foo', false, false, false, false, - false); + list($class_id, $method_id, $args) = $this->protocol091->accessRequest( + '/foo', false, false, false, false, false + ); + $this->assertEquals($expected, $args->getvalue()); } - public function testExchangeDeclare() + /** + * @test + */ + public function exchange_declare() { $expected = "\x00\x00\x03foo\x06direct\x00\x00\x00\x00\x00"; list($class_id, $method_id, $args) = $this->protocol091->exchangeDeclare( 0, 'foo', 'direct', false, false, false, false, false, - array() + [] ); + $this->assertEquals($expected, $args->getvalue()); } - public function testExchangeDelete() + /** + * @test + */ + public function exchange_delete() { $expected = "\x00\x00\x03foo\x00"; list($class_id, $method_id, $args) = $this->protocol091->exchangeDelete(0, 'foo', false, false); + $this->assertEquals($expected, $args->getvalue()); } - public function testExchangeBind() + /** + * @test + */ + public function exchange_bind() { $expected = "\x00\x00\x03foo\x03bar\x03baz\x00\x00\x00\x00\x00"; - list($class_id, $method_id, $args) = $this->protocol091->exchangeBind(0, 'foo', 'bar', 'baz', false, array()); + list($class_id, $method_id, $args) = $this->protocol091->exchangeBind(0, 'foo', 'bar', 'baz', false, []); + $this->assertEquals($expected, $args->getvalue()); } - public function testExchangeUnbind() + /** + * @test + */ + public function exchange_unbind() { $expected = "\x00\x00\x03foo\x03bar\x03baz\x00\x00\x00\x00\x00"; - list($class_id, $method_id, $args) = $this->protocol091->exchangeUnbind(0, 'foo', 'bar', 'baz', false, array()); + list($class_id, $method_id, $args) = $this->protocol091->exchangeUnbind(0, 'foo', 'bar', 'baz', false, []); + $this->assertEquals($expected, $args->getvalue()); } - public function testQueueBind() + /** + * @test + */ + public function queue_bind() { $expected = "\x00\x00\x03foo\x03bar\x03baz\x00\x00\x00\x00\x00"; - list($class_id, $method_id, $args) = $this->protocol091->queueBind(0, 'foo', 'bar', 'baz', false, array()); + list($class_id, $method_id, $args) = $this->protocol091->queueBind(0, 'foo', 'bar', 'baz', false, []); + $this->assertEquals($expected, $args->getvalue()); } - public function testQueueUnbind() + /** + * @test + */ + public function queue_unbind() { $expected = "\x00\x00\x03foo\x03bar\x03baz\x00\x00\x00\x00"; - list($class_id, $method_id, $args) = $this->protocol091->queueUnbind(0, 'foo', 'bar', 'baz', array()); + list($class_id, $method_id, $args) = $this->protocol091->queueUnbind(0, 'foo', 'bar', 'baz', []); + $this->assertEquals($expected, $args->getvalue()); } - public function testQueueDeclare() + /** + * @test + */ + public function queue_declare() { $expected = "\x00\x00\x03foo\x00\x00\x00\x00\x00"; list($class_id, $method_id, $args) = $this->protocol091->queueDeclare( 0, 'foo', false, false, false, false, false, - array() + [] ); + $this->assertEquals($expected, $args->getvalue()); } - public function testQueueDelete() + /** + * @test + */ + public function queue_delete() { $expected = "\x00\x00\x03foo\x00"; list($class_id, $method_id, $args) = $this->protocol091->queueDelete(0, 'foo', false, false, false); + $this->assertEquals($expected, $args->getvalue()); } - public function testQueuePurge() + /** + * @test + */ + public function queue_purge() { $expected = "\x00\x00\x03foo\x00"; list($class_id, $method_id, $args) = $this->protocol091->queuePurge(0, 'foo', false); + $this->assertEquals($expected, $args->getvalue()); } - public function testBasicAck() + /** + * @test + */ + public function basic_ack() { $expected = "\x00\x00\x00\x00\x00\x00\x00\x01\x00"; list($class_id, $method_id, $args) = $this->protocol091->basicAck(1, false); + $this->assertEquals($expected, $args->getvalue()); } - public function testBasicCancel() + /** + * @test + */ + public function basic_cancel() { $expected = "\x03foo\x00"; list($class_id, $method_id, $args) = $this->protocol091->basicCancel('foo', false); + $this->assertEquals($expected, $args->getvalue()); } - public function testBasicConsume() + /** + * @test + */ + public function basic_consume() { $expected = "\x00\x00\x03foo\x03bar\x00\x00\x00\x00\x00"; - list($class_id, $method_id, $args) = $this->protocol091->basicConsume(0, 'foo', 'bar', false, false, false, - false); + list($class_id, $method_id, $args) = $this->protocol091->basicConsume( + 0, 'foo', 'bar', false, false, false, false + ); + $this->assertEquals($expected, $args->getvalue()); } - public function testBasicGet() + /** + * @test + */ + public function basic_get() { $expected = "\x00\x00\x03foo\x00"; list($class_id, $method_id, $args) = $this->protocol091->basicGet(0, 'foo', false); + $this->assertEquals($expected, $args->getvalue()); } - public function testBasicPublish() + /** + * @test + */ + public function basic_publish() { $expected = "\x00\x00\x03foo\x03bar\x00"; list($class_id, $method_id, $args) = $this->protocol091->basicPublish(0, 'foo', 'bar', false, false); + $this->assertEquals($expected, $args->getvalue()); } - public function testBasicQos() + /** + * @test + */ + public function basic_qos() { $expected = "\x00\x00\x00\xA\x00\x01\x00"; list($class_id, $method_id, $args) = $this->protocol091->basicQos(10, 1, false); + $this->assertEquals($expected, $args->getvalue()); } - public function testBasicRecover() + /** + * @test + */ + public function basic_recover_true() { $expected = "\x01"; list($class_id, $method_id, $args) = $this->protocol091->basicRecover(true); + $this->assertEquals($expected, $args->getvalue()); + } + /** + * @test + */ + public function basic_recover_false() + { $expected = "\x00"; list($class_id, $method_id, $args) = $this->protocol091->basicRecover(false); + $this->assertEquals($expected, $args->getvalue()); } - public function testBasicReject() + /** + * @test + */ + public function basic_reject_1_true() { $expected = "\x00\x00\x00\x00\x00\x00\x00\x01\x01"; list($class_id, $method_id, $args) = $this->protocol091->basicReject(1, true); + $this->assertEquals($expected, $args->getvalue()); + } + /** + * @test + */ + public function basic_reject_1_false() + { $expected = "\x00\x00\x00\x00\x00\x00\x00\x01\x00"; list($class_id, $method_id, $args) = $this->protocol091->basicReject(1, false); + $this->assertEquals($expected, $args->getvalue()); } - public function testConnectionBlocked() + /** + * @test + */ + public function connection_blocked() { $expected = 'Low on memory'; list($class_id, $method_id, $args) = $this->protocol091->connectionBlocked($expected); diff --git a/tests/Unit/Message/AMQPMessageTest.php b/tests/Unit/Message/AMQPMessageTest.php index d25e79da3..030931c81 100644 --- a/tests/Unit/Message/AMQPMessageTest.php +++ b/tests/Unit/Message/AMQPMessageTest.php @@ -8,82 +8,105 @@ class AMQPMessageTest extends TestCase { - public function propertiesDataProvider() - { - return array( - array(array('priority' => 1, 'timestamp' => time()), array('priority' => 1, 'timestamp' => time())), - array(array('message_id' => '5414cfa74899a'), array('message_id' => '5414cfa74899a')), - array(array('message_id' => 0), array('message_id' => 0)), - array(array(), array('timestamp' => null)), - array(array(), array('priority' => null)), - array(array('priority' => 0), array('priority' => 0)), - array(array('priority' => false), array('priority' => false)), - array(array('priority' => '0'), array('priority' => '0')), - array( - array('application_headers' => array('x-foo' => '')), - array('application_headers' => array('x-foo' => array('S', ''))) - ), - array( - array('application_headers' => array('x-foo' => '')), - array('application_headers' => array('x-foo' => array('S', null))) - ), - array( - array('application_headers' => array('x-foo' => 0)), - array('application_headers' => array('x-foo' => array('I', 0))) - ), - array( - array('application_headers' => array('x-foo' => 1)), - array('application_headers' => array('x-foo' => array('I', true))) - ), - array( - array('application_headers' => array('x-foo' => 0)), - array('application_headers' => array('x-foo' => array('I', '0'))) - ), - array( - array('application_headers' => array('x-foo' => array())), - array('application_headers' => array('x-foo' => array('A', array()))) - ), - array( - array('application_headers' => array('x-foo' => array(null))), - array('application_headers' => array('x-foo' => array('A', array(null)))) - ), - ); - } - - /** - * @dataProvider propertiesDataProvider + * @dataProvider propertiesData + * @test */ - public function testSerializeProperties(array $expected, array $properties) + public function serialize_properties(array $expected, array $properties) { - /** @var AMQPReader $reader */ $reader = new AMQPReader(null); - /** @var AMQPMessage $message */ $message = new AMQPMessage('', $properties); - /** @var string $encodedData */ - $encodedData = $message->serialize_properties(); - // Bypasses the network part and injects the encoded data into the reader + $encodedData = $message->serialize_properties(); $reader->reuse($encodedData); - // Injects the reader into the message $message->load_properties($reader); - $props = $message->get_properties(); + if (isset($props['application_headers'])) { $props['application_headers'] = $props['application_headers']->getNativeData(); } + $this->assertEquals($expected, $props); } - public function testGetAndSetBodyMethods() + /** + * @test + */ + public function get_and_set_body() { $message = new AMQPMessage(''); $message->setBody('body'); $message->setIsTruncated(true); $message->content_encoding = 'shortstr'; - $this->assertEquals($message->getBody(),'body'); + $this->assertEquals($message->getBody(), 'body'); $this->assertTrue($message->isTruncated()); - $this->assertEquals($message->getContentEncoding(),'shortstr'); + $this->assertEquals($message->getContentEncoding(), 'shortstr'); + } + + public function propertiesData() + { + return [ + [ + ['priority' => 1, 'timestamp' => time()], + ['priority' => 1, 'timestamp' => time()], + ], + [ + ['message_id' => '5414cfa74899a'], + ['message_id' => '5414cfa74899a'], + ], + [ + ['message_id' => 0], + ['message_id' => 0], + ], + [ + [], + ['timestamp' => null], + ], + [ + [], + ['priority' => null], + ], + [ + ['priority' => 0], + ['priority' => 0], + ], + [ + ['priority' => false], + ['priority' => false], + ], + [ + ['priority' => '0'], + ['priority' => '0'], + ], + [ + ['application_headers' => ['x-foo' => '']], + ['application_headers' => ['x-foo' => ['S', '']]], + ], + [ + ['application_headers' => ['x-foo' => '']], + ['application_headers' => ['x-foo' => ['S', null]]], + ], + [ + ['application_headers' => ['x-foo' => 0]], + ['application_headers' => ['x-foo' => ['I', 0]]], + ], + [ + ['application_headers' => ['x-foo' => 1]], + ['application_headers' => ['x-foo' => ['I', true]]], + ], + [ + ['application_headers' => ['x-foo' => 0]], + ['application_headers' => ['x-foo' => ['I', '0']]], + ], + [ + ['application_headers' => ['x-foo' => []]], + ['application_headers' => ['x-foo' => ['A', []]]], + ], + [ + ['application_headers' => ['x-foo' => [null]]], + ['application_headers' => ['x-foo' => ['A', [null]]]], + ], + ]; } } diff --git a/tests/Unit/Wire/AMQPCollectionTest.php b/tests/Unit/Wire/AMQPCollectionTest.php index 98e0a1c9f..0aa269bef 100644 --- a/tests/Unit/Wire/AMQPCollectionTest.php +++ b/tests/Unit/Wire/AMQPCollectionTest.php @@ -7,250 +7,374 @@ class AMQPCollectionTest extends TestCase { - public function testEncode080() + /** + * @test + */ + public function encode_080() { $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_080); - $a = new Wire\AMQPArray(array( + + $a = new Wire\AMQPArray([ 1, - (int)-2147483648, - (int)2147483647, + (int) -2147483648, + (int) 2147483647, -2147483649, 2147483648, true, false, - array('foo' => 'bar'), - array('foo'), - array(), + ['foo' => 'bar'], + ['foo'], + [], new \DateTime('2009-02-13 23:31:30'), - ( - // DateTimeImmutable was added in PHP 5.5 - class_exists('DateTimeImmutable') + (class_exists('DateTimeImmutable') ? new \DateTimeImmutable('2009-02-13 23:31:30') : new \DateTime('2009-02-13 23:31:30') - ) - )); + ), + ]); $this->assertEquals( - array( - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), 1), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), -2147483648), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), 2147483647), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), -2147483649), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), 2147483648), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), 1), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), 0), - array( + [ + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), + 1, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), + -2147483648, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), + 2147483647, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + -2147483649, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + 2147483648, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), + 1, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), + 0, + ], + [ Wire\AMQPAbstractCollection::getDataTypeForSymbol('F'), - array('foo' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), 'bar')) - ), - array( + [ + 'foo' => [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + 'bar', + ], + ], + ], + [ Wire\AMQPAbstractCollection::getDataTypeForSymbol('F'), - array(0 => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), 'foo')) - ), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('F'), array()), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('T'), 1234567890), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('T'), 1234567890), - ), + [ + 0 => [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + 'foo', + ], + ], + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('F'), + [], + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('T'), + 1234567890, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('T'), + 1234567890, + ], + ], $this->getEncodedRawData($a) ); $eData = $this->getEncodedRawData($a, false); + $this->assertEquals(true, $eData[7][1] instanceof Wire\AMQPTable); $this->assertEquals(true, $eData[8][1] instanceof Wire\AMQPTable); $this->assertEquals(true, $eData[9][1] instanceof Wire\AMQPTable); } - protected function setProtoVersion($proto) - { - $r = new \ReflectionProperty('\\PhpAmqpLib\\Wire\\AMQPAbstractCollection', '_protocol'); - $r->setAccessible(true); - $r->setValue(null, $proto); - } - - protected function getEncodedRawData(Wire\AMQPAbstractCollection $c, $recursive = true) - { - $r = new \ReflectionProperty($c, 'data'); - $r->setAccessible(true); - $data = $r->getValue($c); - unset($r); - - if ($recursive) { - foreach ($data as &$v) { - if ($v[1] instanceof Wire\AMQPAbstractCollection) { - $v[1] = $this->getEncodedRawData($v[1]); - } - } - unset($v); - } - - return $data; - } - - public function testEncode091() + /** + * @test + */ + public function encode_091() { $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_091); - $a = new Wire\AMQPArray(array( + + $a = new Wire\AMQPArray([ 1, - (int)-2147483648, - (int)2147483647, + (int) -2147483648, + (int) 2147483647, -2147483649, 2147483648, true, false, - array('foo' => 'bar'), - array('foo'), - array() - )); + ['foo' => 'bar'], + ['foo'], + [], + ]); $is64 = PHP_INT_SIZE == 8; + $this->assertEquals( - array( - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), 1), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), -2147483648), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), 2147483647), - array( - $is64 ? Wire\AMQPAbstractCollection::getDataTypeForSymbol('L') : Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), - -2147483649 - ), - array( - $is64 ? Wire\AMQPAbstractCollection::getDataTypeForSymbol('L') : Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), - 2147483648 - ), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('t'), true), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('t'), false), - array( + [ + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), + 1, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), + -2147483648, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), + 2147483647, + ], + [ + $is64 + ? Wire\AMQPAbstractCollection::getDataTypeForSymbol('L') + : Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + -2147483649, + ], + [ + $is64 + ? Wire\AMQPAbstractCollection::getDataTypeForSymbol('L') + : Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + 2147483648, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('t'), + true, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('t'), + false, + ], + [ Wire\AMQPAbstractCollection::getDataTypeForSymbol('F'), - array('foo' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), 'bar')) - ), - array( + [ + 'foo' => [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + 'bar', + ], + ], + ], + [ Wire\AMQPAbstractCollection::getDataTypeForSymbol('A'), - array(array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), 'foo')) - ), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('A'), array()) - ), + [ + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + 'foo', + ], + ], + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('A'), + [], + ], + ], $this->getEncodedRawData($a) ); $eData = $this->getEncodedRawData($a, false); + $this->assertEquals(true, $eData[7][1] instanceof Wire\AMQPTable); $this->assertEquals(true, $eData[8][1] instanceof Wire\AMQPArray); $this->assertEquals(true, $eData[9][1] instanceof Wire\AMQPArray); } - public function testEncodeRabbit() + /** + * @test + */ + public function encode_rabbit() { $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_RBT); - $a = new Wire\AMQPArray(array( + + $a = new Wire\AMQPArray([ 1, - (int)-2147483648, - (int)2147483647, + (int) -2147483648, + (int) 2147483647, -2147483649, 2147483648, true, false, - array('foo' => 'bar'), - array('foo'), - array() - )); + ['foo' => 'bar'], + ['foo'], + [], + ]); $is64 = PHP_INT_SIZE == 8; + $this->assertEquals( - array( - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), 1), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), -2147483648), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), 2147483647), - array( - $is64 ? Wire\AMQPAbstractCollection::getDataTypeForSymbol('l') : Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), - -2147483649 - ), - array( - $is64 ? Wire\AMQPAbstractCollection::getDataTypeForSymbol('l') : Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), - 2147483648 - ), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('t'), true), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('t'), false), - array( + [ + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), + 1, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), + -2147483648, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), + 2147483647, + ], + [ + $is64 + ? Wire\AMQPAbstractCollection::getDataTypeForSymbol('l') + : Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + -2147483649, + ], + [ + $is64 + ? Wire\AMQPAbstractCollection::getDataTypeForSymbol('l') + : Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + 2147483648, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('t'), + true, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('t'), + false, + ], + [ Wire\AMQPAbstractCollection::getDataTypeForSymbol('F'), - array('foo' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), 'bar')) - ), - array( + [ + 'foo' => [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + 'bar' + ], + ], + ], + [ Wire\AMQPAbstractCollection::getDataTypeForSymbol('A'), - array(array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), 'foo')) - ), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('A'), array()) - ), + [ + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + 'foo', + ], + ], + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('A'), + [], + ], + ], $this->getEncodedRawData($a) ); $eData = $this->getEncodedRawData($a, false); + $this->assertEquals(true, $eData[7][1] instanceof Wire\AMQPTable); $this->assertEquals(true, $eData[8][1] instanceof Wire\AMQPArray); $this->assertEquals(true, $eData[9][1] instanceof Wire\AMQPArray); } - public function testEncodeUnknownDatatype() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPOutOfBoundsException + */ + public function encode_unknown_data_type() { - $this->setExpectedException('PhpAmqpLib\\Exception\\AMQPOutOfBoundsException'); $a = new Wire\AMQPArray(array(new \stdClass())); - $this->fail('Unknown data type not detected!'); } - public function testPushUnsupportedDataType080() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPOutOfRangeException + */ + public function push_unsupported_data_type_080() { $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_080); - $a = new Wire\AMQPArray(); - $this->setExpectedException('PhpAmqpLib\\Exception\\AMQPOutOfRangeException'); + $a->push(12345, Wire\AMQPArray::T_INT_LONGLONG); - $this->fail('Unsupported data type not detected!'); } - public function testPushUnsupportedDataType091() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPOutOfRangeException + */ + public function push_unsupported_data_type_091() { $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_091); - $a = new Wire\AMQPArray(); - $this->setExpectedException('PhpAmqpLib\\Exception\\AMQPOutOfRangeException'); + $a->push(12345, 'foo'); - $this->fail('Unsupported data type not detected!'); } - public function testPushUnsupportedDataTypeRabbit() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPOutOfRangeException + */ + public function push_unsupported_data_type_rabbit() { $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_RBT); - $a = new Wire\AMQPArray(); - $this->setExpectedException('PhpAmqpLib\\Exception\\AMQPOutOfRangeException'); + $a->push(12345, Wire\AMQPArray::T_INT_LONGLONG_U); - $this->fail('Unsupported data type not detected!'); } - public function testPushWithType() + /** + * @test + */ + public function push_with_type() { $a = new Wire\AMQPArray(); + $a->push(576, Wire\AMQPArray::T_INT_LONG); $a->push('foo', Wire\AMQPArray::T_STRING_LONG); - $a->push(new Wire\AMQPTable(array('foo' => 'bar'))); - $a->push(new Wire\AMQPArray(array('bar'))); + $a->push(new Wire\AMQPTable(['foo' => 'bar'])); + $a->push(new Wire\AMQPArray(['bar'])); $this->assertEquals( - array( - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), 576), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), 'foo'), - array( + [ + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), + 576, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + 'foo', + ], + [ Wire\AMQPAbstractCollection::getDataTypeForSymbol('F'), - array('foo' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), 'bar')) - ), - array( + [ + 'foo' => [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + 'bar', + ], + ], + ], + [ Wire\AMQPAbstractCollection::getDataTypeForSymbol('A'), - array(array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), 'bar')) - ) - ), + [ + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + 'bar', + ], + ], + ], + ], $this->getEncodedRawData($a) ); } - public function testConflictingFieldSymbols() + /** + * @test + */ + public function conflicting_field_symbols() { $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_091); @@ -259,10 +383,16 @@ public function testConflictingFieldSymbols() $a->push(1234567, Wire\AMQPArray::T_INT_LONGLONG); $this->assertEquals( - array( - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('U'), 576), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('L'), 1234567) - ), + [ + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('U'), + 576, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('L'), + 1234567, + ], + ], $this->getEncodedRawData($a) ); @@ -273,227 +403,422 @@ public function testConflictingFieldSymbols() $a->push(1234567, Wire\AMQPArray::T_INT_LONGLONG); $this->assertEquals( - array( - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('s'), 576), - array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('l'), 1234567) - ), + [ + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('s'), + 576, + ], + [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('l'), + 1234567, + ], + ], $this->getEncodedRawData($a) ); } - public function testSetEmptyKey() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + * @expectedExceptionMessage Table key must be non-empty string up to 128 chars in length + */ + public function set_empty_key() { $t = new Wire\AMQPTable(); - $this->setExpectedException('PhpAmqpLib\\Exception\\AMQPInvalidArgumentException', - 'Table key must be non-empty string up to 128 chars in length'); $t->set('', 'foo'); - $this->fail('Empty table key not detected!'); } - public function testSetLongKey() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + * @expectedExceptionMessage Table key must be non-empty string up to 128 chars in length + */ + public function set_long_key() { $t = new Wire\AMQPTable(); - $t->set(str_repeat('a', 128), 'foo'); - $this->setExpectedException('PhpAmqpLib\\Exception\\AMQPInvalidArgumentException', - 'Table key must be non-empty string up to 128 chars in length'); $t->set(str_repeat('a', 129), 'bar'); - $this->fail('Excessive key length not detected!'); } - public function testPushMismatchedType() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function push_mismatched_type() { $a = new Wire\AMQPArray(); - $this->setExpectedException('PhpAmqpLib\\Exception\\AMQPInvalidArgumentException'); + $a->push(new Wire\AMQPArray(), Wire\AMQPArray::T_TABLE); - $this->fail('Mismatched data type not detected!'); } - public function testPushRawArrayWithType() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + * @expectedExceptionMessage Arrays must be passed as AMQPArray instance + */ + public function push_raw_array_with_type() { $a = new Wire\AMQPArray(); - $this->setExpectedException('PhpAmqpLib\\Exception\\AMQPInvalidArgumentException', - 'Arrays must be passed as AMQPArray instance'); + $a->push(array(), Wire\AMQPArray::T_ARRAY); - $this->fail('Raw array data not detected!'); } - public function testPushRawTableWithType() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + * @expectedExceptionMessage Tables must be passed as AMQPTable instance + */ + public function push_raw_table_with_type() { $a = new Wire\AMQPArray(); - $this->setExpectedException('PhpAmqpLib\\Exception\\AMQPInvalidArgumentException', - 'Tables must be passed as AMQPTable instance'); + $a->push(array(), Wire\AMQPArray::T_TABLE); - $this->fail('Raw table data not detected!'); } - public function testPushFloatWithDecimalType() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + * @expectedExceptionMessage Decimal values must be instance of AMQPDecimal + */ + public function push_float_with_decimal_type() { $a = new Wire\AMQPArray(); - $this->setExpectedException('PhpAmqpLib\\Exception\\AMQPInvalidArgumentException', - 'Decimal values must be instance of AMQPDecimal'); + $a->push(35.2, Wire\AMQPArray::T_DECIMAL); - $this->fail('Wrong decimal data not detected!'); } - public function testArrayRoundTrip080() + /** + * @test + */ + public function array_round_trip_080() { $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_080); - $a = new Wire\AMQPArray($this->getTestDataSrc()); - $this->assertEquals(array_values($this->getTestDataCmp080()), $a->getNativeData()); - } - - protected function getTestDataSrc() - { - return array( - 'long' => 12345, - 'long_neg' => -12345, - 'longlong' => 3000000000, - 'longlong_neg' => -3000000000, - 'float_low' => (float)9.2233720368548, - 'float_high' => (float)9223372036854800000, - 'bool_true' => true, - 'bool_false' => false, - 'void' => null, - 'array' => array(1, 2, 3, 'foo', array('bar' => 'baz'), array('boo', false, 5), true, null), - 'array_empty' => array(), - 'table' => array( - 'foo' => 'bar', - 'baz' => 'boo', - 'bool' => true, - 'tbl' => array('bar' => 'baz'), - 'arr' => array('boo', false, 5) - ), - 'table_num' => array(1 => 5, 3 => 'foo', 786 => 674), - 'array_nested' => array(1, array(2, array(3, array(4)))), - 'table_nested' => array('i' => 1, 'n' => array('i' => 2, 'n' => array('i' => 3, 'n' => array('i' => 4)))) - ); - } - protected function getTestDataCmp080() - { - return array( - 'long' => 12345, - 'long_neg' => -12345, - 'longlong' => (string)3000000000, - 'longlong_neg' => (string)-3000000000, - 'float_low' => (string)(float)9.2233720368548, - 'float_high' => (string)(float)9223372036854800000, - 'bool_true' => 1, - 'bool_false' => 0, - 'void' => '', - 'array' => array(1, 2, 3, 'foo', array('bar' => 'baz'), array('boo', 0, 5), 1, ''), - 'array_empty' => array(), - 'table' => array( - 'foo' => 'bar', - 'baz' => 'boo', - 'bool' => 1, - 'tbl' => array('bar' => 'baz'), - 'arr' => array('boo', 0, 5) - ), - 'table_num' => array(1 => 5, 3 => 'foo', 786 => 674), - 'array_nested' => array(1, array(2, array(3, array(4)))), - 'table_nested' => array('i' => 1, 'n' => array('i' => 2, 'n' => array('i' => 3, 'n' => array('i' => 4)))) - ); + $this->assertEquals(array_values($this->getTestDataCmp080()), $a->getNativeData()); } - public function testArrayRoundTrip091() + /** + * @test + */ + public function array_round_trip_091() { $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_091); - $a = new Wire\AMQPArray($this->getTestDataSrc()); + $this->assertEquals(array_values($this->getTestDataCmp()), $a->getNativeData()); } /** - * The only purpose of this *Cmp / *Cmp080 shit is to pass tests on travis's ancient phpunit 3.7.38 + * @test */ - protected function getTestDataCmp() - { - return array( - 'long' => 12345, - 'long_neg' => -12345, - 'longlong' => 3000000000, - 'longlong_neg' => -3000000000, - 'float_low' => (string)(float)9.2233720368548, - 'float_high' => (string)(float)9223372036854800000, - 'bool_true' => true, - 'bool_false' => false, - 'void' => null, - 'array' => array(1, 2, 3, 'foo', array('bar' => 'baz'), array('boo', false, 5), true, null), - 'array_empty' => array(), - 'table' => array( - 'foo' => 'bar', - 'baz' => 'boo', - 'bool' => true, - 'tbl' => array('bar' => 'baz'), - 'arr' => array('boo', false, 5) - ), - 'table_num' => array(1 => 5, 3 => 'foo', 786 => 674), - 'array_nested' => array(1, array(2, array(3, array(4)))), - 'table_nested' => array('i' => 1, 'n' => array('i' => 2, 'n' => array('i' => 3, 'n' => array('i' => 4)))) - ); - } - - public function testArrayRoundTripRabbit() + public function array_round_trip_rabbit() { $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_RBT); - $a = new Wire\AMQPArray($this->getTestDataSrc()); + $this->assertEquals(array_values($this->getTestDataCmp()), $a->getNativeData()); } - public function testTableRoundTrip080() + /** + * @test + */ + public function table_round_trip_080() { $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_080); - $a = new Wire\AMQPTable($this->getTestDataSrc()); + $this->assertEquals($this->getTestDataCmp080(), $a->getNativeData()); } - public function testTableRoundTrip091() + /** + * @test + */ + public function table_round_trip_091() { $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_091); - $a = new Wire\AMQPTable($this->getTestDataSrc()); + $this->assertEquals($this->getTestDataCmp(), $a->getNativeData()); } - public function testTableRoundTripRabbit() + /** + * @test + */ + public function table_round_trip_rabbit() { $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_RBT); - $a = new Wire\AMQPTable($this->getTestDataSrc()); + $this->assertEquals($this->getTestDataCmp(), $a->getNativeData()); } - public function testIterator() + /** + * @test + */ + public function iterator() { - $d = array('a' => 1, 'b' => -2147, 'c' => array('foo' => 'bar'), 'd' => true, 'e' => false); - $ed = array( - 'a' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), 1), - 'b' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), -2147), - 'c' => array( + $d = [ + 'a' => 1, + 'b' => -2147, + 'c' => [ + 'foo' => 'bar', + ], + 'd' => true, + 'e' => false, + ]; + + $ed = [ + 'a' => [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), + 1, + ], + 'b' => [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), + -2147, + ], + 'c' => [ Wire\AMQPAbstractCollection::getDataTypeForSymbol('F'), - array('foo' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), 'bar')) - ), - 'd' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('t'), true), - 'e' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('t'), false) - ); + [ + 'foo' => [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), + 'bar', + ], + ], + ], + 'd' => [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('t'), + true, + ], + 'e' => [ + Wire\AMQPAbstractCollection::getDataTypeForSymbol('t'), + false, + ], + ]; $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_091); $a = new Wire\AMQPTable($d); foreach ($a as $key => $val) { if (!isset($d[$key])) { - $this->fail('Unknown key: ' . $key); + $this->fail('Unknown key: '.$key); + } + $this->assertEquals( + $ed[$key], + $val[1] instanceof Wire\AMQPAbstractCollection + ? [$val[0], $this->getEncodedRawData($val[1])] + : $val + ); + } + } + + protected function setProtoVersion($proto) + { + $r = new \ReflectionProperty('\\PhpAmqpLib\\Wire\\AMQPAbstractCollection', '_protocol'); + $r->setAccessible(true); + $r->setValue(null, $proto); + } + + protected function getEncodedRawData(Wire\AMQPAbstractCollection $c, $recursive = true) + { + $r = new \ReflectionProperty($c, 'data'); + $r->setAccessible(true); + $data = $r->getValue($c); + unset($r); + + if ($recursive) { + foreach ($data as &$v) { + if ($v[1] instanceof Wire\AMQPAbstractCollection) { + $v[1] = $this->getEncodedRawData($v[1]); + } } - $this->assertEquals($ed[$key], $val[1] instanceof Wire\AMQPAbstractCollection ? array( - $val[0], - $this->getEncodedRawData($val[1]) - ) : $val); + unset($v); } + + return $data; + } + + protected function getTestDataSrc() + { + return [ + 'long' => 12345, + 'long_neg' => -12345, + 'longlong' => 3000000000, + 'longlong_neg' => -3000000000, + 'float_low' => (float) 9.2233720368548, + 'float_high' => (float) 9223372036854800000, + 'bool_true' => true, + 'bool_false' => false, + 'void' => null, + 'array' => [ + 1, + 2, + 3, + 'foo', + ['bar' => 'baz'], + ['boo', false, 5], + true, + null + ], + 'array_empty' => [], + 'table' => [ + 'foo' => 'bar', + 'baz' => 'boo', + 'bool' => true, + 'tbl' => ['bar' => 'baz'], + 'arr' => ['boo', false, 5], + ], + 'table_num' => [ + 1 => 5, + 3 => 'foo', + 786 => 674, + ], + 'array_nested' => [ + 1, + [ + 2, + [ + 3, + [4], + ], + ], + ], + 'table_nested' => [ + 'i' => 1, + 'n' => [ + 'i' => 2, + 'n' => [ + 'i' => 3, + 'n' => [ + 'i' => 4, + ], + ], + ], + ], + ]; + } + + /** + * The only purpose of this *Cmp / *Cmp080 shit is to pass tests on travis's ancient phpunit 3.7.38. + */ + protected function getTestDataCmp() + { + return [ + 'long' => 12345, + 'long_neg' => -12345, + 'longlong' => 3000000000, + 'longlong_neg' => -3000000000, + 'float_low' => (string) (float) 9.2233720368548, + 'float_high' => (string) (float) 9223372036854800000, + 'bool_true' => true, + 'bool_false' => false, + 'void' => null, + 'array' => [ + 1, + 2, + 3, + 'foo', + ['bar' => 'baz'], + ['boo', false, 5], + true, + null + ], + 'array_empty' => [], + 'table' => [ + 'foo' => 'bar', + 'baz' => 'boo', + 'bool' => true, + 'tbl' => ['bar' => 'baz'], + 'arr' => ['boo', false, 5], + ], + 'table_num' => [ + 1 => 5, + 3 => 'foo', + 786 => 674, + ], + 'array_nested' => [ + 1, + [ + 2, + [ + 3, + [4], + ], + ], + ], + 'table_nested' => [ + 'i' => 1, + 'n' => [ + 'i' => 2, + 'n' => [ + 'i' => 3, + 'n' => [ + 'i' => 4, + ], + ], + ], + ], + ]; + } + + protected function getTestDataCmp080() + { + return [ + 'long' => 12345, + 'long_neg' => -12345, + 'longlong' => (string) 3000000000, + 'longlong_neg' => (string) -3000000000, + 'float_low' => (string) (float) 9.2233720368548, + 'float_high' => (string) (float) 9223372036854800000, + 'bool_true' => 1, + 'bool_false' => 0, + 'void' => '', + 'array' => [ + 1, + 2, + 3, + 'foo', + ['bar' => 'baz'], + ['boo', 0, 5], + 1, + '', + ], + 'array_empty' => [], + 'table' => [ + 'foo' => 'bar', + 'baz' => 'boo', + 'bool' => 1, + 'tbl' => ['bar' => 'baz'], + 'arr' => ['boo', 0, 5], + ], + 'table_num' => [ + 1 => 5, + 3 => 'foo', + 786 => 674, + ], + 'array_nested' => [ + 1, + [ + 2, + [ + 3, + [4], + ], + ], + ], + 'table_nested' => [ + 'i' => 1, + 'n' => [ + 'i' => 2, + 'n' => [ + 'i' => 3, + 'n' => [ + 'i' => 4, + ], + ], + ], + ], + ]; } } diff --git a/tests/Unit/Wire/AMQPDecimalTest.php b/tests/Unit/Wire/AMQPDecimalTest.php index 49402f592..5a4606510 100644 --- a/tests/Unit/Wire/AMQPDecimalTest.php +++ b/tests/Unit/Wire/AMQPDecimalTest.php @@ -7,21 +7,42 @@ class AMQPDecimalTest extends TestCase { - public function testDecimal() + /** + * @test + */ + public function as_bc_value() { - $decimal = new AMQPDecimal(100,2); + $decimal = new AMQPDecimal(100, 2); - $this->assertEquals($decimal->asBCvalue(),1); - $this->assertEquals($decimal->getN(),100); - $this->assertEquals($decimal->getE(),2); + $this->assertEquals($decimal->asBCvalue(), 1); } /** - * @expectedException \PhpAmqpLib\Exception\AMQPOutOfBoundsException + * @test */ - public function testDecimalWithNegativeValue() + public function get_n() { - new AMQPDecimal(100,-1); + $decimal = new AMQPDecimal(100, 2); + + $this->assertEquals($decimal->getN(), 100); } + /** + * @test + */ + public function get_e() + { + $decimal = new AMQPDecimal(100, 2); + + $this->assertEquals($decimal->getE(), 2); + } + + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPOutOfBoundsException + */ + public function negative_value() + { + new AMQPDecimal(100, -1); + } } diff --git a/tests/Unit/Wire/AMQPWriterTest.php b/tests/Unit/Wire/AMQPWriterTest.php index 446f77e81..0771f6688 100644 --- a/tests/Unit/Wire/AMQPWriterTest.php +++ b/tests/Unit/Wire/AMQPWriterTest.php @@ -9,9 +9,6 @@ class AMQPWriterTest extends TestCase { - /** - * @var AMQPWriter - */ protected $writer; public function setUp() @@ -20,85 +17,82 @@ public function setUp() $this->writer = new AMQPWriter(); } - protected function setProtoVersion($proto) - { - $r = new \ReflectionProperty('\\PhpAmqpLib\\Wire\\AMQPAbstractCollection', '_protocol'); - $r->setAccessible(true); - $r->setValue(null, $proto); - } - public function tearDown() { $this->setProtoVersion(AMQPArray::PROTOCOL_RBT); $this->writer = null; } - public function testWriteArray() + /** + * @test + */ + public function write_array() { - $this->writer->write_array(array( + $this->writer->write_array([ 'rabbit@localhost', 'hare@localhost', 42, true - )); - + ]); $out = $this->writer->getvalue(); - - $this->assertEquals(51, mb_strlen($out, 'ASCII')); - $expected = "\x00\x00\x00\x2fS\x00\x00\x00\x10rabbit@localhostS\x00\x00\x00\x0Ehare@localhostI\x00\x00\x00\x2at\x01"; + $this->assertEquals(51, mb_strlen($out, 'ASCII')); $this->assertEquals($expected, $out); } - public function testWriteAMQPArray() + /** + * @test + */ + public function write_AMQP_array() { $this->writer->write_array( - new AMQPArray( - array( + new AMQPArray([ 'rabbit@localhost', 'hare@localhost', 42, true - ) - ) + ]) ); + $this->assertEquals( "\x00\x00\x00\x2fS\x00\x00\x00\x10rabbit@localhostS\x00\x00\x00\x0Ehare@localhostI\x00\x00\x00\x2at\x01", $this->writer->getvalue() ); } - public function testWriteTable() + /** + * @test + */ + public function write_table() { - $this->writer->write_table(array( - 'x-foo' => array('S', 'bar'), - 'x-bar' => array('A', array('baz', 'qux')), - 'x-baz' => array('I', 42), - 'x-true' => array('t', true), - 'x-false' => array('t', false), - 'x-shortshort' => array('b', -5), - 'x-shortshort-u' => array('B', 5), - 'x-short' => array('U', -1024), - 'x-short-u' => array('u', 125), - 'x-short-str' => array('s', 'foo'), + $this->writer->write_table([ + 'x-foo' => ['S', 'bar'], + 'x-bar' => ['A', ['baz', 'qux']], + 'x-baz' => ['I', 42], + 'x-true' => ['t', true], + 'x-false' => ['t', false], + 'x-shortshort' => ['b', -5], + 'x-shortshort-u' => ['B', 5], + 'x-short' => ['U', -1024], + 'x-short-u' => ['u', 125], + 'x-short-str' => ['s', 'foo'], 'x-bytes' => array('x', 'foobar'), - )); - + ]); $out = $this->writer->getvalue(); - - $expected = "\x00\x00\x00\xa3\x05x-fooS\x00\x00\x00\x03bar\x05x-barA\x00\x00\x00\x10S\x00\x00\x00\x03bazS\x00\x00\x00\x03qux\x05x-bazI\x00\x00\x00\x2a\x06x-truet\x01\x07x-falset\x00" . "\X0cx-shortshortb\xfb\x0ex-shortshort-uB\x05\x07x-shortU\xfc\x00\x09x-short-uu\x00\x7d\x0bx-short-strs\x03foo\x07x-bytesx\x00\x00\x00\x06foobar"; - $this->assertEquals($expected, $out); } - public function testWriteAMQPTable() + /** + * @test + */ + public function write_AMQP_table() { $t = new AMQPTable(); $t->set('x-foo', 'bar', AMQPTable::T_STRING_LONG); - $t->set('x-bar', new AMQPArray(array('baz', 'qux'))); + $t->set('x-bar', new AMQPArray(['baz', 'qux'])); $t->set('x-baz', 42, AMQPTable::T_INT_LONG); $t->set('x-true', true, AMQPTable::T_BOOL); $t->set('x-false', false, AMQPTable::T_BOOL); @@ -107,8 +101,8 @@ public function testWriteAMQPTable() $t->set('x-short', -1024, AMQPTable::T_INT_SHORT); $t->set('x-short-u', 125, AMQPTable::T_INT_SHORT_U); $t->set('x-short-str', 'foo', AMQPTable::T_STRING_SHORT); - $this->writer->write_table($t); + $this->assertEquals( "\x00\x00\x00\x90\x05x-fooS\x00\x00\x00\x03bar\x05x-barA\x00\x00\x00\x10S\x00\x00\x00\x03bazS\x00\x00\x00\x03qux\x05x-bazI\x00\x00\x00\x2a\x06x-truet\x01\x07x-falset\x00" . "\X0cx-shortshortb\xfb\x0ex-shortshort-uB\x05\x07x-shortU\xfc\x00\x09x-short-uu\x00\x7d\x0bx-short-strs\x03foo", @@ -116,12 +110,21 @@ public function testWriteAMQPTable() ); } - public function testWriteTableThrowsExceptionOnInvalidType() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPOutOfRangeException + */ + public function write_table_with_invalid_type() { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPOutOfRangeException'); + $this->writer->write_table([ + 'x-foo' => ['_', 'bar'], + ]); + } - $this->writer->write_table(array( - 'x-foo' => array('_', 'bar'), - )); + protected function setProtoVersion($proto) + { + $r = new \ReflectionProperty('\\PhpAmqpLib\\Wire\\AMQPAbstractCollection', '_protocol'); + $r->setAccessible(true); + $r->setValue(null, $proto); } } diff --git a/tests/Unit/Wire/IO/SocketIOTest.php b/tests/Unit/Wire/IO/SocketIOTest.php index 7c002bcac..b208d53cc 100644 --- a/tests/Unit/Wire/IO/SocketIOTest.php +++ b/tests/Unit/Wire/IO/SocketIOTest.php @@ -1,4 +1,5 @@ connect(); return $socketIO; } /** + * @test * @expectedException \PhpAmqpLib\Exception\AMQPIOException */ - public function testConnectWithInValidCredentials() + public function connect_with_invalid_credentials() { - \PHPUnit_Framework_Error_Warning::$enabled = false; - - $socket = new SocketIO('invalid_host',1,1,true); - $socket->connect(); - $socket->close(); - $socket->read(1); + $socket = new SocketIO('invalid_host', 1, 1, true); + @$socket->connect(); } /** - * @depends testConnect + * @test + * @depends connect * @expectedException \PhpAmqpLib\Exception\AMQPRuntimeException */ - public function testReadWhenClosed(SocketIO $socketIO) + public function read_when_closed(SocketIO $socketIO) { - \PHPUnit_Framework_Error_Warning::$enabled = false; - $socketIO->close(); + $socketIO->read(1); } /** - * @depends testConnect + * @test + * @depends connect * @expectedException \PhpAmqpLib\Exception\AMQPRuntimeException */ - public function testWriteWhenClosed(SocketIO $socketIO) + public function write_when_closed(SocketIO $socketIO) { - \PHPUnit_Framework_Error_Warning::$enabled = false; $socketIO->write('data'); } } diff --git a/tests/Unit/Wire/IO/StreamIOTest.php b/tests/Unit/Wire/IO/StreamIOTest.php index eaaef9d8d..0afd93808 100644 --- a/tests/Unit/Wire/IO/StreamIOTest.php +++ b/tests/Unit/Wire/IO/StreamIOTest.php @@ -5,17 +5,14 @@ use PhpAmqpLib\Wire\IO\StreamIO; use PHPUnit\Framework\TestCase; -/** - * Tests StreamIO. - */ class StreamIOTest extends TestCase { - /** + * @test * @expectedException \InvalidArgumentException * @expectedExceptionMessage read_write_timeout must be at least 2x the heartbeat */ - public function testReadWriteTimeoutMustBeAtLeastTwiceHeartbeat() + public function read_write_timeout_must_be_at_least_2x_the_heartbeat() { new StreamIO( 'localhost', diff --git a/tests/Unit/WireTest.php b/tests/Unit/WireTest.php index 829bbab25..4ffec41fb 100644 --- a/tests/Unit/WireTest.php +++ b/tests/Unit/WireTest.php @@ -10,396 +10,282 @@ class WireTest extends TestCase { - const LONG_RND_ITERS = 100000; - /** - * @var bool + * @dataProvider bitWrData + * @test */ - protected $is64; - - public function __construct() - { - parent::__construct(); - $this->is64 = PHP_INT_SIZE == 8; - } - - public function testBitWriteRead() - { - $this->bitWriteRead(true); - $this->bitWriteRead(false); - } - - protected function bitWriteRead($v) - { - $this->writeAndRead($v, 'write_bit', 'read_bit'); - } - - protected function writeAndRead($value, $write_method, $read_method, $reflection = false) - { - $writer = new AMQPWriter(); - if ($reflection) { - $m = new \ReflectionMethod($writer, $write_method); - $m->setAccessible(true); - $m->invoke($writer, $value); - } else { - $writer->{$write_method}($value); - } - - $reader = new AMQPReader($writer->getvalue()); - if ($reflection) { - $m = new \ReflectionMethod($reader, $read_method); - $m->setAccessible(true); - $readValue = $m->invoke($reader); - } else { - $readValue = $reader->{$read_method}(); - } - - $this->assertEquals($value, $readValue); - } - - public function testOctetWriteRead() - { - for ($i = 0; $i <= 255; $i++) { - $this->octetWriteRead($i); - } - } - - protected function octetWriteRead($v) - { - $this->writeAndRead($v, 'write_octet', 'read_octet'); - } - - public function testOctetOutOfRangeLower() - { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->octetWriteRead(-1); - $this->fail('Overflow not detected!'); - } - - public function testOctetOutOfRangeUpper() - { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->octetWriteRead(256); - $this->fail('Overflow not detected!'); - } - - public function testSignedOctetWriteRead() + public function bit_wr($value) { - for ($i = -128; $i <= 127; $i++) { - $this->signedOctetWriteRead($i); - } - } - - protected function signedOctetWriteRead($v) - { - $this->writeAndRead($v, 'write_signed_octet', 'read_signed_octet'); - } - - public function testSignedOctetOutOfRangeLower() - { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->signedOctetWriteRead(-129); - $this->fail('Overflow not detected!'); - } - - public function testSignedOctetOutOfRangeUpper() - { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->signedOctetWriteRead(128); - $this->fail('Overflow not detected!'); - } - - public function testShortWriteRead() - { - $this->shortWriteRead(0); - $this->shortWriteRead(1); - $this->shortWriteRead(65535); - - $this->setExpectedException( - '\PhpAmqpLib\Exception\AMQPInvalidArgumentException', - 'Short out of range: 65536' - ); - $this->shortWriteRead(65536); - } - - protected function shortWriteRead($v) - { - $this->writeAndRead($v, 'write_short', 'read_short'); + $this->wr($value, 'write_bit', 'read_bit'); } - public function testShortOutOfRangeLower() + /** + * @dataProvider octetWrData + * @test + */ + public function octet_wr($value) { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->shortWriteRead(-1); - $this->fail('Overflow not detected!'); + $this->wr($value, 'write_octet', 'read_octet'); } - public function testShortOutOfRangeUpper() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function octet_wr_out_of_range_lower() { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->shortWriteRead(65536); - $this->fail('Overflow not detected!'); + $this->wr(-1, 'write_octet', 'read_octet'); } - public function testSignedShortWriteRead() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function octet_wr_out_of_range_upper() { - $this->signedShortWriteRead(-32768); - $this->signedShortWriteRead(-32767); - $this->signedShortWriteRead(32766); - $this->signedShortWriteRead(32767); + $this->wr(256, 'write_octet', 'read_octet'); } - protected function signedShortWriteRead($v) + /** + * @dataProvider signedOctetWrData + * @test + */ + public function signed_octet_wr($value) { - $this->writeAndRead($v, 'write_signed_short', 'read_signed_short'); + $this->wr($value, 'write_signed_octet', 'read_signed_octet'); } - public function testSignedShortOutOfRangeLower() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function signed_octet_wr_out_of_range_lower() { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->signedShortWriteRead(-32769); - $this->fail('Overflow not detected!'); + $this->wr(-129, 'write_signed_octet', 'read_signed_octet'); } - public function testSignedShortOutOfRangeUpper() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function signed_octet_wr_out_of_range_upper() { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->signedShortWriteRead(32768); - $this->fail('Overflow not detected!'); + $this->wr(128, 'write_signed_octet', 'read_signed_octet'); } - public function testLongWriteRead() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + * @expectedExceptionMessage Short out of range: 65536 + */ + public function short_wr() { - $max = $this->is64 ? 4294967295 : PHP_INT_MAX; - - $this->longWriteRead(0); - $this->longWriteRead(1); - $this->longWriteRead(($max-1)); - $this->longWriteRead($max); - - //values of interest - $this->longWriteRead('0'); - $this->longWriteRead('1'); - $this->longWriteRead('2'); - - $this->longWriteRead('2147483646'); - $this->longWriteRead('2147483647'); - $this->longWriteRead('2147483648'); - - $this->longWriteRead('4294967293'); - $this->longWriteRead('4294967294'); - $this->longWriteRead('4294967295'); + $this->wr(65536, 'write_short', 'read_short'); } - protected function longWriteRead($v) + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function short_wr_out_of_range_lower() { - $this->writeAndRead($v, 'write_long', 'read_long'); + $this->wr(-1, 'write_short', 'read_short'); } - public function testLongOutOfRangeLower() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function short_wr_out_of_range_upper() { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->longWriteRead(-1); - $this->fail('Overflow not detected!'); + $this->wr(65536, 'write_short', 'read_short'); } - public function testLongOutOfRangeUpper() + /** + * @dataProvider signedShortWrData + * @test + */ + public function signed_short_wr($value) { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->longWriteRead('4294967296'); - $this->fail('Overflow not detected!'); + $this->wr($value, 'write_signed_short', 'read_signed_short'); } - public function testSignedLongWriteRead() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function signed_short_wr_out_of_range_lower() { - $this->signedLongWriteRead(-2147483648); - $this->signedLongWriteRead(-2147483647); - $this->signedLongWriteRead(2147483646); - $this->signedLongWriteRead(2147483647); - - //values of interest - $this->signedLongWriteRead('-2147483648'); - $this->signedLongWriteRead('-2147483647'); - $this->signedLongWriteRead('-2147483646'); - - $this->signedLongWriteRead('-2'); - $this->signedLongWriteRead('-1'); - $this->signedLongWriteRead('0'); - $this->signedLongWriteRead('1'); - $this->signedLongWriteRead('2'); - - $this->signedLongWriteRead('2147483645'); - $this->signedLongWriteRead('2147483646'); - $this->signedLongWriteRead('2147483647'); + $this->wr(-32769, 'write_signed_short', 'read_signed_short'); } - protected function signedLongWriteRead($v) + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function signed_short_wr_out_of_range_upper() { - $this->writeAndRead($v, 'write_signed_long', 'read_signed_long', true); + $this->wr(32768, 'write_signed_short', 'read_signed_short'); } - public function testSignedLongOutOfRangeLower() + /** + * @dataProvider longWrData + * @test + */ + public function long_wr($value) { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->signedLongWriteRead('-2147483649'); - $this->fail('Overflow not detected!'); + $this->wr($value, 'write_long', 'read_long'); } - public function testSignedLongOutOfRangeUpper() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function long_wr_out_of_range_lower() { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->signedLongWriteRead('2147483648'); - $this->fail('Overflow not detected!'); + $this->wr(-1, 'write_long', 'read_long'); } - public function testLonglongWriteRead() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function long_wr_out_of_range_upper() { - $this->longlongWriteRead(0); - $this->longlongWriteRead(1); - $this->longlongWriteRead((PHP_INT_MAX-1)); - $this->longlongWriteRead(PHP_INT_MAX); - - $this->longlongWriteRead('0'); - $this->longlongWriteRead('1'); - $this->longlongWriteRead('2'); - - $this->longlongWriteRead('2147483646'); - $this->longlongWriteRead('2147483647'); - $this->longlongWriteRead('2147483648'); - - $this->longlongWriteRead('4294967294'); - $this->longlongWriteRead('4294967295'); - $this->longlongWriteRead('4294967296'); - - $this->longlongWriteRead('9223372036854775806'); - $this->longlongWriteRead('9223372036854775807'); - $this->longlongWriteRead('9223372036854775808'); - - $this->longlongWriteRead('18446744073709551613'); - $this->longlongWriteRead('18446744073709551614'); - $this->longlongWriteRead('18446744073709551615'); + $this->wr('4294967296', 'write_long', 'read_long'); } - protected function longlongWriteRead($v) + /** + * @dataProvider signedLongWrData + * @test + */ + public function signed_long_wr($value) { - $this->writeAndRead($v, 'write_longlong', 'read_longlong'); + $this->wr($value, 'write_signed_long', 'read_signed_long', true); } - public function testLonglongOutOfRangeLower() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function signed_long_wr_out_of_range_lower() { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->longlongWriteRead(-1); - $this->fail('Overflow not detected!'); + $this->wr('-2147483649', 'write_signed_long', 'read_signed_long', true); } - public function testLonglongOutOfRangeUpper() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function signed_long_wr_out_of_range_upper() { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->longlongWriteRead('18446744073709551616'); - $this->fail('Overflow not detected!'); + $this->wr('2147483648', 'write_signed_long', 'read_signed_long', true); } - public function testSignedLonglongWriteRead() + /** + * @dataProvider longlongWrData + * @test + */ + public function longlong_wr($value) { - $this->signedLonglongWriteRead(-PHP_INT_MAX - 1); - $this->signedLonglongWriteRead(-PHP_INT_MAX); - $this->signedLonglongWriteRead((PHP_INT_MAX-1)); - $this->signedLonglongWriteRead(PHP_INT_MAX); - - $this->signedLonglongWriteRead('-9223372036854775808'); - $this->signedLonglongWriteRead('-9223372036854775807'); - $this->signedLonglongWriteRead('-9223372036854775806'); - - $this->signedLonglongWriteRead('-4294967297'); - $this->signedLonglongWriteRead('-4294967296'); - $this->signedLonglongWriteRead('-4294967295'); - - $this->signedLonglongWriteRead('-2147483649'); - $this->signedLonglongWriteRead('-2147483648'); - $this->signedLonglongWriteRead('-2147483647'); - - $this->signedLonglongWriteRead('-2'); - $this->signedLonglongWriteRead('-1'); - $this->signedLonglongWriteRead('0'); - $this->signedLonglongWriteRead('1'); - $this->signedLonglongWriteRead('2'); - - $this->signedLonglongWriteRead('2147483646'); - $this->signedLonglongWriteRead('2147483647'); - $this->signedLonglongWriteRead('2147483648'); - - $this->signedLonglongWriteRead('4294967294'); - $this->signedLonglongWriteRead('4294967295'); - $this->signedLonglongWriteRead('4294967296'); - - $this->signedLonglongWriteRead('9223372036854775805'); - $this->signedLonglongWriteRead('9223372036854775806'); - $this->signedLonglongWriteRead('9223372036854775807'); + $this->wr($value, 'write_longlong', 'read_longlong'); } - protected function signedLonglongWriteRead($v) + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function longlong_wr_out_of_range_lower() { - $this->writeAndRead($v, 'write_signed_longlong', 'read_signed_longlong'); + $this->wr('-1', 'write_longlong', 'read_longlong'); } - public function testSignedLonglongOutOfRangeLower() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function longlong_wr_out_of_range_upper() { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->signedLonglongWriteRead('-9223372036854775809'); - $this->fail('Overflow not detected!'); + $this->wr('18446744073709551616', 'write_longlong', 'read_longlong'); } - public function testSignedLonglongOutOfRangeUpper() + /** + * @dataProvider signedLonglongWrData + * @test + */ + public function signed_longlong_wr($value) { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->signedLonglongWriteRead('9223372036854775808'); - $this->fail('Overflow not detected!'); + $this->wr($value, 'write_signed_longlong', 'read_signed_longlong'); } - public function testShortstrWriteRead() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function signed_longlong_wr_out_of_range_lower() { - $this->shortstrWriteRead('a'); - $this->shortstrWriteRead('üıß∑œ´®†¥¨πøˆ¨¥†®'); + $this->wr('-9223372036854775809', 'write_signed_longlong', 'read_signed_longlong'); } - protected function shortstrWriteRead($v) + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function signed_longlong_wr_out_of_range_upper() { - $this->writeAndRead($v, 'write_shortstr', 'read_shortstr'); + $this->wr('9223372036854775808', 'write_signed_longlong', 'read_signed_longlong'); } - public function testShortstrOutOfRangeASCII() + /** + * @dataProvider shortstrWrData + * @test + */ + public function shortstr_wr($value) { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->shortstrWriteRead(str_repeat('z', 256)); - $this->fail('Overflow not detected!'); + $this->wr($value, 'write_shortstr', 'read_shortstr'); } - public function testShortstrOutOfRangeUTFTwoByte() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function shortstr_wr_out_of_range_ASCII() { - $this->setExpectedException('PhpAmqpLib\Exception\AMQPInvalidArgumentException'); - $this->shortstrWriteRead(str_repeat("\xd0\xaf", 128)); - $this->fail('Overflow not detected!'); + $this->wr(str_repeat('z', 256), 'write_shortstr', 'read_shortstr'); } - public function testLongstrWriteRead() + /** + * @test + * @expectedException \PhpAmqpLib\Exception\AMQPInvalidArgumentException + */ + public function shortstr_wr_out_of_range_utf_two_byte() { - $this->longstrWriteRead('a'); - $this->longstrWriteRead('üıß∑œ´®†¥¨πøˆ¨¥†®'); - $this->longstrWriteRead( - 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' . - 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' . - 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' - ); + $this->wr(str_repeat("\xd0\xaf", 128), 'write_shortstr', 'read_shortstr'); } - protected function longstrWriteRead($v) + /** + * @dataProvider longstrWrData + * @test + */ + public function longstr_wr($value) { - $this->writeAndRead($v, 'write_longstr', 'read_longstr'); + $this->wr($value, 'write_longstr', 'read_longstr'); } - public function testArrayWriteReadNative() - { - $d = array(1, -2147483648, 2147483647, true, false, array(array('foo', array('bar'))), array()); + /** + * @test + */ + public function array_wr_native() + { + $d = [ + 1, + -2147483648, + 2147483647, + true, + false, + [['foo', ['bar']]], + [], + ]; $w = new AMQPWriter(); $w->write_array($d); @@ -410,12 +296,15 @@ public function testArrayWriteReadNative() $this->assertEquals($d, $rd); } - public function testArrayWriteReadCollection() + /** + * @test + */ + public function array_wr_collection() { $w = new AMQPWriter(); $w->write_array( new AMQPArray( - array( + [ 12345, -12345, 3000000000, @@ -424,19 +313,19 @@ public function testArrayWriteReadCollection() (float)9223372036854800000, true, false, - array(1, 2, 3, 'foo', array('bar' => 'baz'), array('boo', false, 5), true), - array(), - array( + [1, 2, 3, 'foo', ['bar' => 'baz'], ['boo', false, 5], true], + [], + [ 'foo' => 'bar', 'baz' => 'boo', 'bool' => true, - 'tbl' => array('bar' => 'baz'), - 'arr' => array('boo', false, 5) - ), - array(1 => 5, 3 => 'foo', 786 => 674), - array(1, array(2, array(3, array(4)))), - array('i' => 1, 'n' => array('i' => 2, 'n' => array('i' => 3, 'n' => array('i' => 4)))) - ) + 'tbl' => ['bar' => 'baz'], + 'arr' => ['boo', false, 5] + ], + [1 => 5, 3 => 'foo', 786 => 674], + [1, [2, [3, [4]]]], + ['i' => 1, 'n' => ['i' => 2, 'n' => ['i' => 3, 'n' => ['i' => 4]]]] + ] ) ); @@ -444,7 +333,7 @@ public function testArrayWriteReadCollection() //type casting - thanks to ancient phpunit on travis $this->assertEquals( - array( + [ 12345, -12345, (string)3000000000, @@ -453,37 +342,40 @@ public function testArrayWriteReadCollection() (string)(float)9223372036854800000, true, false, - array(1, 2, 3, 'foo', array('bar' => 'baz'), array('boo', false, 5), true), - array(), - array( + [1, 2, 3, 'foo', ['bar' => 'baz'], ['boo', false, 5], true], + [], + [ 'foo' => 'bar', 'baz' => 'boo', 'bool' => true, - 'tbl' => array('bar' => 'baz'), - 'arr' => array('boo', false, 5) - ), - array(1 => 5, 3 => 'foo', 786 => 674), - array(1, array(2, array(3, array(4)))), - array('i' => 1, 'n' => array('i' => 2, 'n' => array('i' => 3, 'n' => array('i' => 4)))) - ), + 'tbl' => ['bar' => 'baz'], + 'arr' => ['boo', false, 5] + ], + [1 => 5, 3 => 'foo', 786 => 674], + [1, [2, [3, [4]]]], + ['i' => 1, 'n' => ['i' => 2, 'n' => ['i' => 3, 'n' => ['i' => 4]]]] + ], $r->read_array(true)->getNativeData() ); } - public function testTableWriteReadNative() - { - $d = array( - 'a' => array('I', 1), - 'b' => array('I', -2147483648), - 'c' => array('I', 2147483647), - 'd' => array('l', '-2147483649'), - 'e' => array('l', '2147483648'), - 'f' => array('t', true), - 'g' => array('t', false), - 'h' => array('F', array('foo' => array('S', 'baz'))), - 'i' => array('A', array(array('foo', array('bar')))), - 'j' => array('A', array()) - ); + /** + * @test + */ + public function table_wr_native() + { + $d = [ + 'a' => ['I', 1], + 'b' => ['I', -2147483648], + 'c' => ['I', 2147483647], + 'd' => ['l', '-2147483649'], + 'e' => ['l', '2147483648'], + 'f' => ['t', true], + 'g' => ['t', false], + 'h' => ['F', ['foo' => ['S', 'baz']]], + 'i' => ['A', [['foo', ['bar']]]], + 'j' => ['A', []], + ]; $w = new AMQPWriter(); $w->write_table($d); @@ -494,12 +386,15 @@ public function testTableWriteReadNative() $this->assertEquals($d, $rd); } - public function testTableWriteReadCollection() + /** + * @test + */ + public function table_wr_collection() { $w = new AMQPWriter(); $w->write_table( new AMQPTable( - array( + [ 'long' => 12345, 'long_neg' => -12345, 'longlong' => 3000000000, @@ -508,22 +403,22 @@ public function testTableWriteReadCollection() 'float_high' => (float)9223372036854800000, 'bool_true' => true, 'bool_false' => false, - 'array' => array(1, 2, 3, 'foo', array('bar' => 'baz'), array('boo', false, 5), true), - 'array_empty' => array(), - 'table' => array( + 'array' => [1, 2, 3, 'foo', ['bar' => 'baz'], ['boo', false, 5], true], + 'array_empty' => [], + 'table' => [ 'foo' => 'bar', 'baz' => 'boo', 'bool' => true, - 'tbl' => array('bar' => 'baz'), - 'arr' => array('boo', false, 5) - ), - 'table_num' => array(1 => 5, 3 => 'foo', 786 => 674), - 'array_nested' => array(1, array(2, array(3, array(4)))), - 'table_nested' => array( + 'tbl' => ['bar' => 'baz'], + 'arr' => ['boo', false, 5] + ], + 'table_num' => [1 => 5, 3 => 'foo', 786 => 674], + 'array_nested' => [1, [2, [3, [4]]]], + 'table_nested' => [ 'i' => 1, - 'n' => array('i' => 2, 'n' => array('i' => 3, 'n' => array('i' => 4))) - ) - ) + 'n' => ['i' => 2, 'n' => ['i' => 3, 'n' => ['i' => 4]]], + ], + ] ) ); @@ -531,7 +426,7 @@ public function testTableWriteReadCollection() //type casting - thanks to ancient phpunit on travis $this->assertEquals( - array( + [ 'long' => 12345, 'long_neg' => -12345, 'longlong' => (string)3000000000, @@ -540,23 +435,203 @@ public function testTableWriteReadCollection() 'float_high' => (string)(float)9223372036854800000, 'bool_true' => true, 'bool_false' => false, - 'array' => array(1, 2, 3, 'foo', array('bar' => 'baz'), array('boo', false, 5), true), - 'array_empty' => array(), - 'table' => array( + 'array' => [1, 2, 3, 'foo', ['bar' => 'baz'], ['boo', false, 5], true], + 'array_empty' => [], + 'table' => [ 'foo' => 'bar', 'baz' => 'boo', 'bool' => true, - 'tbl' => array('bar' => 'baz'), - 'arr' => array('boo', false, 5) - ), - 'table_num' => array(1 => 5, 3 => 'foo', 786 => 674), - 'array_nested' => array(1, array(2, array(3, array(4)))), - 'table_nested' => array( + 'tbl' => ['bar' => 'baz'], + 'arr' => ['boo', false, 5] + ], + 'table_num' => [1 => 5, 3 => 'foo', 786 => 674], + 'array_nested' => [1, [2, [3, [4]]]], + 'table_nested' => [ 'i' => 1, - 'n' => array('i' => 2, 'n' => array('i' => 3, 'n' => array('i' => 4))) - ) - ), + 'n' => ['i' => 2, 'n' => ['i' => 3, 'n' => ['i' => 4]]] + ] + ], $r->read_table(true)->getNativeData() ); } + + public function bitWrData() + { + return [ + [true], + [false], + ]; + } + + public function octetWrData() + { + $data = []; + for ($i = 0; $i <= 255; $i++) { + $data[] = [$i]; + } + + return $data; + } + + public function signedOctetWrData() + { + $data = []; + for ($i = -128; $i <= 127; $i++) { + $data[] = [$i]; + } + + return $data; + } + + public function signedShortWrData() + { + return [ + [-32768], + [-32767], + [32766], + [32767], + ]; + } + + public function longWrData() + { + $max = PHP_INT_SIZE == 8 ? 4294967295 : PHP_INT_MAX; + + return [ + [0], + [1], + [$max - 1], + [$max], + ['0'], + ['1'], + ['2'], + ['2147483646'], + ['2147483647'], + ['2147483648'], + ['4294967293'], + ['4294967294'], + ['4294967295'], + ]; + } + + public function signedLongWrData() + { + return [ + [-2147483648], + [-2147483647], + [2147483646], + [2147483647], + ['-2147483648'], + ['-2147483647'], + ['-2147483646'], + ['-2'], + ['-1'], + ['0'], + ['1'], + ['2'], + ['2147483645'], + ['2147483646'], + ['2147483647'], + ]; + } + + public function longlongWrData() + { + return [ + [0], + [1], + [PHP_INT_MAX - 1], + [PHP_INT_MAX], + ['0'], + ['1'], + ['2'], + ['2147483646'], + ['2147483647'], + ['2147483648'], + ['4294967294'], + ['4294967295'], + ['4294967296'], + ['9223372036854775806'], + ['9223372036854775807'], + ['9223372036854775808'], + ['18446744073709551613'], + ['18446744073709551614'], + ['18446744073709551615'], + ]; + } + + public function signedLonglongWrData() + { + return [ + [-PHP_INT_MAX - 1], + [-PHP_INT_MAX], + [PHP_INT_MAX - 1], + [PHP_INT_MAX], + ['-9223372036854775808'], + ['-9223372036854775807'], + ['-9223372036854775806'], + ['-4294967297'], + ['-4294967296'], + ['-4294967295'], + ['-2147483649'], + ['-2147483648'], + ['-2147483647'], + ['-2'], + ['-1'], + ['0'], + ['1'], + ['2'], + ['2147483646'], + ['2147483647'], + ['2147483648'], + ['4294967294'], + ['4294967295'], + ['4294967296'], + ['9223372036854775805'], + ['9223372036854775806'], + ['9223372036854775807'], + ]; + } + + public function shortstrWrData() { + return [ + ['a'], + ['üıß∑œ´®†¥¨πøˆ¨¥†®'], + ]; + } + + public function longstrWrData() { + return [ + ['a'], + ['üıß∑œ´®†¥¨πøˆ¨¥†®'], + [ + 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' . + 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' . + 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' + ] + ]; + } + + protected function wr($value, $write_method, $read_method, $reflection = false) + { + $writer = new AMQPWriter(); + if ($reflection) { + $m = new \ReflectionMethod($writer, $write_method); + $m->setAccessible(true); + $m->invoke($writer, $value); + } else { + $writer->{$write_method}($value); + } + + $reader = new AMQPReader($writer->getvalue()); + if ($reflection) { + $m = new \ReflectionMethod($reader, $read_method); + $m->setAccessible(true); + $readValue = $m->invoke($reader); + } else { + $readValue = $reader->{$read_method}(); + } + + $this->assertEquals($value, $readValue); + } }