Skip to content

Commit

Permalink
add tests, remove functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Aug 20, 2018
1 parent a1207d7 commit 5b3835a
Show file tree
Hide file tree
Showing 27 changed files with 370 additions and 338 deletions.
3 changes: 0 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@
},
"exclude-from-classmap": [
"/Tests/"
],
"files": [
"pkg/enqueue/functions_include.php"
]
},
"autoload-dev": {
Expand Down
6 changes: 3 additions & 3 deletions docs/transport/amqp.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ $factory = new AmqpConnectionFactory([

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('amqp:');
$psrContext = \Enqueue\dsn_to_context('amqp+ext:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('amqp:')->createContext();
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('amqp+ext:')->createContext();
```

## Declare topic.
Expand Down
6 changes: 3 additions & 3 deletions docs/transport/amqp_bunny.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ $factory = new AmqpConnectionFactory('amqp://user:[email protected]:10000/%2f');

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('amqp:');
$psrContext = \Enqueue\dsn_to_context('amqp+bunny:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('amqp:')->createContext();
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('amqp+bunny:')->createContext();
```

## Declare topic.
Expand Down
6 changes: 3 additions & 3 deletions docs/transport/amqp_lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ $factory = new AmqpConnectionFactory([

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('amqp:');
$psrContext = \Enqueue\dsn_to_context('amqp+lib:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('amqp:')->createContext();
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('amqp+lib:')->createContext();
```

## Declare topic.
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/dbal.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ $factory = new ManagerRegistryConnectionFactory($registry, [

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('mysql:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('mysql:')->createContext();
```

## Init database
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/filesystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ $connectionFactory = new FsConnectionFactory([

$psrContext = $connectionFactory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('file:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('file:')->createContext();
```

## Send message to topic
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/gearman.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ $factory = new GearmanConnectionFactory([

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('gearman:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('gearman:')->createContext();
```

## Send message to topic
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/gps.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ $connectionFactory = new GpsConnectionFactory('gps:');

$psrContext = $connectionFactory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('gps:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('gps:')->createContext();
```

## Send message to topic
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/kafka.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ $connectionFactory = new RdKafkaConnectionFactory([

$psrContext = $connectionFactory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('kafka:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('kafka:')->createContext();
```

## Send message to topic
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/mongodb.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ $factory = new MongodbConnectionFactory([

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('mongodb:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('mongodb:')->createContext();
```

## Send message to topic
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/pheanstalk.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ $factory = new PheanstalkConnectionFactory([

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('beanstalk:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('beanstalk:')->createContext();
```

## Send message to topic
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ $factory = new RedisConnectionFactory('redis://example.com:1000?vendor=phpredis'

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('redis:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('redis:')->createContext();

// pass redis instance directly
$redis = new \Enqueue\Redis\PhpRedis([ /** redis connection options */ ]);
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/sqs.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ $psrContext = $factory->createContext();
$client = new Aws\Sqs\SqsClient([ /* ... */ ]);
$factory = new SqsConnectionFactory($client);

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('sqs:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('sqs:')->createContext();
```

## Declare queue.
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/stomp.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ $factory = new StompConnectionFactory('stomp://example.com:1000?login=theLogin')

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('stomp:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('stomp:')->createContext();
```

## Send message to topic
Expand Down
8 changes: 4 additions & 4 deletions pkg/dsn/Dsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,18 @@ private function parse(string $dsn): void
}

list($scheme, $dsnWithoutScheme) = explode(':', $dsn, 2);
if (false == preg_match('/[\w\d+-.]/', $scheme)) {
throw new \LogicException('The DSN is invalid. Scheme contains illegal symbols.');
}

$scheme = strtolower($scheme);
if (false == preg_match('/^[a-z\d+-.]*$/', $scheme)) {
throw new \LogicException('The DSN is invalid. Scheme contains illegal symbols.');
}

$schemeParts = explode('+', $scheme);
$this->scheme = $scheme;
$this->schemeProtocol = $schemeParts[0];

unset($schemeParts[0]);
$this->schemeExtensions = $schemeParts;
$this->schemeExtensions = array_values($schemeParts);

if ($host = parse_url($dsn, PHP_URL_HOST)) {
$this->host = $host;
Expand Down
93 changes: 93 additions & 0 deletions pkg/dsn/Tests/DsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,101 @@

namespace Enqueue\Dsn\Tests;

use Enqueue\Dsn\Dsn;
use PHPUnit\Framework\TestCase;

class DsnTest extends TestCase
{
public function testCouldBeConstructedWithDsnAsFirstArgument()
{
new Dsn('foo://localhost:1234');
}

public function testThrowsIfSchemePartIsMissing()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The DSN is invalid. It does not have scheme separator ":".');
new Dsn('foobar');
}

public function testThrowsIfSchemeContainsIllegalSymbols()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The DSN is invalid. Scheme contains illegal symbols.');
new Dsn('foo_&%&^bar://localhost');
}

/**
* @dataProvider provideSchemes
*/
public function testShouldParseSchemeCorrectly(string $dsn, string $expectedScheme, string $expectedSchemeProtocol, array $expectedSchemeExtensions)
{
$dsn = new Dsn($dsn);

$this->assertSame($expectedScheme, $dsn->getScheme());
$this->assertSame($expectedSchemeProtocol, $dsn->getSchemeProtocol());
$this->assertSame($expectedSchemeExtensions, $dsn->getSchemeExtensions());
}

public function testShouldParseUser()
{
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');

$this->assertSame('theUser', $dsn->getUser());
}

public function testShouldParsePassword()
{
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');

$this->assertSame('thePass', $dsn->getPassword());
}

public function testShouldParseHost()
{
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');

$this->assertSame('theHost', $dsn->getHost());
}

public function testShouldParsePort()
{
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');

$this->assertSame(1267, $dsn->getPort());
}

public function testShouldParsePath()
{
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');

$this->assertSame('/thePath', $dsn->getPath());
}

public function testShouldParseQuery()
{
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath?foo=fooVal&bar=bar%2fVal');

$this->assertSame('foo=fooVal&bar=bar%2fVal', $dsn->getQueryString());
$this->assertSame(['foo' => 'fooVal', 'bar' => 'bar/Val'], $dsn->getQuery());
}

public static function provideSchemes()
{
yield [':', '', '', []];

yield ['FOO:', 'foo', 'foo', []];

yield ['foo:', 'foo', 'foo', []];

yield ['foo+bar:', 'foo+bar', 'foo', ['bar']];

yield ['foo+bar+baz:', 'foo+bar+baz', 'foo', ['bar', 'baz']];

yield ['foo:?bar=barVal', 'foo', 'foo', []];

yield ['amqp+ext://guest:guest@localhost:5672/%2f', 'amqp+ext', 'amqp', ['ext']];

yield ['amqp+ext+rabbitmq:', 'amqp+ext+rabbitmq', 'amqp', ['ext', 'rabbitmq']];
}
}
65 changes: 38 additions & 27 deletions pkg/enqueue/ConnectionFactoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,59 @@
class ConnectionFactoryFactory
{
/**
* @param string|array $config
* @param string
*
* @return PsrConnectionFactory
*/
public function create($config): PsrConnectionFactory
public function create(string $dsn): PsrConnectionFactory
{
if (is_string($config)) {
$config = ['dsn' => $config];
}
$dsn = new Dsn($dsn);

if (false == is_array($config)) {
throw new \InvalidArgumentException(sprintf('Config must be either string or array. Got %s', gettype($config)));
}
$availableSchemes = Resources::getAvailableSchemes();

if (false == array_key_exists('dsn', $config)) {
throw new \InvalidArgumentException('The config must have dsn field set.');
}
if (false == array_key_exists($dsn->getScheme(), $availableSchemes)) {
$knownSchemes = Resources::getKnownSchemes();
if (array_key_exists($dsn->getScheme(), $knownSchemes)) {
$knownConnections = Resources::getKnownConnections();

$dsn = new Dsn($config['dsn']);
throw new \LogicException(sprintf(
'A transport "%s" is not installed. Run "composer req %s" to add it.',
$knownSchemes[$dsn->getScheme()],
$knownConnections['package']
));
}

$availableSchemes = Resources::getAvailableSchemes();
throw new \LogicException(sprintf(
'A transport is not known. Make sure you registered it with "%s" if it is custom one.',
Resources::class
));
}

if (array_key_exists($dsn->getScheme(), $availableSchemes)) {
$dsnSchemeExtensions = $dsn->getSchemeExtensions();
if (false == $dsnSchemeExtensions) {
$factoryClass = $availableSchemes[$dsn->getScheme()];

return new $factoryClass($config);
return new $factoryClass((string) $dsn);
}

$knownSchemes = Resources::getKnownSchemes();
if (array_key_exists($dsn->getScheme(), $knownSchemes)) {
$knownConnections = Resources::getKnownConnections();
$protocol = $dsn->getSchemeProtocol();
foreach ($availableSchemes as $driverClass => $info) {
if (false == in_array($protocol, $info['schemes'], true)) {
continue;
}

throw new \LogicException(sprintf(
'A transport "%s" is not installed. Run "composer req %s" to add it.',
$knownSchemes[$dsn->getScheme()],
$knownConnections['package']
));
if (empty($info['supportedSchemeExtensions'])) {
continue;
}

$diff = array_diff($dsnSchemeExtensions, $info['supportedSchemeExtensions']);
if (empty($diff)) {
$factoryClass = $availableSchemes[$dsn->getScheme()];

return new $factoryClass((string) $dsn);
}
}

throw new \LogicException(sprintf(
'A transport is not known. Make sure you registered it with "%s" if it is custom one.',
Resources::class
));
throw new \LogicException(sprintf('There is no factory that supports scheme "%s"', $dsn->getScheme()));
}
}
Loading

0 comments on commit 5b3835a

Please sign in to comment.