Skip to content

Commit 6ada205

Browse files
OskarStarkfabpot
authored andcommitted
[Notifier] [DX] Dsn::getRequiredOption()
1 parent 003aadb commit 6ada205

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* The component is not marked as `@experimental` anymore
88
* [BC BREAK] Changed the return type of `AbstractTransportFactory::getEndpoint()` from `?string` to `string`
9+
* Added `DSN::getRequiredOption` method which throws a new `MissingRequiredOptionException`.
910

1011
5.2.0
1112
-----
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Exception;
13+
14+
/**
15+
* @author Oskar Stark <oskarstark@googlemail.com>
16+
*
17+
* @experimental in 5.3
18+
*/
19+
class MissingRequiredOptionException extends IncompleteDsnException
20+
{
21+
public function __construct(string $option, string $dsn = null, ?\Throwable $previous = null)
22+
{
23+
$message = sprintf('The option "%s" is required but missing.', $option);
24+
25+
parent::__construct($message, $dsn, $previous);
26+
}
27+
}

Tests/Transport/DsnTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
16+
use Symfony\Component\Notifier\Exception\MissingRequiredOptionException;
1617
use Symfony\Component\Notifier\Transport\Dsn;
1718

1819
final class DsnTest extends TestCase
@@ -120,4 +121,54 @@ public function testGetOption()
120121
$this->assertSame('default', $dsn->getOption('nullable', 'default'));
121122
$this->assertSame('default', $dsn->getOption('not_existent_property', 'default'));
122123
}
124+
125+
public function testGetRequiredOptionGetsOptionIfSet()
126+
{
127+
$options = ['with_value' => 'some value'];
128+
$dsn = new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', $options, '/channel');
129+
130+
$this->assertSame('some value', $dsn->getRequiredOption('with_value'));
131+
}
132+
133+
public function testGetRequiredOptionGetsOptionIfValueIsZero()
134+
{
135+
$options = ['timeout' => 0];
136+
$dsn = new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', $options, '/channel');
137+
138+
$this->assertSame(0, $dsn->getRequiredOption('timeout'));
139+
}
140+
141+
/**
142+
* @dataProvider getRequiredOptionThrowsMissingRequiredOptionExceptionProvider
143+
*/
144+
public function testGetRequiredOptionThrowsMissingRequiredOptionException(string $expectedExceptionMessage, array $options, string $option)
145+
{
146+
$dsn = new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', $options, '/channel');
147+
148+
$this->expectException(MissingRequiredOptionException::class);
149+
$this->expectExceptionMessage($expectedExceptionMessage);
150+
151+
$dsn->getRequiredOption($option);
152+
}
153+
154+
public function getRequiredOptionThrowsMissingRequiredOptionExceptionProvider(): iterable
155+
{
156+
yield [
157+
'The option "foo_bar" is required but missing.',
158+
['with_value' => 'some value'],
159+
'foo_bar',
160+
];
161+
162+
yield [
163+
'The option "with_empty_string" is required but missing.',
164+
['with_empty_string' => ''],
165+
'with_empty_string',
166+
];
167+
168+
yield [
169+
'The option "with_null" is required but missing.',
170+
['with_null' => null],
171+
'with_null',
172+
];
173+
}
123174
}

Tests/TransportFactoryTestCase.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
1616
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
17+
use Symfony\Component\Notifier\Exception\MissingRequiredOptionException;
1718
use Symfony\Component\Notifier\Transport\Dsn;
1819
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
1920

@@ -52,6 +53,14 @@ public function incompleteDsnProvider(): iterable
5253
return [];
5354
}
5455

56+
/**
57+
* @return iterable<array{0: string, 1: string|null}>
58+
*/
59+
public function missingRequiredOptionProvider(): iterable
60+
{
61+
return [];
62+
}
63+
5564
/**
5665
* @dataProvider supportsProvider
5766
*/
@@ -106,4 +115,21 @@ public function testIncompleteDsnException(string $dsn, string $message = null)
106115

107116
$factory->create($dsn);
108117
}
118+
119+
/**
120+
* @dataProvider missingRequiredOptionProvider
121+
*/
122+
public function testMissingRequiredOptionException(string $dsn, string $message = null): void
123+
{
124+
$factory = $this->createFactory();
125+
126+
$dsn = Dsn::fromString($dsn);
127+
128+
$this->expectException(MissingRequiredOptionException::class);
129+
if (null !== $message) {
130+
$this->expectExceptionMessage($message);
131+
}
132+
133+
$factory->create($dsn);
134+
}
109135
}

Transport/Dsn.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Notifier\Transport;
1313

1414
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
15+
use Symfony\Component\Notifier\Exception\MissingRequiredOptionException;
1516

1617
/**
1718
* @author Fabien Potencier <fabien@symfony.com>
@@ -94,6 +95,15 @@ public function getOption(string $key, $default = null)
9495
return $this->options[$key] ?? $default;
9596
}
9697

98+
public function getRequiredOption(string $key)
99+
{
100+
if (!\array_key_exists($key, $this->options) || '' === trim($this->options[$key])) {
101+
throw new MissingRequiredOptionException($key);
102+
}
103+
104+
return $this->options[$key];
105+
}
106+
97107
public function getPath(): ?string
98108
{
99109
return $this->path;

Transport/TransportFactoryInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Notifier\Transport;
1313

1414
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
15+
use Symfony\Component\Notifier\Exception\MissingRequiredOptionException;
1516
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
1617

1718
/**
@@ -22,6 +23,7 @@ interface TransportFactoryInterface
2223
/**
2324
* @throws UnsupportedSchemeException
2425
* @throws IncompleteDsnException
26+
* @throws MissingRequiredOptionException
2527
*/
2628
public function create(Dsn $dsn): TransportInterface;
2729

0 commit comments

Comments
 (0)