Skip to content

Commit

Permalink
amqp-lib tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fibula committed Jul 20, 2017
1 parent e97fd66 commit 1404370
Show file tree
Hide file tree
Showing 13 changed files with 1,262 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*~
/composer.lock
/composer.phar
/phpunit.xml
/vendor/
/.idea/
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sudo: false

git:
depth: 1

language: php

php:
- '5.6'
- '7.0'

cache:
directories:
- $HOME/.composer/cache

install:
- composer self-update
- composer install --prefer-source --ignore-platform-reqs

script:
- vendor/bin/phpunit --exclude-group=functional
7 changes: 3 additions & 4 deletions AmqpConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,11 @@ private function parseDsn($dsn)

unset($dsnConfig['scheme'], $dsnConfig['query'], $dsnConfig['fragment'], $dsnConfig['path']);

$config = array_replace($this->defaultConfig(), $dsnConfig);
$config = array_map(function ($value) {
$dsnConfig = array_map(function ($value) {
return urldecode($value);
}, $config);
}, $dsnConfig);

return $config;
return $dsnConfig;
}

/**
Expand Down
1 change: 1 addition & 0 deletions AmqpTopic.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class AmqpTopic implements PsrTopic
public function __construct($name)
{
$this->name = $name;
$this->type = 'direct';
$this->passive = false;
$this->durable = false;
$this->autoDelete = true;
Expand Down
281 changes: 281 additions & 0 deletions Tests/AmqpConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
<?php

namespace Enqueue\AmqpLib\Tests;

use Enqueue\AmqpLib\AmqpConnectionFactory;
use Enqueue\Test\ClassExtensionTrait;
use PHPUnit\Framework\TestCase;

/**
* The class contains the factory tests dedicated to configuration.
*/
class AmqpConnectionFactoryConfigTest extends TestCase
{
use ClassExtensionTrait;

public function testThrowNeitherArrayStringNorNullGivenAsConfig()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The config must be either an array of options, a DSN string or null');

new AmqpConnectionFactory(new \stdClass());
}

public function testThrowIfSchemeIsNotAmqp()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The given DSN scheme "http" is not supported. Could be "amqp" only.');

new AmqpConnectionFactory('http://example.com');
}

public function testThrowIfDsnCouldNotBeParsed()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Failed to parse DSN "amqp://:@/"');

new AmqpConnectionFactory('amqp://:@/');
}

public function testThrowIfReceiveMenthodIsInvalid()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Invalid "receive_method" option value "invalidMethod". It could be only "basic_get", "basic_consume"');

new AmqpConnectionFactory(['receive_method' => 'invalidMethod']);
}

/**
* @dataProvider provideConfigs
*
* @param mixed $config
* @param mixed $expectedConfig
*/
public function testShouldParseConfigurationAsExpected($config, $expectedConfig)
{
$factory = new AmqpConnectionFactory($config);

$this->assertAttributeEquals($expectedConfig, 'config', $factory);
}

