Skip to content

Commit 843f817

Browse files
committed
fix: switch to DSN class, add tests
1 parent 1dfe488 commit 843f817

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

DbalConnectionFactory.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Doctrine\DBAL\Connection;
88
use Doctrine\DBAL\DriverManager;
9+
use Enqueue\Dsn\Dsn;
910
use Interop\Queue\ConnectionFactory;
1011
use Interop\Queue\Context;
1112

@@ -93,20 +94,7 @@ private function establishConnection(): Connection
9394

9495
private function parseDsn(string $dsn, array $config = null): array
9596
{
96-
if (false === strpos($dsn, ':')) {
97-
throw new \LogicException(sprintf('The DSN is invalid. It does not have scheme separator ":".'));
98-
}
99-
100-
list($scheme) = explode(':', $dsn, 2);
101-
$scheme = strtolower($scheme);
102-
103-
if (false === strpos($scheme, 'sqlite') && false === parse_url($dsn)) {
104-
throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn));
105-
}
106-
107-
if (false == preg_match('/^[a-z\d+-.]*$/', $scheme)) {
108-
throw new \LogicException('The DSN is invalid. Scheme contains illegal symbols.');
109-
}
97+
$parsedDsn = Dsn::parseFirst($dsn);
11098

11199
$supported = [
112100
'db2' => 'db2',
@@ -124,13 +112,13 @@ private function parseDsn(string $dsn, array $config = null): array
124112
'sqlite+pdo' => 'pdo_sqlite',
125113
];
126114

127-
if (false == isset($supported[$scheme])) {
128-
throw new \LogicException(sprintf('The given DSN schema "%s" is not supported. There are supported schemes: "%s".', $scheme, implode('", "', array_keys($supported))));
115+
if ($parsedDsn && false == isset($supported[$parsedDsn->getScheme()])) {
116+
throw new \LogicException(sprintf('The given DSN schema "%s" is not supported. There are supported schemes: "%s".', $parsedDsn->getScheme(), implode('", "', array_keys($supported))));
129117
}
130118

131-
$doctrineScheme = $supported[$scheme];
132-
133-
if ($scheme.':' === $dsn && is_array($config) && array_key_exists('connection', $config)) {
119+
$doctrineScheme = $supported[$parsedDsn->getScheme()];
120+
$dsnHasProtocolOnly = $parsedDsn->getScheme().':' === $dsn;
121+
if ($dsnHasProtocolOnly && is_array($config) && array_key_exists('connection', $config)) {
134122
$default = [
135123
'driver' => $doctrineScheme,
136124
'host' => 'localhost',
@@ -148,9 +136,9 @@ private function parseDsn(string $dsn, array $config = null): array
148136
return [
149137
'lazy' => true,
150138
'connection' => [
151-
'url' => $scheme.':' === $dsn ?
139+
'url' => $dsnHasProtocolOnly ?
152140
$doctrineScheme.'://root@localhost' :
153-
str_replace($scheme, $doctrineScheme, $dsn),
141+
str_replace($parsedDsn->getScheme(), $doctrineScheme, $dsn),
154142
],
155143
];
156144
}

Tests/DbalConnectionFactoryTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,32 @@ public function testShouldCreateLazyContext()
2828
$this->assertAttributeEquals(null, 'connection', $context);
2929
$this->assertAttributeInternalType('callable', 'connectionFactory', $context);
3030
}
31+
32+
public function testShouldParseGenericDSN()
33+
{
34+
$factory = new DbalConnectionFactory('pgsql+pdo://foo@bar');
35+
36+
$context = $factory->createContext();
37+
38+
$this->assertInstanceOf(DbalContext::class, $context);
39+
40+
$config = $context->getConfig();
41+
$this->assertArrayHasKey('connection', $config);
42+
$this->assertArrayHasKey('url', $config['connection']);
43+
$this->assertEquals('pdo_pgsql://foo@bar', $config['connection']['url']);
44+
}
45+
46+
public function testShouldParseSqliteAbsolutePathDSN()
47+
{
48+
$factory = new DbalConnectionFactory('sqlite+pdo:////tmp/some.sq3');
49+
50+
$context = $factory->createContext();
51+
52+
$this->assertInstanceOf(DbalContext::class, $context);
53+
54+
$config = $context->getConfig();
55+
$this->assertArrayHasKey('connection', $config);
56+
$this->assertArrayHasKey('url', $config['connection']);
57+
$this->assertEquals('pdo_sqlite:////tmp/some.sq3', $config['connection']['url']);
58+
}
3159
}

0 commit comments

Comments
 (0)