diff --git a/src/BowlerServiceProvider.php b/src/BowlerServiceProvider.php index a5a0645..4a18ba9 100644 --- a/src/BowlerServiceProvider.php +++ b/src/BowlerServiceProvider.php @@ -30,16 +30,16 @@ public function register() return $app['vinelab.bowler.registrator']; }); - // Bind connection to env configuration - $rbmqHost = config('queue.connections.rabbitmq.host'); - $rbmqPort = config('queue.connections.rabbitmq.port'); - $rbmqUsername = config('queue.connections.rabbitmq.username'); - $rbmqPassword = config('queue.connections.rabbitmq.password'); - $rbmqConnectionTimeout = config('queue.connections.rabbitmq.connection_timeout') ? (int) config('queue.connections.rabbitmq.connection_timeout') : 30; - $rbmqReadWriteTimeout = config('queue.connections.rabbitmq.read_write_timeout') ? (int) config('queue.connections.rabbitmq.read_write_timeout') : 30; - $rbmqHeartbeat = config('queue.connections.rabbitmq.heartbeat') ? (int) config('queue.connections.rabbitmq.heartbeat') : 15; - - $this->app->bind(Connection::class, function () use ($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat) { + $this->app->bind(Connection::class, function () { + // Bind connection to env configuration + $rbmqHost = config('queue.connections.rabbitmq.host'); + $rbmqPort = config('queue.connections.rabbitmq.port'); + $rbmqUsername = config('queue.connections.rabbitmq.username'); + $rbmqPassword = config('queue.connections.rabbitmq.password'); + $rbmqConnectionTimeout = config('queue.connections.rabbitmq.connection_timeout') ? (int) config('queue.connections.rabbitmq.connection_timeout') : 30; + $rbmqReadWriteTimeout = config('queue.connections.rabbitmq.read_write_timeout') ? (int) config('queue.connections.rabbitmq.read_write_timeout') : 30; + $rbmqHeartbeat = config('queue.connections.rabbitmq.heartbeat') ? (int) config('queue.connections.rabbitmq.heartbeat') : 15; + return new Connection($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat); }); diff --git a/src/Connection.php b/src/Connection.php index 6a289d5..b4caf4b 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -98,22 +98,35 @@ public function __construct($host = 'localhost', $port = 5672, $username = 'gues $this->readWriteTimeout = $readWriteTimeout; $this->heartbeat = $heartbeat; - $this->connection = new AMQPStreamConnection( - $host, - $port, - $username, - $password, - $vhost = '/', - $insist = false, - $login_method = 'AMQPLAIN', - $login_response = null, - $locale = 'en_US', - $connectionTimeout, - $readWriteTimeout, - $context = null, - $keepalive = false, - $heartbeat - ); + $this->initAMQPStreamConnection($host, $port, $username, $password, $connectionTimeout, $readWriteTimeout, $heartbeat); + } + + protected function initAMQPStreamConnection($host, $port, $username, $password, $connectionTimeout, $readWriteTimeout, $heartbeat, $vhost = '/',$insist = false, $login_method = 'AMQPLAIN', $login_response = null, $locale = 'en_US', $context = null, $keepalive = false) + { + $vhost = '/'; + $insist = false; + $login_method = 'AMQPLAIN'; + $login_response = null; + $locale = 'en_US'; + $context = null; + $keepalive = false; + + $this->connection = app()->makeWith(AMQPStreamConnection::class, [ + 'host' => $host, + 'port' => $port, + 'user' => $username, + 'password' => $password, + 'vhost' => $vhost, + 'insist' => $insist, + 'login_method' => $login_method, + 'login_response' => $login_response, + 'locale' => $locale, + 'connection_timeout' => $connectionTimeout, + 'read_write_timeout' => $readWriteTimeout, + 'context' => $context, + 'keepalive' => $keepalive, + 'heartbeat' => $heartbeat, + ]); $this->channel = $this->connection->channel(); } diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 9a7b4ed..0cc3ddc 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -6,6 +6,7 @@ use ReflectionClass; use Vinelab\Bowler\Connection; use PhpAmqpLib\Wire\IO\StreamIO; +use Illuminate\Broadcasting\Channel; use Illuminate\Support\Facades\Config; use Vinelab\Http\Client as HTTPClient; use PhpAmqpLib\Connection\AMQPStreamConnection; @@ -53,65 +54,42 @@ public function test_fetching_consumers_default() public function test_set_default_configurations_values() { + $mConnection = $this->getMockBuilder(Connection::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->app->bind(Connection::class, function () use ($mConnection) { + return $mConnection; + }); $connection = $this->app[Connection::class]; - $this->assertEquals(30, $this->getProtectedProperty($connection, 'readWriteTimeout')); - $this->assertEquals(30, $this->getProtectedProperty($connection, 'connectionTimeout')); - $this->assertEquals(15, $this->getProtectedProperty($connection, 'heartbeat')); + + $this->assertAttributeEquals(15, 'heartbeat', $connection); + $this->assertAttributeEquals(30, 'readWriteTimeout', $connection); + $this->assertAttributeEquals(30, 'connectionTimeout', $connection); } public function test_set_altered_configurations_values() { - Config::set('queue.connections.rabbitmq.host', 'localhost'); - Config::set('queue.connections.rabbitmq.port', 5672); - Config::set('queue.connections.rabbitmq.username', 'default'); - Config::set('queue.connections.rabbitmq.password', 'default'); - // Config::set('queue.connections.rabbitmq.read_write_timeout', 60); - // Config::set('queue.connections.rabbitmq.connection_timeout', 60); - // Config::set('queue.connections.rabbitmq.heartbeat', 30); - - // $this->app['config']->set('queue.connections.rabbitmq.read_write_timeout', 120); - // $this->app['config']->set('queue.connections.rabbitmq.connection_timeout', 120); - - $rbmqHost = config('queue.connections.rabbitmq.host'); - $rbmqPort = config('queue.connections.rabbitmq.port'); - $rbmqUsername = config('queue.connections.rabbitmq.username'); - $rbmqPassword = config('queue.connections.rabbitmq.password'); - $rbmqConnectionTimeout = config('queue.connections.rabbitmq.connection_timeout'); - $rbmqReadWriteTimeout = config('queue.connections.rabbitmq.read_write_timeout'); - $rbmqHeartbeat = config('queue.connections.rabbitmq.heartbeat'); - - $this->app->bind(AMQPStreamConnection::class, function () use ($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, $rbmqConnectionTimeout, $rbmqReadWriteTimeout, $rbmqHeartbeat) { - return new AMQPStreamConnection($rbmqHost, $rbmqPort, $rbmqUsername, $rbmqPassword, '/', false, 'AMQPLAIN', null, 'en_US', config('queue.connections.rabbitmq.connection_timeout'), config('queue.connections.rabbitmq.read_write_timeout'), null, false, config('queue.connections.rabbitmq.heartbeat')); + Config::set('queue.connections.rabbitmq.host', 'notlocal'); + Config::set('queue.connections.rabbitmq.port', 6666); + Config::set('queue.connections.rabbitmq.read_write_timeout', 60); + Config::set('queue.connections.rabbitmq.connection_timeout', 60); + Config::set('queue.connections.rabbitmq.heartbeat', 30); + + $mAMQPStreamConnection = M::mock(AMQPStreamConnection::class); + $this->app->bind(AMQPStreamConnection::class, function () use ($mAMQPStreamConnection) { + return $mAMQPStreamConnection; }); + $mChannel = M::mock(Channel::class); + $mAMQPStreamConnection->shouldReceive('channel')->once()->withNoArgs()->andReturn($mChannel); + $mAMQPStreamConnection->shouldReceive('close')->once()->withNoArgs(); + $mChannel->shouldReceive('close')->once()->withNoArgs(); + $connection = $this->app[Connection::class]; - - $conn = $connection->getConnection(); - $io = $this->getProtectedProperty($conn, 'io'); - $this->assertEquals(60, $this->getProtectedProperty($io, 'read_write_timeout')); - $this->assertEquals(60, $this->getProtectedProperty($io, 'connection_timeout')); - $this->assertEquals(30, $this->getProtectedProperty($io, 'heartbeat')); + $this->assertAttributeEquals(30, 'heartbeat', $connection); + $this->assertAttributeEquals(60, 'readWriteTimeout', $connection); + $this->assertAttributeEquals(60, 'connectionTimeout', $connection); } - - protected static function getProtectedProperty($class, $value) - { - $reflection = new ReflectionClass($class); - $property = $reflection->getProperty($value); - $property->setAccessible(true); - return $property->getValue($class); - } - - // protected function getEnvironmentSetUp($app) - // { - // parent::getEnvironmentSetUp($app); - - // $app['config']->set('queue.connections.rabbitmq.host', 'localhost'); - // $app['config']->set('queue.connections.rabbitmq.port', 5672); - // $app['config']->set('queue.connections.rabbitmq.username', 'guest'); - // $app['config']->set('queue.connections.rabbitmq.password', 'guest'); - // $app['config']->set('queue.connections.rabbitmq.heartbeat', 30); - // $app['config']->set('queue.connections.rabbitmq.read_write_timeout', 60); - // $app['config']->set('queue.connections.rabbitmq.connection_timeout', 60); - // } } diff --git a/tests/TestCase.php b/tests/TestCase.php index a0f7207..f9dc6b7 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -35,8 +35,5 @@ protected function getEnvironmentSetUp($app) $app['config']->set('queue.connections.rabbitmq.port', 5672); $app['config']->set('queue.connections.rabbitmq.username', 'guest'); $app['config']->set('queue.connections.rabbitmq.password', 'guest'); - // $app['config']->set('queue.connections.rabbitmq.heartbeat', 15); - // $app['config']->set('queue.connections.rabbitmq.read_write_timeout', 30); - // $app['config']->set('queue.connections.rabbitmq.connection_timeout', 30); } }