Skip to content

AMQPSocketConnection has a different parameters order, lets consider it #527

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

Merged
merged 2 commits into from
May 2, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Provider/ConnectionParametersProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ interface ConnectionParametersProviderInterface
* 'keepalive' => false,
* 'heartbeat' => 0,
* 'use_socket' => true,
* 'constructor_args' => array(...)
* )
*
* If constructor_args is present, all the other parameters are ignored; constructor_args are passes as constructor
* arguments.
*
* @return array
*/
public function getConnectionParameters();
Expand Down
55 changes: 39 additions & 16 deletions RabbitMq/AMQPConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,45 @@ public function __construct(
*/
public function createConnection()
{
return new $this->class(
$this->parameters['host'],
$this->parameters['port'],
$this->parameters['user'],
$this->parameters['password'],
$this->parameters['vhost'],
false, // insist
'AMQPLAIN', // login_method
null, // login_response
'en_US', // locale
$this->parameters['connection_timeout'],
$this->parameters['read_write_timeout'],
$this->parameters['ssl_context'],
$this->parameters['keepalive'],
$this->parameters['heartbeat']
);
if (isset($this->parameters['constructor_args']) && is_array($this->parameters['constructor_args'])) {
$ref = new \ReflectionClass($this->class);
return $ref->newInstanceArgs($this->parameters['constructor_args']);
}

if ($this->class == 'PhpAmqpLib\Connection\AMQPSocketConnection' || is_subclass_of($this->class , 'PhpAmqpLib\Connection\AMQPSocketConnection')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not know really how to test this since will try to connect to rabbtmq

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I forgot it extends from AbstractConnection

return new $this->class(
$this->parameters['host'],
$this->parameters['port'],
$this->parameters['user'],
$this->parameters['password'],
$this->parameters['vhost'],
false, // insist
'AMQPLAIN', // login_method
null, // login_response
'en_US', // locale
isset($this->parameters['read_timeout']) ? $this->parameters['read_timeout'] : $this->parameters['read_write_timeout'],
$this->parameters['keepalive'],
isset($this->parameters['write_timeout']) ? $this->parameters['write_timeout'] : $this->parameters['read_write_timeout'],
$this->parameters['heartbeat']
);
} else {
return new $this->class(
$this->parameters['host'],
$this->parameters['port'],
$this->parameters['user'],
$this->parameters['password'],
$this->parameters['vhost'],
false, // insist
'AMQPLAIN', // login_method
null, // login_response
'en_US', // locale
$this->parameters['connection_timeout'],
$this->parameters['read_write_timeout'],
$this->parameters['ssl_context'],
$this->parameters['keepalive'],
$this->parameters['heartbeat']
);
}
}

/**
Expand Down
80 changes: 80 additions & 0 deletions Tests/RabbitMq/AMQPConnectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use OldSound\RabbitMqBundle\Provider\ConnectionParametersProviderInterface;
use OldSound\RabbitMqBundle\RabbitMq\AMQPConnectionFactory;
use OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection;
use OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPSocketConnection;
use PHPUnit\Framework\TestCase;

class AMQPConnectionFactoryTest extends TestCase
Expand Down Expand Up @@ -37,6 +38,63 @@ public function testDefaultValues()
), $instance->constructParams);
}

public function testSocketConnection()
{
$factory = new AMQPConnectionFactory(
'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPSocketConnection',
array()
);

/** @var AMQPSocketConnection $instance */
$instance = $factory->createConnection();
$this->assertInstanceOf('PhpAmqpLib\Connection\AMQPSocketConnection', $instance);
$this->assertEquals(array(
'localhost', // host
5672, // port
'guest', // user
'guest', // password
'/', // vhost
false, // insist
"AMQPLAIN", // login method
null, // login response
"en_US", // locale
3, // read_timeout
false, // keepalive
3, // write_timeout
0, // heartbeat
), $instance->constructParams);
}

public function testSocketConnectionWithParams()
{
$factory = new AMQPConnectionFactory(
'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPSocketConnection',
array(
'read_timeout' => 31,
'write_timeout' => 32,
)
);

/** @var AMQPSocketConnection $instance */
$instance = $factory->createConnection();
$this->assertInstanceOf('PhpAmqpLib\Connection\AMQPSocketConnection', $instance);
$this->assertEquals(array(
'localhost', // host
5672, // port
'guest', // user
'guest', // password
'/', // vhost
false, // insist
"AMQPLAIN", // login method
null, // login response
"en_US", // locale
31, // read_timeout
false, // keepalive
32, // write_timeout
0, // heartbeat
), $instance->constructParams);
}

public function testStandardConnectionParameters()
{
$factory = new AMQPConnectionFactory(
Expand Down Expand Up @@ -211,6 +269,28 @@ public function testSSLConnectionParameters()
), $instance->constructParams);
}

public function testConnectionsParametersProviderWithConstructorArgs()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this tests covers already constructor_args. the clas name has to be OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection, using any other connection will try to connect to rabbitmq

{
$connectionParametersProvider = $this->prepareConnectionParametersProvider();
$connectionParametersProvider->expects($this->once())
->method('getConnectionParameters')
->will($this->returnValue(
array(
'constructor_args' => array(1,2,3,4)
)
));
$factory = new AMQPConnectionFactory(
'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
array(),
$connectionParametersProvider
);

/** @var AMQPConnection $instance */
$instance = $factory->createConnection();
$this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
$this->assertEquals(array(1,2,3,4), $instance->constructParams);
}

public function testConnectionsParametersProvider()
{
$connectionParametersProvider = $this->prepareConnectionParametersProvider();
Expand Down
16 changes: 16 additions & 0 deletions Tests/RabbitMq/Fixtures/AMQPSocketConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures;

use PhpAmqpLib\Connection\AMQPSocketConnection as BaseAMQPSocketConnection;

class AMQPSocketConnection extends BaseAMQPSocketConnection
{
public $constructParams;

public function __construct()
{
// save params for direct access in tests
$this->constructParams = func_get_args();
}
}