Skip to content

Commit 3c7a8cf

Browse files
committed
add tests, remove functions.
1 parent 59f93f4 commit 3c7a8cf

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

Dsn.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,18 @@ private function parse(string $dsn): void
184184
}
185185

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

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

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

197197
unset($schemeParts[0]);
198-
$this->schemeExtensions = $schemeParts;
198+
$this->schemeExtensions = array_values($schemeParts);
199199

200200
if ($host = parse_url($dsn, PHP_URL_HOST)) {
201201
$this->host = $host;

Tests/DsnTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,101 @@
22

33
namespace Enqueue\Dsn\Tests;
44

5+
use Enqueue\Dsn\Dsn;
56
use PHPUnit\Framework\TestCase;
67

78
class DsnTest extends TestCase
89
{
10+
public function testCouldBeConstructedWithDsnAsFirstArgument()
11+
{
12+
new Dsn('foo://localhost:1234');
13+
}
14+
15+
public function testThrowsIfSchemePartIsMissing()
16+
{
17+
$this->expectException(\LogicException::class);
18+
$this->expectExceptionMessage('The DSN is invalid. It does not have scheme separator ":".');
19+
new Dsn('foobar');
20+
}
21+
22+
public function testThrowsIfSchemeContainsIllegalSymbols()
23+
{
24+
$this->expectException(\LogicException::class);
25+
$this->expectExceptionMessage('The DSN is invalid. Scheme contains illegal symbols.');
26+
new Dsn('foo_&%&^bar://localhost');
27+
}
28+
29+
/**
30+
* @dataProvider provideSchemes
31+
*/
32+
public function testShouldParseSchemeCorrectly(string $dsn, string $expectedScheme, string $expectedSchemeProtocol, array $expectedSchemeExtensions)
33+
{
34+
$dsn = new Dsn($dsn);
35+
36+
$this->assertSame($expectedScheme, $dsn->getScheme());
37+
$this->assertSame($expectedSchemeProtocol, $dsn->getSchemeProtocol());
38+
$this->assertSame($expectedSchemeExtensions, $dsn->getSchemeExtensions());
39+
}
40+
41+
public function testShouldParseUser()
42+
{
43+
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');
44+
45+
$this->assertSame('theUser', $dsn->getUser());
46+
}
47+
48+
public function testShouldParsePassword()
49+
{
50+
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');
51+
52+
$this->assertSame('thePass', $dsn->getPassword());
53+
}
54+
55+
public function testShouldParseHost()
56+
{
57+
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');
58+
59+
$this->assertSame('theHost', $dsn->getHost());
60+
}
61+
62+
public function testShouldParsePort()
63+
{
64+
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');
65+
66+
$this->assertSame(1267, $dsn->getPort());
67+
}
68+
69+
public function testShouldParsePath()
70+
{
71+
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath');
72+
73+
$this->assertSame('/thePath', $dsn->getPath());
74+
}
75+
76+
public function testShouldParseQuery()
77+
{
78+
$dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath?foo=fooVal&bar=bar%2fVal');
79+
80+
$this->assertSame('foo=fooVal&bar=bar%2fVal', $dsn->getQueryString());
81+
$this->assertSame(['foo' => 'fooVal', 'bar' => 'bar/Val'], $dsn->getQuery());
82+
}
83+
84+
public static function provideSchemes()
85+
{
86+
yield [':', '', '', []];
87+
88+
yield ['FOO:', 'foo', 'foo', []];
89+
90+
yield ['foo:', 'foo', 'foo', []];
91+
92+
yield ['foo+bar:', 'foo+bar', 'foo', ['bar']];
93+
94+
yield ['foo+bar+baz:', 'foo+bar+baz', 'foo', ['bar', 'baz']];
95+
96+
yield ['foo:?bar=barVal', 'foo', 'foo', []];
97+
98+
yield ['amqp+ext://guest:guest@localhost:5672/%2f', 'amqp+ext', 'amqp', ['ext']];
99+
100+
yield ['amqp+ext+rabbitmq:', 'amqp+ext+rabbitmq', 'amqp', ['ext', 'rabbitmq']];
101+
}
9102
}

0 commit comments

Comments
 (0)