Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow assoc array and generator for connection creation #689

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions PhpAmqpLib/Connection/AbstractConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions tests/Functional/Connection/ConnectionCreationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace PhpAmqpLib\Tests\Functional\Connection;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Tests\Functional\AbstractConnectionTest;

/**
* @group connection
*/
class ConnectionCreationTest extends AbstractConnectionTest
{

public function hostDataProvider()
{
return array(
'plain' => 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);
}
}