Skip to content

Commit

Permalink
Merge pull request #689 from black-silence/create_connection_foreach
Browse files Browse the repository at this point in the history
allow assoc array and generator for connection creation
  • Loading branch information
lukebakken committed May 22, 2019
2 parents 461e79d + 70482c6 commit 8d4d929
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
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);
}
}

0 comments on commit 8d4d929

Please sign in to comment.