From 70482c602ac9dea3af8af7ce1020c26c80ae6f63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20V=C3=B6lk?= Date: Tue, 14 May 2019 11:48:13 +0200 Subject: [PATCH] allow assoc array and generator for connection creation --- PhpAmqpLib/Connection/AbstractConnection.php | 14 +++--- .../Connection/ConnectionCreationTest.php | 43 +++++++++++++++++++ 2 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 tests/Functional/Connection/ConnectionCreationTest.php diff --git a/PhpAmqpLib/Connection/AbstractConnection.php b/PhpAmqpLib/Connection/AbstractConnection.php index 5b5b57544..4a7fa42cb 100644 --- a/PhpAmqpLib/Connection/AbstractConnection.php +++ b/PhpAmqpLib/Connection/AbstractConnection.php @@ -1032,13 +1032,13 @@ public function getLibraryProperties() public static function create_connection($hosts, $options = array()){ $latest_exception = null; - for($i = 0; $i < count($hosts); $i++) { - AbstractConnection::validate_host($hosts[$i]); - $host = $hosts[$i]['host']; - $port = $hosts[$i]['port']; - $user = $hosts[$i]['user']; - $password = $hosts[$i]['password']; - $vhost = isset($hosts[$i]['vhost']) ? $hosts[$i]['vhost'] : "/"; + foreach ($hosts as $hostdef) { + AbstractConnection::validate_host($hostdef); + $host = $hostdef['host']; + $port = $hostdef['port']; + $user = $hostdef['user']; + $password = $hostdef['password']; + $vhost = isset($hostdef['vhost']) ? $hostdef['vhost'] : "/"; try { $conn = static::try_create_connection($host, $port, $user, $password, $vhost, $options); return $conn; diff --git a/tests/Functional/Connection/ConnectionCreationTest.php b/tests/Functional/Connection/ConnectionCreationTest.php new file mode 100644 index 000000000..533c24cdf --- /dev/null +++ b/tests/Functional/Connection/ConnectionCreationTest.php @@ -0,0 +1,43 @@ + array( + array( + array('host' => HOST, 'port' => PORT, 'user' => USER, 'password' => PASS), + array('host' => HOST, 'port' => PORT, 'user' => USER, 'password' => PASS) + ) + ), + 'keys' => array( + array( + 'host1' => array('host' => HOST, 'port' => PORT, 'user' => USER, 'password' => PASS), + 'host2' => array('host' => HOST, 'port' => PORT, 'user' => USER, 'password' => PASS) + ) + ) + ); + } + + /** + * @test + * @dataProvider hostDataProvider + * @covers \PhpAmqpLib\Connection\AbstractConnection::create_connection() + */ + public function create_connection(array $hosts) + { + $conn = AMQPStreamConnection::create_connection($hosts, array()); + $this->assertInstanceOf('PhpAmqpLib\Connection\AMQPStreamConnection', $conn); + } +}