Skip to content

Commit 20d0623

Browse files
committed
fix&add tests.
1 parent 5b3835a commit 20d0623

10 files changed

Lines changed: 303 additions & 159 deletions

‎phpunit.xml.dist‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@
100100
<testsuite name="async-command">
101101
<directory>pkg/async-command/Tests</directory>
102102
</testsuite>
103+
104+
<testsuite name="dsn">
105+
<directory>pkg/dsn/Tests</directory>
106+
</testsuite>
103107
</testsuites>
104108

105109
<php>

‎pkg/dbal/DbalConnectionFactory.php‎

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -92,51 +92,55 @@ private function establishConnection()
9292
return $this->connection;
9393
}
9494

95-
/**
96-
* @param string $dsn
97-
*
98-
* @return array
99-
*/
100-
private function parseDsn($dsn)
95+
private function parseDsn(string $dsn): array
10196
{
97+
if (false === strpos($dsn, ':')) {
98+
throw new \LogicException(sprintf('The DSN is invalid. It does not have scheme separator ":".'));
99+
}
100+
102101
if (false === parse_url($dsn)) {
103102
throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn));
104103
}
105104

106-
if (!preg_match('/^([0-9a-z_]+):(.+)?$/', $dsn, $matches)) {
107-
throw new \LogicException('Schema is empty');
105+
list($scheme) = explode(':', $dsn, 2);
106+
107+
$scheme = strtolower($scheme);
108+
if (false == preg_match('/^[a-z\d+-.]*$/', $scheme)) {
109+
throw new \LogicException('The DSN is invalid. Scheme contains illegal symbols.');
108110
}
109-
$schema = $matches[1];
110111

111112
$supported = [
112-
'db2' => true,
113-
'ibm_db2' => true,
114-
'mssql' => true,
115-
'pdo_sqlsrv' => true,
116-
'mysql' => true,
117-
'mysql2' => true,
118-
'pdo_mysql' => true,
119-
'pgsql' => true,
120-
'postgres' => true,
121-
'postgresql' => true,
122-
'pdo_pgsql' => true,
123-
'sqlite' => true,
124-
'sqlite3' => true,
125-
'pdo_sqlite' => true,
113+
'db2' => 'db2',
114+
'ibm-db2' => 'ibm-db2',
115+
'mssql' => 'mssql',
116+
'sqlsrv+pdo' => 'pdo_sqlsrv',
117+
'mysql' => 'mysql',
118+
'mysql2' => 'mysql2',
119+
'mysql+pdo' => 'pdo_mysql',
120+
'pgsql' => 'pgsql',
121+
'postgres' => 'postgres',
122+
'pgsql+pdo' => 'pdo_pgsql',
123+
'sqlite' => 'sqlite',
124+
'sqlite3' => 'sqlite3',
125+
'sqlite+pdo' => 'pdo_sqlite',
126126
];
127127

128-
if (false == isset($supported[$schema])) {
128+
if (false == isset($supported[$scheme])) {
129129
throw new \LogicException(sprintf(
130130
'The given DSN schema "%s" is not supported. There are supported schemes: "%s".',
131-
$schema,
131+
$scheme,
132132
implode('", "', array_keys($supported))
133133
));
134134
}
135135

136+
$doctrineScheme = $supported[$scheme];
137+
136138
return [
137139
'lazy' => true,
138140
'connection' => [
139-
'url' => $schema.':' === $dsn ? $schema.'://root@localhost' : $dsn,
141+
'url' => $scheme.':' === $dsn ?
142+
$doctrineScheme.'://root@localhost' :
143+
str_replace($scheme, $doctrineScheme, $dsn),
140144
],
141145
];
142146
}

‎pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public function testThrowNeitherArrayStringNorNullGivenAsConfig()
2424
public function testThrowIfSchemeIsNotSupported()
2525
{
2626
$this->expectException(\LogicException::class);
27-
$this->expectExceptionMessage('The given DSN schema "http" is not supported. There are supported schemes: "db2", "ibm_db2", "mssql", "pdo_sqlsrv", "mysql", "mysql2", "pdo_mysql", "pgsql", "postgres", "postgresql", "pdo_pgsql", "sqlite", "sqlite3", "pdo_sqlite"');
27+
$this->expectExceptionMessage('The given DSN schema "http" is not supported. There are supported schemes: "db2", "ibm-db2", "mssql", "sqlsrv+pdo", "mysql", "mysql2", "mysql+pdo", "pgsql", "postgres", "pgsql+pdo", "sqlite", "sqlite3", "sqlite+pdo".');
2828

2929
new DbalConnectionFactory('http://example.com');
3030
}
3131

3232
public function testThrowIfDsnCouldNotBeParsed()
3333
{
3434
$this->expectException(\LogicException::class);
35-
$this->expectExceptionMessage('Schema is empty');
35+
$this->expectExceptionMessage('The DSN is invalid. It does not have scheme separator ":".');
3636

3737
new DbalConnectionFactory('invalidDSN');
3838
}
@@ -78,7 +78,7 @@ public static function provideConfigs()
7878
];
7979

