Skip to content

[Notifier] Change Dsn api #39585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Symfony/Component/Notifier/Bridge/Esendex/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ CHANGELOG
-----

* The bridge is not marked as `@experimental` anymore
* [BC BREAK] Changed signature of `EsendexTransport::__construct()` method from:
`public function __construct(string $token, string $accountReference, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)`
to:
`public function __construct(string $email, string $password, string $accountReference, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)`
* [BC BREAK] Change signature of `EsendexTransport::__construct()` method from:
`public function __construct(string $token, string $accountReference, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)`
to:
`public function __construct(string $email, string $password, string $accountReference, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)`

5.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CHANGELOG
-----

* The bridge is not marked as `@experimental` anymore
* [BC BREAK] `LinkedInTransportFactory` is now final
* [BC BREAK] `LinkedInTransportFactory` is now final

5.2.0
-----
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Notifier/Bridge/Mattermost/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ CHANGELOG
-----

* The bridge is not marked as `@experimental` anymore
* [BC BREAK] Changed signature of `MattermostTransport::__construct()` method from:
`public function __construct(string $token, string $channel, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, string $path = null)`
to:
`public function __construct(string $token, string $channel, ?string $path = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)`
* [BC BREAK] Change signature of `MattermostTransport::__construct()` method from:
`public function __construct(string $token, string $channel, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, string $path = null)`
to:
`public function __construct(string $token, string $channel, ?string $path = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)`

5.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testCreateWithDeprecatedDsn()
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Support for Slack webhook DSN has been dropped since 5.2 (maybe you haven\'t updated the DSN when upgrading from 5.1).');

$factory->create(Dsn::fromString('slack://default/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'));
$factory->create(new Dsn('slack://default/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'));
}

public function supportsProvider(): iterable
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Notifier/Bridge/Zulip/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ CHANGELOG
-----

* The bridge is not marked as `@experimental` anymore
* [BC BREAK] `ZulipTransport` is now final
* [BC BREAK] `ZulipTransportFactory` is now final
* [BC BREAK] `ZulipTransport` is now final
* [BC BREAK] `ZulipTransportFactory` is now final

5.2.0
-----
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Notifier/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ CHANGELOG
-----

* The component is not marked as `@experimental` anymore
* [BC BREAK] Change signature of `Dsn::__construct()` method from:
`public function __construct(string $scheme, string $host, ?string $user = null, ?string $password = null, ?int $port = null, array $options = [], ?string $path = null)`
to:
`public function __construct(string $dsn)`
* [BC BREAK] Remove `Dsn::fromString()` method
* [BC BREAK] Changed the return type of `AbstractTransportFactory::getEndpoint()` from `?string` to `string`
* Added `DSN::getRequiredOption` method which throws a new `MissingRequiredOptionException`.

Expand Down
186 changes: 137 additions & 49 deletions src/Symfony/Component/Notifier/Tests/Transport/DsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,79 +19,136 @@
final class DsnTest extends TestCase
{
/**
* @dataProvider fromStringProvider
* @dataProvider constructProvider
*/
public function testFromString(string $string, Dsn $expectedDsn)
public function testConstruct(string $dsnString, string $scheme, string $host, ?string $user = null, ?string $password = null, ?int $port = null, array $options = [], ?string $path = null)
{
$actualDsn = Dsn::fromString($string);
$dsn = new Dsn($dsnString);
$this->assertSame($dsnString, $dsn->getOriginalDsn());

$this->assertSame($expectedDsn->getScheme(), $actualDsn->getScheme());
$this->assertSame($expectedDsn->getHost(), $actualDsn->getHost());
$this->assertSame($expectedDsn->getPort(), $actualDsn->getPort());
$this->assertSame($expectedDsn->getUser(), $actualDsn->getUser());
$this->assertSame($expectedDsn->getPassword(), $actualDsn->getPassword());
$this->assertSame($expectedDsn->getPath(), $actualDsn->getPath());
$this->assertSame($expectedDsn->getOption('from'), $actualDsn->getOption('from'));

$this->assertSame($string, $actualDsn->getOriginalDsn());
$this->assertSame($scheme, $dsn->getScheme());
$this->assertSame($host, $dsn->getHost());
$this->assertSame($user, $dsn->getUser());
$this->assertSame($password, $dsn->getPassword());
$this->assertSame($port, $dsn->getPort());
$this->assertSame($path, $dsn->getPath());
$this->assertSame($options, $dsn->getOptions());
}

public function fromStringProvider(): iterable
public function constructProvider(): iterable
{
yield 'simple dsn' => [
'scheme://localhost',
new Dsn('scheme', 'localhost', null, null, null, [], null),
'scheme',
'localhost',
];

yield 'simple dsn including @ sign, but no user/password/token' => [
'scheme://@localhost',
new Dsn('scheme', 'localhost', null, null),
'scheme',
'localhost',
];

yield 'simple dsn including : sign and @ sign, but no user/password/token' => [
'scheme://:@localhost',
new Dsn('scheme', 'localhost', null, null),
'scheme',
'localhost',
];

yield 'simple dsn including user, : sign and @ sign, but no password' => [
'scheme://user1:@localhost',
new Dsn('scheme', 'localhost', 'user1', null),
'scheme',
'localhost',
'user1',
];

yield 'simple dsn including : sign, password, and @ sign, but no user' => [
'scheme://:pass@localhost',
new Dsn('scheme', 'localhost', null, 'pass'),
'scheme',
'localhost',
null,
'pass',
];

yield 'dsn with user and pass' => [
'scheme://u$er:pa$s@localhost',
new Dsn('scheme', 'localhost', 'u$er', 'pa$s', null, [], null),
'scheme',
'localhost',
'u$er',
'pa$s',
];

yield 'dsn with user and pass and custom port' => [
'scheme://u$er:pa$s@localhost:8000',
new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', [], null),
'scheme',
'localhost',
'u$er',
'pa$s',
8000,
];

yield 'dsn with user and pass, custom port and custom path' => [
'scheme://u$er:pa$s@localhost:8000/channel',
new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', [], '/channel'),
'scheme',
'localhost',
'u$er',
'pa$s',
8000,
[],
'/channel',
];

yield 'dsn with user and pass, custom port, custom path and custom options' => [
yield 'dsn with user and pass, custom port, custom path and custom option' => [
'scheme://u$er:pa$s@localhost:8000/channel?from=FROM',
new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', ['from' => 'FROM'], '/channel'),
'scheme',
'localhost',
'u$er',
'pa$s',
8000,
[
'from' => 'FROM',
],
'/channel',
];

yield 'dsn with user and pass, custom port, custom path and custom options' => [
'scheme://u$er:pa$s@localhost:8000/channel?from=FROM&to=TO',
'scheme',
'localhost',
'u$er',
'pa$s',
8000,
[
'from' => 'FROM',
'to' => 'TO',
],
'/channel',
];

yield 'dsn with user and pass, custom port, custom path and custom options and custom options keep the same order' => [
'scheme://u$er:pa$s@localhost:8000/channel?to=TO&from=FROM',
'scheme',
'localhost',
'u$er',
'pa$s',
8000,
[
'to' => 'TO',
'from' => 'FROM',
],
'/channel',
];
}

/**
* @dataProvider invalidDsnProvider
*/
public function testInvalidDsn(string $dsn, string $exceptionMessage)
public function testInvalidDsn(string $dsnString, string $exceptionMessage)
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($exceptionMessage);
Dsn::fromString($dsn);

new Dsn($dsnString);
}

public function invalidDsnProvider(): iterable
Expand All @@ -112,38 +169,75 @@ public function invalidDsnProvider(): iterable
];
}