public static function provideConfigs()
{
yield [
null,
[
'host' => 'localhost',
'port' => 5672,
'vhost' => '/',
'user' => 'guest',
'pass' => 'guest',
'read_timeout' => 3,
'write_timeout' => 3,
'lazy' => true,
'receive_method' => 'basic_get',
'stream' => true,
'insist' => false,
'login_method' => 'AMQPLAIN',
'login_response' => null,
'locale' => 'en_US',
'keepalive' => false,
'heartbeat' => 0,
'connection_timeout' => 3.0,
'read_write_timeout' => 3.0,
],
];

// some examples from Appendix A: Examples (https://www.rabbitmq.com/uri-spec.html)

yield [
'amqp://user:pass@host:10000/vhost',
[
'host' => 'host',
'port' => 10000,
'vhost' => 'vhost',
'user' => 'user',
'pass' => 'pass',
'read_timeout' => 3,
'write_timeout' => 3,
'lazy' => true,
'receive_method' => 'basic_get',
'stream' => true,
'insist' => false,
'login_method' => 'AMQPLAIN',
'login_response' => null,
'locale' => 'en_US',
'keepalive' => false,
'heartbeat' => 0,
'connection_timeout' => 3.0,
'read_write_timeout' => 3.0,
],
];

yield [
'amqp://user%61:%61pass@ho%61st:10000/v%2fhost',
[
'host' => 'hoast',
'port' => 10000,
'vhost' => 'v/host',
'user' => 'usera',
'pass' => 'apass',
'read_timeout' => 3,
'write_timeout' => 3,
'lazy' => true,
'receive_method' => 'basic_get',
'stream' => true,
'insist' => false,
'login_method' => 'AMQPLAIN',
'login_response' => null,
'locale' => 'en_US',
'keepalive' => false,
'heartbeat' => 0,
'connection_timeout' => 3.0,
'read_write_timeout' => 3.0,
],
];

yield [
'amqp://',
[
'host' => 'localhost',
'port' => 5672,
'vhost' => '/',
'user' => 'guest',
'pass' => 'guest',
'read_timeout' => 3,
'write_timeout' => 3,
'lazy' => true,
'receive_method' => 'basic_get',
'stream' => true,
'insist' => false,
'login_method' => 'AMQPLAIN',
'login_response' => null,
'locale' => 'en_US',
'keepalive' => false,
'heartbeat' => 0,
'connection_timeout' => 3.0,
'read_write_timeout' => 3.0,
],
];

yield [
'amqp://user:pass@host:10000/vhost?connection_timeout=2&lazy=',
[
'host' => 'host',
'port' => 10000,
'vhost' => 'vhost',
'user' => 'user',
'pass' => 'pass',
'read_timeout' => 3,
'write_timeout' => 3,
'lazy' => '',
'receive_method' => 'basic_get',
'stream' => true,
'insist' => false,
'login_method' => 'AMQPLAIN',
'login_response' => null,
'locale' => 'en_US',
'keepalive' => false,
'heartbeat' => 0,
'connection_timeout' => '2',
'read_write_timeout' => 3.0,
],
];

yield [
[],
[
'host' => 'localhost',
'port' => 5672,
'vhost' => '/',
'user' => 'guest',
'pass' => 'guest',
'read_timeout' => 3,
'write_timeout' => 3,
'lazy' => true,
'receive_method' => 'basic_get',
'stream' => true,
'insist' => false,
'login_method' => 'AMQPLAIN',
'login_response' => null,
'locale' => 'en_US',
'keepalive' => false,
'heartbeat' => 0,
'connection_timeout' => 3.0,
'read_write_timeout' => 3.0,
],
];

yield [
['lazy' => false, 'host' => 'host'],
[
'host' => 'host',
'port' => 5672,
'vhost' => '/',
'user' => 'guest',
'pass' => 'guest',
'read_timeout' => 3,
'write_timeout' => 3,
'lazy' => false,
'receive_method' => 'basic_get',
'stream' => true,
'insist' => false,
'login_method' => 'AMQPLAIN',
'login_response' => null,
'locale' => 'en_US',
'keepalive' => false,
'heartbeat' => 0,
'connection_timeout' => 3.0,
'read_write_timeout' => 3.0,
],
];

yield [
['connection_timeout' => 123, 'read_write_timeout' => 321],
[
'host' => 'localhost',
'port' => 5672,
'vhost' => '/',
'user' => 'guest',
'pass' => 'guest',
'read_timeout' => 3,
'write_timeout' => 3,
'lazy' => true,
'receive_method' => 'basic_get',
'stream' => true,
'insist' => false,
'login_method' => 'AMQPLAIN',
'login_response' => null,
'locale' => 'en_US',
'keepalive' => false,
'heartbeat' => 0,
'connection_timeout' => 123,
'read_write_timeout' => 321,
],
];

yield [
'amqp://user:pass@host:10000/vhost?connection_timeout=123&read_write_timeout=321',
[
'host' => 'host',
'port' => 10000,
'vhost' => 'vhost',
'user' => 'user',
'pass' => 'pass',
'read_timeout' => 3,
'write_timeout' => 3,
'lazy' => true,
'receive_method' => 'basic_get',
'stream' => true,
'insist' => false,
'login_method' => 'AMQPLAIN',
'login_response' => null,
'locale' => 'en_US',
'keepalive' => false,
'heartbeat' => 0,
'connection_timeout' => '123',
'read_write_timeout' => '321',
],
];
}
}
Loading

0 comments on commit 1404370

Please sign in to comment.