8000 [Notifier] Close Dsn api · symfony/symfony@9f489e9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f489e9

Browse files
committed
[Notifier] Close Dsn api
1 parent fe91b86 commit 9f489e9

File tree

2 files changed

+122
-30
lines changed

2 files changed

+122
-30
lines changed

src/Symfony/Component/Notifier/Tests/Transport/DsnTest.php

Lines changed: 113 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,66 +21,123 @@ final class DsnTest extends TestCase
2121
/**
2222
* @dataProvider fromStringProvider
2323
*/
24-
public function testFromString(string $string, Dsn $expectedDsn)
24+
public function testFromString(string $dsnString, string $scheme, string $host, ?string $user = null, ?string $password = null, ?int $port = null, array $options = [], ?string $path = null)
2525
{
26-
$actualDsn = Dsn::fromString($string);
26+
$dsn = Dsn::fromString($dsnString);
2727

28-
$this->assertSame($expectedDsn->getScheme(), $actualDsn->getScheme());
29-
$this->assertSame($expectedDsn->getHost(), $actualDsn->getHost());
30-
$this->assertSame($expectedDsn->getPort(), $actualDsn->getPort());
31-
$this->assertSame($expectedDsn->getUser(), $actualDsn->getUser());
32-
$this->assertSame($expectedDsn->getPassword(), $actualDsn->getPassword());
33-
$this->assertSame($expectedDsn->getPath(), $actualDsn->getPath());
34-
$this->assertSame($expectedDsn->getOption('from'), $actualDsn->getOption('from'));
28+
$this->assertSame($scheme, $dsn->getScheme());
29+
$this->assertSame($host, $dsn->getHost());
30+
$this->assertSame($user, $dsn->getUser());
31+
$this->assertSame($password, $dsn->getPassword());
32+
$this->assertSame($port, $dsn->getPort());
33+
$this->assertSame($path, $dsn->getPath());
34+
$this->assertSame($options, $dsn->getOptions());
3535

36-
$this->assertSame($string, $actualDsn->getOriginalDsn());
36+
$this->assertSame($dsnString, $dsn->getOriginalDsn());
3737
}
3838

3939
public function fromStringProvider(): iterable
4040
{
4141
yield 'simple dsn' => [
4242
'scheme://localhost',
43-
new Dsn('scheme', 'localhost', null, null, null, [], null),
43+
'scheme',
44+
'localhost',
4445
];
4546

4647
yield 'simple dsn including @ sign, but no user/password/token' => [
4748
'scheme://@localhost',
48-
new Dsn('scheme', 'localhost', null, null),
49+
'scheme',
50+
'localhost',
4951
];
5052

5153
yield 'simple dsn including : sign and @ sign, but no user/password/token' => [
5254
'scheme://:@localhost',
53-
new Dsn('scheme', 'localhost', null, null),
55+
'scheme',
56+
'localhost',
5457
];
5558

5659
yield 'simple dsn including user, : sign and @ sign, but no password' => [
5760
'scheme://user1:@localhost',
58-
new Dsn('scheme', 'localhost', 'user1', null),
61+
'scheme',
62+
'localhost',
63+
'user1',
5964
];
6065

6166
yield 'simple dsn including : sign, password, and @ sign, but no user' => [
6267
'scheme://:pass@localhost',
63-
new Dsn('scheme', 'localhost', null, 'pass'),
68+
'scheme',
69+
'localhost',
70+
null,
71+
'pass',
6472
];
6573

6674
yield 'dsn with user and pass' => [
6775
'scheme://u$er:pa$s@localhost',
68-
new Dsn('scheme', 'localhost', 'u$er', 'pa$s', null, [], null),
76+
'scheme',
77+
'localhost',
78+
'u$er',
79+
'pa$s',
6980
];
7081

7182
yield 'dsn with user and pass and custom port' => [
7283
'scheme://u$er:pa$s@localhost:8000',
73-
new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', [], null),
84+
'scheme',
85+
'localhost',
86+
'u$er',
87+
'pa$s',
88+
8000,
7489
];
7590

7691
yield 'dsn with user and pass, custom port and custom path' => [
7792
'scheme://u$er:pa$s@localhost:8000/channel',
78-
new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', [], '/channel'),
93+
'scheme',
94+
'localhost',
95+
'u$er',
96+
'pa$s',
97+
8000,
98+
[],
99+
'/channel',
79100
];
80101

81-
yield 'dsn with user and pass, custom port, custom path and custom options' => [
102+
yield 'dsn with user and pass, custom port, custom path and custom option' => [
82103
'scheme://u$er:pa$s@localhost:8000/channel?from=FROM',
83-
new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', ['from' => 'FROM'], '/channel'),
104+
'scheme',
105+
'localhost',
106+
'u$er',
107+
'pa$s',
108+
8000,
109+
[
110+
'from' => 'FROM',
111+
],
112+
'/channel',
113+
];
114+
115+
yield 'dsn with user and pass, custom port, custom path and custom options' => [
116+
'scheme://u$er:pa$s@localhost:8000/channel?from=FROM&to=TO',
117+
'scheme',
118+
'localhost',
119+
'u$er',
120+
'pa$s',
121+
8000,
122+
[
123+
'from' => 'FROM',
124+
'to' => 'TO',
125+
],
126+
'/channel',
127+
];
128+
129+
yield 'dsn with user and pass, custom port, custom path and custom options and custom options keep the same order' => [
130+
'scheme://u$er:pa$s@localhost:8000/channel?to=TO&from=FROM',
131+
'scheme',
132+
'localhost',
133+
'u$er',
134+
'pa$s',
135+
8000,
136+
[
137+
'to' => 'TO',
138+
'from' => 'FROM',
139+
],
140+
'/channel',
84141
];
85142
}
86143

@@ -91,6 +148,7 @@ public function testInvalidDsn(string $dsn, string $exceptionMessage)
91148
{
92149
$this->expectException(InvalidArgumentException::class);
93150
$this->expectExceptionMessage($exceptionMessage);
151+
94152
Dsn::fromString($dsn);
95153
}
96154

@@ -112,14 +170,43 @@ public function invalidDsnProvider(): iterable
112170
];
113171
}
114172