public function testGetOption()
/**
* @dataProvider getOptionProvider
*/
public function testGetOption($expected, string $dsnString, string $option, ?string $default = null)
{
$options = ['with_value' => 'some value', 'nullable' => null];
$dsn = new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', $options, '/channel');
$dsn = new Dsn($dsnString);

$this->assertSame('some value', $dsn->getOption('with_value'));
$this->assertSame('default', $dsn->getOption('nullable', 'default'));
$this->assertSame('default', $dsn->getOption('not_existent_property', 'default'));
$this->assertSame($expected, $dsn->getOption($option, $default));
}

public function testGetRequiredOptionGetsOptionIfSet()
public function getOptionProvider(): iterable
{
$options = ['with_value' => 'some value'];
$dsn = new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', $options, '/channel');
yield [
'foo',
'scheme://localhost?with_value=foo',
'with_value',
];

yield [
'',
'scheme://localhost?empty=',
'empty',
];
Comment on lines +190 to +194
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to return null in case of an empty string, wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabpot thoughts?


$this->assertSame('some value', $dsn->getRequiredOption('with_value'));
yield [
'0',
'scheme://localhost?zero=0',
'zero',
];

yield [
'default-value',
'scheme://localhost?option=value',
'non_existent_property',
'default-value',
];
}

/**
* @dataProvider getRequiredOptionProvider
*/
public function testGetRequiredOption(string $expectedValue, string $options, string $option)
{
$dsn = new Dsn(sprintf('scheme://localhost?%s', $options));

$this->assertSame($expectedValue, $dsn->getRequiredOption($option));
}

public function testGetRequiredOptionGetsOptionIfValueIsZero()
public function getRequiredOptionProvider(): iterable
{
$options = ['timeout' => 0];
$dsn = new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', $options, '/channel');
yield [
'value',
'with_value=value',
'with_value',
];

$this->assertSame(0, $dsn->getRequiredOption('timeout'));
yield [
'0',
'timeout=0',
'timeout',
];
}

/**
* @dataProvider getRequiredOptionThrowsMissingRequiredOptionExceptionProvider
*/
public function testGetRequiredOptionThrowsMissingRequiredOptionException(string $expectedExceptionMessage, array $options, string $option)
public function testGetRequiredOptionThrowsMissingRequiredOptionException(string $expectedExceptionMessage, string $options, string $option)
{
$dsn = new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', $options, '/channel');
$dsn = new Dsn(sprintf('scheme://localhost?%s', $options));

$this->expectException(MissingRequiredOptionException::class);
$this->expectExceptionMessage($expectedExceptionMessage);
Expand All @@ -155,20 +249,14 @@ public function getRequiredOptionThrowsMissingRequiredOptionExceptionProvider():
{
yield [
'The option "foo_bar" is required but missing.',
['with_value' => 'some value'],
'with_value=value',
'foo_bar',
];

yield [
'The option "with_empty_string" is required but missing.',
['with_empty_string' => ''],
'with_empty_string=',
'with_empty_string',
];

yield [
'The option "with_null" is required but missing.',
['with_null' => null],
'with_null',
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public function testCreateThrowsUnsupportedSchemeException()
{
$this->expectException(UnsupportedSchemeException::class);

$this->nullTransportFactory->create(new Dsn('foo', ''));
$this->nullTransportFactory->create(new Dsn('foo://localhost'));
}

public function testCreate()
{
$this->assertInstanceOf(
NullTransport::class,
$this->nullTransportFactory->create(new Dsn('null', ''))
$this->nullTransportFactory->create(new Dsn('null://null'))
);
}
}
Loading