Skip to content

Commit 92c17e6

Browse files
goetasskafandri
authored andcommitted
AMQPSocketConnection has a different parameters order, lets consider it (#527)
* AMQPSocketConnection has a different parameters order, lets consider it * allow generic parameters to be passed to connection constructors
1 parent 7471ccf commit 92c17e6

File tree

4 files changed

+139
-16
lines changed

4 files changed

+139
-16
lines changed

Provider/ConnectionParametersProviderInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ interface ConnectionParametersProviderInterface
2525
* 'keepalive' => false,
2626
* 'heartbeat' => 0,
2727
* 'use_socket' => true,
28+
* 'constructor_args' => array(...)
2829
* )
2930
*
31+
* If constructor_args is present, all the other parameters are ignored; constructor_args are passes as constructor
32+
* arguments.
33+
*
3034
* @return array
3135
*/
3236
public function getConnectionParameters();

RabbitMq/AMQPConnectionFactory.php

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,45 @@ public function __construct(
6060
*/
6161
public function createConnection()
6262
{
63-
return new $this->class(
64-
$this->parameters['host'],
65-
$this->parameters['port'],
66-
$this->parameters['user'],
67-
$this->parameters['password'],
68-
$this->parameters['vhost'],
69-
false, // insist
70-
'AMQPLAIN', // login_method
71-
null, // login_response
72-
'en_US', // locale
73-
$this->parameters['connection_timeout'],
74-
$this->parameters['read_write_timeout'],
75-
$this->parameters['ssl_context'],
76-
$this->parameters['keepalive'],
77-
$this->parameters['heartbeat']
78-
);
63+
if (isset($this->parameters['constructor_args']) && is_array($this->parameters['constructor_args'])) {
64+
$ref = new \ReflectionClass($this->class);
65+
return $ref->newInstanceArgs($this->parameters['constructor_args']);
66+
}
67+
68+
if ($this->class == 'PhpAmqpLib\Connection\AMQPSocketConnection' || is_subclass_of($this->class , 'PhpAmqpLib\Connection\AMQPSocketConnection')) {
69+
return new $this->class(
70+
$this->parameters['host'],
71+
$this->parameters['port'],
72+
$this->parameters['user'],
73+
$this->parameters['password'],
74+
$this->parameters['vhost'],
75+
false, // insist
76+
'AMQPLAIN', // login_method
77+
null, // login_response
78+
'en_US', // locale
79+
isset($this->parameters['read_timeout']) ? $this->parameters['read_timeout'] : $this->parameters['read_write_timeout'],
80+
$this->parameters['keepalive'],
81+
isset($this->parameters['write_timeout']) ? $this->parameters['write_timeout'] : $this->parameters['read_write_timeout'],
82+
$this->parameters['heartbeat']
83+
);
84+
} else {
85+
return new $this->class(
86+
$this->parameters['host'],
87+
$this->parameters['port'],
88+
$this->parameters['user'],
89+
$this->parameters['password'],
90+
$this->parameters['vhost'],
91+
false, // insist
92+
'AMQPLAIN', // login_method
93+
null, // login_response
94+
'en_US', // locale
95+
$this->parameters['connection_timeout'],
96+
$this->parameters['read_write_timeout'],
97+
$this->parameters['ssl_context'],
98+
$this->parameters['keepalive'],
99+
$this->parameters['heartbeat']
100+
);
101+
}
79102
}
80103

81104
/**

Tests/RabbitMq/AMQPConnectionFactoryTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use OldSound\RabbitMqBundle\Provider\ConnectionParametersProviderInterface;
66
use OldSound\RabbitMqBundle\RabbitMq\AMQPConnectionFactory;
77
use OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection;
8+
use OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPSocketConnection;
89
use PHPUnit\Framework\TestCase;
910

1011
class AMQPConnectionFactoryTest extends TestCase
@@ -37,6 +38,63 @@ public function testDefaultValues()
3738
), $instance->constructParams);
3839
}
3940

41+
public function testSocketConnection()
42+
{
43+
$factory = new AMQPConnectionFactory(
44+
'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPSocketConnection',
45+
array()
46+
);
47+
48+
/** @var AMQPSocketConnection $instance */
49+
$instance = $factory->createConnection();
50+
$this->assertInstanceOf('PhpAmqpLib\Connection\AMQPSocketConnection', $instance);
51+
$this->assertEquals(array(
52+
'localhost', // host
53+
5672, // port
54+
'guest', // user
55+
'guest', // password
56+
'/', // vhost
57+
false, // insist
58+
"AMQPLAIN", // login method
59+
null, // login response
60+
"en_US", // locale
61+
3, // read_timeout
62+
false, // keepalive
63+
3, // write_timeout
64+
0, // heartbeat
65+
), $instance->constructParams);
66+
}
67+
68+
public function testSocketConnectionWithParams()
69+
{
70+
$factory = new AMQPConnectionFactory(
71+
'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPSocketConnection',
72+
array(
73+
'read_timeout' => 31,
74+
'write_timeout' => 32,
75+
)
76+
);
77+
78+
/** @var AMQPSocketConnection $instance */
79+
$instance = $factory->createConnection();
80+
$this->assertInstanceOf('PhpAmqpLib\Connection\AMQPSocketConnection', $instance);
81+
$this->assertEquals(array(
82+
'localhost', // host
83+
5672, // port
84+
'guest', // user
85+
'guest', // password
86+
'/', // vhost
87+
false, // insist
88+
"AMQPLAIN", // login method
89+
null, // login response
90+
"en_US", // locale
91+
31, // read_timeout
92+
false, // keepalive
93+
32, // write_timeout
94+
0, // heartbeat
95+
), $instance->constructParams);
96+
}
97+
4098
public function testStandardConnectionParameters()
4199
{
42100
$factory = new AMQPConnectionFactory(
@@ -211,6 +269,28 @@ public function testSSLConnectionParameters()
211269
), $instance->constructParams);
212270
}
213271

272+
public function testConnectionsParametersProviderWithConstructorArgs()
273+
{
274+
$connectionParametersProvider = $this->prepareConnectionParametersProvider();
275+
$connectionParametersProvider->expects($this->once())
276+
->method('getConnectionParameters')
277+
->will($this->returnValue(
278+
array(
279+
'constructor_args' => array(1,2,3,4)
280+
)
281+
));
282+
$factory = new AMQPConnectionFactory(
283+
'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
284+
array(),
285+
$connectionParametersProvider
286+
);
287+
288+
/** @var AMQPConnection $instance */
289+
$instance = $factory->createConnection();
290+
$this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
291+
$this->assertEquals(array(1,2,3,4), $instance->constructParams);
292+
}
293+
214294
public function testConnectionsParametersProvider()
215295
{
216296
$connectionParametersProvider = $this->prepareConnectionParametersProvider();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures;
4+
5+
use PhpAmqpLib\Connection\AMQPSocketConnection as BaseAMQPSocketConnection;
6+
7+
class AMQPSocketConnection extends BaseAMQPSocketConnection
8+
{
9+
public $constructParams;
10+
11+
public function __construct()
12+
{
13+
// save params for direct access in tests
14+
$this->constructParams = func_get_args();
15+
}
16+
}

0 commit comments

Comments
 (0)