Skip to content

Commit de307a2

Browse files
authored
Merge pull request #84 from php-enqueue/functions
Add dsn_to_connection_factory and dsn_to_context functions.
2 parents 767b7f0 + 799f2b5 commit de307a2

6 files changed

+192
-0
lines changed

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
"friendsofphp/php-cs-fixer": "^2",
2828
"empi89/php-amqp-stubs": "*@dev"
2929
},
30+
"autoload": {
31+
"files": ["pkg/enqueue/functions_include.php"]
32+
},
3033
"config": {
3134
"bin-dir": "bin"
3235
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Enqueue\Tests\Router;
4+
5+
use Enqueue\AmqpExt\AmqpConnectionFactory;
6+
use Enqueue\Fs\FsConnectionFactory;
7+
use Enqueue\Null\NullConnectionFactory;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class DsnToConnectionFactoryFunctionTest extends TestCase
11+
{
12+
public function testThrowIfDsnEmpty()
13+
{
14+
$this->expectException(\LogicException::class);
15+
$this->expectExceptionMessage('The scheme could not be parsed from DSN ""');
16+
17+
\Enqueue\dsn_to_connection_factory('');
18+
}
19+
20+
public function testThrowIfDsnMissingScheme()
21+
{
22+
$this->expectException(\LogicException::class);
23+
$this->expectExceptionMessage('The scheme could not be parsed from DSN "dsnMissingScheme"');
24+
25+
\Enqueue\dsn_to_connection_factory('dsnMissingScheme');
26+
}
27+
28+
public function testThrowIfDsnNotSupported()
29+
{
30+
$this->expectException(\LogicException::class);
31+
$this->expectExceptionMessage('The scheme "http" is not supported. Supported "file", "amqp", "null"');
32+
33+
\Enqueue\dsn_to_connection_factory('http://schemeNotSupported');
34+
}
35+
36+
/**
37+
* @dataProvider provideDSNs
38+
*
39+
* @param mixed $dsn
40+
* @param mixed $expectedFactoryClass
41+
*/
42+
public function testReturnsExpectedFactoryInstance($dsn, $expectedFactoryClass)
43+
{
44+
$factory = \Enqueue\dsn_to_connection_factory($dsn);
45+
46+
$this->assertInstanceOf($expectedFactoryClass, $factory);
47+
}
48+
49+
public static function provideDSNs()
50+
{
51+
yield ['amqp://', AmqpConnectionFactory::class];
52+
53+
yield ['amqp://user:pass@foo/vhost', AmqpConnectionFactory::class];
54+
55+
yield ['file://', FsConnectionFactory::class];
56+
57+
yield ['file://foo/bar/baz', FsConnectionFactory::class];
58+
59+
yield ['null://', NullConnectionFactory::class];
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Enqueue\Tests\Router;
4+
5+
use Enqueue\AmqpExt\AmqpContext;
6+
use Enqueue\Fs\FsContext;
7+
use Enqueue\Null\NullContext;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class DsnToContextFunctionTest extends TestCase
11+
{
12+
public function testThrowIfDsnEmpty()
13+
{
14+
$this->expectException(\LogicException::class);
15+
$this->expectExceptionMessage('The scheme could not be parsed from DSN ""');
16+
17+
\Enqueue\dsn_to_context('');
18+
}
19+
20+
public function testThrowIfDsnMissingScheme()
21+
{
22+
$this->expectException(\LogicException::class);
23+
$this->expectExceptionMessage('The scheme could not be parsed from DSN "dsnMissingScheme"');
24+
25+
\Enqueue\dsn_to_context('dsnMissingScheme');
26+
}
27+
28+
public function testThrowIfDsnNotSupported()
29+
{
30+
$this->expectException(\LogicException::class);
31+
$this->expectExceptionMessage('The scheme "http" is not supported. Supported "file", "amqp", "null"');
32+
33+
\Enqueue\dsn_to_context('http://schemeNotSupported');
34+
}
35+
36+
/**
37+
* @dataProvider provideDSNs
38+
*
39+
* @param mixed $dsn
40+
* @param mixed $expectedFactoryClass
41+
*/
42+
public function testReturnsExpectedFactoryInstance($dsn, $expectedFactoryClass)
43+
{
44+
$factory = \Enqueue\dsn_to_context($dsn);
45+
46+
$this->assertInstanceOf($expectedFactoryClass, $factory);
47+
}
48+
49+
public static function provideDSNs()
50+
{
51+
yield ['amqp://', AmqpContext::class];
52+
53+
yield ['amqp://user:pass@foo/vhost', AmqpContext::class];
54+
55+
yield ['file://', FsContext::class];
56+
57+
yield ['file:/'.sys_get_temp_dir(), FsContext::class];
58+
59+
yield ['null://', NullContext::class];
60+
}
61+
}

pkg/enqueue/composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
"symfony/dependency-injection": "^2.8|^3",
2222
"symfony/config": "^2.8|^3",
2323
"enqueue/null": "^0.4",
24+
"enqueue/amqp-ext": "^0.4",
25+
"enqueue/fs": "^0.4",
2426
"enqueue/test": "^0.4",
2527
"enqueue/simple-client": "^0.4"
2628
},
@@ -37,6 +39,7 @@
3739
},
3840
"autoload": {
3941
"psr-4": { "Enqueue\\": "" },
42+
"files": ["functions_include.php"],
4043
"exclude-from-classmap": [
4144
"/Tests/"
4245
]

pkg/enqueue/functions.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Enqueue;
4+
5+
use Enqueue\AmqpExt\AmqpConnectionFactory;
6+
use Enqueue\Fs\FsConnectionFactory;
7+
use Enqueue\Null\NullConnectionFactory;
8+
use Enqueue\Psr\PsrConnectionFactory;
9+
use Enqueue\Psr\PsrContext;
10+
11+
/**
12+
* @param string $dsn
13+
*
14+
* @return PsrConnectionFactory
15+
*/
16+
function dsn_to_connection_factory($dsn)
17+
{
18+
$map = [];
19+
20+
if (class_exists(FsConnectionFactory::class)) {
21+
$map['file'] = FsConnectionFactory::class;
22+
}
23+
24+
if (class_exists(AmqpConnectionFactory::class)) {
25+
$map['amqp'] = AmqpConnectionFactory::class;
26+
}
27+
28+
if (class_exists(NullConnectionFactory::class)) {
29+
$map['null'] = NullConnectionFactory::class;
30+
}
31+
32+
list($scheme) = explode('://', $dsn);
33+
if (false == $scheme || false === strpos($dsn, '://')) {
34+
throw new \LogicException(sprintf('The scheme could not be parsed from DSN "%s"', $dsn));
35+
}
36+
37+
if (false == array_key_exists($scheme, $map)) {
38+
throw new \LogicException(sprintf(
39+
'The scheme "%s" is not supported. Supported "%s"',
40+
$scheme,
41+
implode('", "', array_keys($map))
42+
));
43+
}
44+
45+
$factoryClass = $map[$scheme];
46+
47+
return new $factoryClass($dsn);
48+
}
49+
50+
/**
51+
* @param string $dsn
52+
*
53+
* @return PsrContext
54+
*/
55+
function dsn_to_context($dsn)
56+
{
57+
return dsn_to_connection_factory($dsn)->createContext();
58+
}

pkg/enqueue/functions_include.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Don't redefine the functions if included multiple times.
4+
if (false == function_exists('Enqueue\dsn_to_connection_factory')) {
5+
require __DIR__.'/functions.php';
6+
}

0 commit comments

Comments
 (0)