8080
yield [
81-
'pdo_mysql:',
81+
'mysql+pdo:',
8282
[
8383
'connection' => [
8484
'url' => 'pdo_mysql://root@localhost',
@@ -114,7 +114,7 @@ public static function provideConfigs()
114114
];
115115

116116
yield [
117-
'pdo_mysql://user:pass@host:10001/db',
117+
'mysql+pdo://user:pass@host:10001/db',
118118
[
119119
'connection' => [
120120
'url' => 'pdo_mysql://user:pass@host:10001/db',

‎pkg/enqueue/ConnectionFactoryFactory.php‎

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,54 @@
55
use Enqueue\Dsn\Dsn;
66
use Interop\Queue\PsrConnectionFactory;
77

8-
class ConnectionFactoryFactory
8+
final class ConnectionFactoryFactory implements ConnectionFactoryFactoryInterface
99
{
10-
/**
11-
* @param string
12-
*
13-
* @return PsrConnectionFactory
14-
*/
1510
public function create(string $dsn): PsrConnectionFactory
1611
{
1712
$dsn = new Dsn($dsn);
1813

19-
$availableSchemes = Resources::getAvailableSchemes();
20-
21-
if (false == array_key_exists($dsn->getScheme(), $availableSchemes)) {
22-
$knownSchemes = Resources::getKnownSchemes();
23-
if (array_key_exists($dsn->getScheme(), $knownSchemes)) {
24-
$knownConnections = Resources::getKnownConnections();
25-
26-
throw new \LogicException(sprintf(
27-
'A transport "%s" is not installed. Run "composer req %s" to add it.',
28-
$knownSchemes[$dsn->getScheme()],
29-
$knownConnections['package']
30-
));
31-
}
14+
if ($factoryClass = $this->findFactory($dsn, Resources::getAvailableConnections())) {
15+
return new $factoryClass((string) $dsn);
16+
}
3217

18+
$knownConnections = Resources::getKnownConnections();
19+
if ($factoryClass = $this->findFactory($dsn, $knownConnections)) {
3320
throw new \LogicException(sprintf(
34-
'A transport is not known. Make sure you registered it with "%s" if it is custom one.',
35-
Resources::class
21+
'To use given scheme "%s" a package has to be installed. Run "composer req %s" to add it.',
22+
$dsn->getScheme(),
23+
$knownConnections[$factoryClass]['package']
3624
));
3725
}
3826

39-
$dsnSchemeExtensions = $dsn->getSchemeExtensions();
40-
if (false == $dsnSchemeExtensions) {
41-
$factoryClass = $availableSchemes[$dsn->getScheme()];
42-
43-
return new $factoryClass((string) $dsn);
44-
}
27+
throw new \LogicException(sprintf(
28+
'A given scheme "%s" is not supported. Maybe it is a custom connection, make sure you registered it with "%s::addConnection".',
29+
$dsn->getScheme(),
30+
Resources::class
31+
));
32+
}
4533

34+
private function findFactory(Dsn $dsn, array $factories): ?string
35+
{
4636
$protocol = $dsn->getSchemeProtocol();
47-
foreach ($availableSchemes as $driverClass => $info) {
37+
foreach ($factories as $connectionClass => $info) {
4838
if (false == in_array($protocol, $info['schemes'], true)) {
4939
continue;
5040
}
5141

42+
if (false == $dsn->getSchemeExtensions()) {
43+
return $connectionClass;
44+
}
45+
5246
if (empty($info['supportedSchemeExtensions'])) {
5347
continue;
5448
}
5549

56-
$diff = array_diff($dsnSchemeExtensions, $info['supportedSchemeExtensions']);
50+
$diff = array_diff($dsn->getSchemeExtensions(), $info['supportedSchemeExtensions']);
5751
if (empty($diff)) {
58-
$factoryClass = $availableSchemes[$dsn->getScheme()];
59-
60-
return new $factoryClass((string) $dsn);
52+
return $connectionClass;
6153
}
6254
}
6355

64-
throw new \LogicException(sprintf('There is no factory that supports scheme "%s"', $dsn->getScheme()));
56+
return null;
6557
}
6658
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Enqueue;
4+
5+
use Interop\Queue\PsrConnectionFactory;
6+
7+
interface ConnectionFactoryFactoryInterface
8+
{
9+
public function create(string $dsn): PsrConnectionFactory;
10+
}

‎pkg/enqueue/Resources.php‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,37 +88,36 @@ public static function getKnownConnections(): array
8888
];
8989
$map[AmqpExtConnectionFactory::class] = [
9090
'schemes' => ['amqp', 'amqps'],
91-
'supportedSchemeExtensions' => ['ext', 'rabiitmq'],
91+
'supportedSchemeExtensions' => ['ext', 'rabbitmq'],
9292
'package' => 'enqueue/amqp-ext',
9393
];
9494
$map[AmqpBunnyConnectionFactory::class] = [
9595
'schemes' => ['amqp'],
96-
'supportedSchemeExtensions' => ['bunny', 'rabiitmq'],
96+
'supportedSchemeExtensions' => ['bunny', 'rabbitmq'],
9797
'package' => 'enqueue/amqp-bunny',
9898
];
9999
$map[AmqpLibConnectionFactory::class] = [
100100
'schemes' => ['amqp', 'amqps'],
101-
'supportedSchemeExtensions' => ['lib', 'rabiitmq'],
101+
'supportedSchemeExtensions' => ['lib', 'rabbitmq'],
102102
'package' => 'enqueue/amqp-lib',
103103
];
104104

105105
$map[DbalConnectionFactory::class] = [
106106
'schemes' => [
107107
'db2',
108-
'ibm_db2',
108+
'ibm-db2',
109109
'mssql',
110-
'pdo_sqlsrv',
110+
'sqlsrv',
111111
'mysql',
112112
'mysql2',
113-
'pdo_mysql',
113+
'mysql',
114114
'pgsql',
115115
'postgres',
116-
'pdo_pgsql',
117116
'sqlite',
118117
'sqlite3',
119-
'pdo_sqlite',
118+
'sqlite',
120119
],
121-
'supportedSchemeExtensions' => [],
120+
'supportedSchemeExtensions' => ['pdo'],
122121
'package' => 'enqueue/dbal',
123122
];
124123

@@ -173,12 +172,10 @@ public static function getKnownConnections(): array
173172

174173
public static function addConnection(string $connectionFactoryClass, array $schemes, array $extensions, string $package): void
175174
{
176-
if (false == class_exists($connectionFactoryClass)) {
177-
throw new \InvalidArgumentException(sprintf('The connection factory class "%s" does not exist.', $connectionFactoryClass));
178-
}
179-
180-
if (false == (new \ReflectionClass($connectionFactoryClass))->implementsInterface(PsrConnectionFactory::class)) {
181-
throw new \InvalidArgumentException(sprintf('The connection factory class "%s" must implement "%s" interface.', $connectionFactoryClass, PsrConnectionFactory::class));
175+
if (class_exists($connectionFactoryClass)) {
176+
if (false == (new \ReflectionClass($connectionFactoryClass))->implementsInterface(PsrConnectionFactory::class)) {
177+
throw new \InvalidArgumentException(sprintf('The connection factory class "%s" must implement "%s" interface.', $connectionFactoryClass, PsrConnectionFactory::class));
178+
}
182179
}
183180

184181
if (empty($schemes)) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Enqueue\Tests\Client;
4+
5+
use Enqueue\Client\Resources;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class ResourcesTest extends TestCase
9+
{
10+
public function testShouldBeFinal()
11+
{
12+
$rc = new \ReflectionClass(Resources::class);
13+
14+
$this->assertTrue($rc->isFinal());
15+
}
16+
17+
public function testShouldConstructorBePrivate()
18+
{
19+
$rc = new \ReflectionClass(Resources::class);
20+
21+
$this->assertTrue($rc->getConstructor()->isPrivate());
22+
}
23+
}

0 commit comments

Comments
 (0)