bug #39532 [Notifier] Fix toString when optional parameter is not set (OskarStark)

This PR was merged into the 5.2 branch.

Discussion
----------

[Notifier] Fix toString when optional parameter is not set

| Q             | A
| ------------- | ---
| Branch?       | 5.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | ---
| License       | MIT
| Doc PR        | ---

Commits
-------

e61363c1f2 [Notifier] Fix toString when optional parameter is not set
This commit is contained in:
Fabien Potencier 2020-12-17 17:28:54 +01:00
commit cf08a502ac
4 changed files with 25 additions and 3 deletions

View File

@ -43,6 +43,10 @@ final class RocketChatTransport extends AbstractTransport
public function __toString(): string
{
if (null === $this->chatChannel) {
return sprintf('rocketchat://%s', $this->getEndpoint());
}
return sprintf('rocketchat://%s?channel=%s', $this->getEndpoint(), $this->chatChannel);
}

View File

@ -30,6 +30,13 @@ final class RocketChatTransportTest extends TestCase
$this->assertSame('rocketchat://host.test?channel=testChannel', (string) $transport);
}
public function testToStringContainsNoChannelBecauseItsOptional()
{
$transport = $this->createTransport(null);
$this->assertSame('rocketchat://host.test', (string) $transport);
}
public function testSupportsChatMessage()
{
$transport = $this->createTransport();
@ -46,8 +53,8 @@ final class RocketChatTransportTest extends TestCase
$transport->send($this->createMock(MessageInterface::class));
}
private function createTransport(): RocketChatTransport
private function createTransport(?string $channel = 'testChannel'): RocketChatTransport
{
return (new RocketChatTransport('testAccessToken', 'testChannel', $this->createMock(HttpClientInterface::class)))->setHost('host.test');
return (new RocketChatTransport('testAccessToken', $channel, $this->createMock(HttpClientInterface::class)))->setHost('host.test');
}
}

View File

@ -48,6 +48,10 @@ final class TelegramTransport extends AbstractTransport
public function __toString(): string
{
if (null === $this->chatChannel) {
return sprintf('telegram://%s', $this->getEndpoint());
}
return sprintf('telegram://%s?channel=%s', $this->getEndpoint(), $this->chatChannel);
}

View File

@ -31,6 +31,13 @@ final class TelegramTransportTest extends TestCase
$this->assertSame('telegram://host.test?channel=testChannel', (string) $transport);
}
public function testToStringContainsNoChannelBecauseItsOptional()
{
$transport = $this->createTransport(null);
$this->assertSame('telegram://host.test', (string) $transport);
}
public function testSupportsChatMessage()
{
$transport = $this->createTransport();
@ -186,7 +193,7 @@ JSON;
$this->assertEquals('telegram://host.test?channel=defaultChannel', $sentMessage->getTransport());
}
private function createTransport($channel = 'testChannel', ?HttpClientInterface $client = null): TelegramTransport
private function createTransport(?string $channel = 'testChannel', ?HttpClientInterface $client = null): TelegramTransport
{
return (new TelegramTransport('token', $channel, $client ?: $this->createMock(HttpClientInterface::class)))->setHost('host.test');
}