115-
public function testGetOption()
173+
/**
174+
* @dataProvider getOptionProvider
175+
*/
176+
public function testGetOption($expected, string $dsnString, string $option, ?string $default = null)
116177
{
117-
$options = ['with_value' => 'some value', 'nullable' => null];
118-
$dsn = new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', $options, '/channel');
178+
$dsn = Dsn::fromString($dsnString);
179+
180+
$this->assertSame($expected, $dsn->getOption($option, $default));
181+
}
119182

120-
$this->assertSame('some value', $dsn->getOption('with_value'));
121-
$this->assertSame('default', $dsn->getOption('nullable', 'default'));
122-
$this->assertSame('default', $dsn->getOption('not_existent_property', 'default'));
183+
public function getOptionProvider(): iterable
184+
{
185+
yield [
186+
'foo',
187+
'scheme://u$er:pa$s@localhost:8000/channel?with_value=foo',
188+
'with_value',
189+
];
190+
191+
yield [
192+
'default-value',
193+
'scheme://u$er:pa$s@localhost:8000/channel?empty=',
194+
'empty',
195+
'default-value'
196+
];
197+
198+
yield [
199+
0,
200+
'scheme://u$er:pa$s@localhost:8000/channel?zero=0',
201+
'zero',
202+
];
203+
204+
yield [
205+
'default-value',
206+
'scheme://u$er:pa$s@localhost:8000/channel?option=value',
207+
'non_existent_property',
208+
'default-value'
209+
];
123210
}
124211

125212
public function testGetRequiredOptionGetsOptionIfSet()

src/Symfony/Component/Notifier/Transport/Dsn.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ final class Dsn
2626
private $port;
2727
private $options;
2828
private $path;
29-
private $dsn;
29+
private $originalDsn;
3030

31-
public function __construct(string $scheme, string $host, ?string $user = null, ?string $password = null, ?int $port = null, array $options = [], ?string $path = null)
31+
private function __construct(string $scheme, string $host, ?string $user = null, ?string $password = null, ?int $port = null, array $options = [], ?string $path = null)
3232
{
3333
$this->scheme = $scheme;
3434
$this->host = $host;
@@ -60,7 +60,7 @@ public static function fromString(string $dsn): self
6060
parse_str($parsedDsn['query'] ?? '', $query);
6161

6262
$dsnObject = new self($parsedDsn['scheme'], $parsedDsn['host'], $user, $password, $port, $query, $path);
63-
$dsnObject->dsn = $dsn;
63+
$dsnObject->originalDsn = $dsn;
6464

6565
return $dsnObject;
6666
}
@@ -104,13 +104,18 @@ public function getRequiredOption(string $key)
104104
return $this->options[$key];
105105
}
106106

107+
public function getOptions(): array
108+
{
109+
return $this->options;
110+
}
111+
107112
public function getPath(): ?string
108113
{
109114
return $this->path;
110115
}
111116

112117
public function getOriginalDsn(): string
113118
{
114-
return $this->dsn;
119+
return $this->originalDsn;
115120
}
116121
}

0 commit comments

Comments
 (0)
0