Skip to content

Commit

Permalink
Fix test for provider configuration, thanks to @Mulkave
Browse files Browse the repository at this point in the history
  • Loading branch information
harris21 committed Jan 14, 2019
1 parent 56a15e1 commit a2af70e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 80 deletions.
20 changes: 10 additions & 10 deletions src/BowlerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
45 changes: 29 additions & 16 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
80 changes: 29 additions & 51 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
// }
}
3 changes: 0 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit a2af70e

Please sign in to comment.