Merge branch '5.2' into 5.x

* 5.2:
  [Notifier] Fix parsing Dsn with empty user/password
  Remove void return type from test methods
  [Notifier] [Discord] Use private const and mb_strlen()
  [Notifier] Set message id on SentMessage
  [Notifier] Fix toString when optional parameter is not set
  [Notifier] Use assertSame()
This commit is contained in:
Fabien Potencier 2020-12-18 08:28:30 +01:00
commit 8dfd31d975
13 changed files with 72 additions and 25 deletions

View File

@ -20,12 +20,12 @@ class DsnTest extends TestCase
/**
* @dataProvider fromStringProvider
*/
public function testFromString(string $string, Dsn $dsn): void
public function testFromString(string $string, Dsn $dsn)
{
$this->assertEquals($dsn, Dsn::fromString($string));
}
public function testGetOption(): void
public function testGetOption()
{
$options = ['with_value' => 'some value', 'nullable' => null];
$dsn = new Dsn('smtp', 'example.com', null, null, null, $options);
@ -38,7 +38,7 @@ class DsnTest extends TestCase
/**
* @dataProvider invalidDsnProvider
*/
public function testInvalidDsn(string $dsn, string $exceptionMessage): void
public function testInvalidDsn(string $dsn, string $exceptionMessage)
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($exceptionMessage);

View File

@ -21,7 +21,7 @@ use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\Connection;
*/
class AmazonSqsIntegrationTest extends TestCase
{
public function testConnectionSendToFifoQueueAndGet(): void
public function testConnectionSendToFifoQueueAndGet()
{
if (!getenv('MESSENGER_SQS_FIFO_QUEUE_DSN')) {
$this->markTestSkipped('The "MESSENGER_SQS_FIFO_QUEUE_DSN" environment variable is required.');
@ -30,7 +30,7 @@ class AmazonSqsIntegrationTest extends TestCase
$this->execute(getenv('MESSENGER_SQS_FIFO_QUEUE_DSN'));
}
public function testConnectionSendAndGet(): void
public function testConnectionSendAndGet()
{
if (!getenv('MESSENGER_SQS_DSN')) {
$this->markTestSkipped('The "MESSENGER_SQS_DSN" environment variable is required.');

View File

@ -26,7 +26,7 @@ class StopWorkerOnFailureLimitListenerTest extends TestCase
/**
* @dataProvider countProvider
*/
public function testWorkerStopsWhenMaximumCountReached(int $max, bool $shouldStop): void
public function testWorkerStopsWhenMaximumCountReached(int $max, bool $shouldStop)
{
$worker = $this->createMock(Worker::class);
$worker->expects($shouldStop ? $this->atLeastOnce() : $this->never())->method('stop');
@ -53,7 +53,7 @@ class StopWorkerOnFailureLimitListenerTest extends TestCase
yield [4, false];
}
public function testWorkerLogsMaximumCountReachedWhenLoggerIsGiven(): void
public function testWorkerLogsMaximumCountReachedWhenLoggerIsGiven()
{
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->once())->method('info')

View File

@ -30,6 +30,8 @@ final class DiscordTransport extends AbstractTransport
{
protected const HOST = 'discord.com';
private const SUBJECT_LIMIT = 2000;
private $token;
private $webhookId;
@ -66,7 +68,7 @@ final class DiscordTransport extends AbstractTransport
$content = $message->getSubject();
if (\strlen($content) > 2000) {
if (mb_strlen($content, 'UTF-8') > self::SUBJECT_LIMIT) {
throw new LengthException('The subject length of a Discord message must not exceed 2000 characters.');
}

View File

@ -58,7 +58,7 @@ final class DiscordTransportTest extends TestCase
$this->expectException(LengthException::class);
$this->expectExceptionMessage('The subject length of a Discord message must not exceed 2000 characters.');
$transport->send(new ChatMessage(str_repeat('d', 2001)));
$transport->send(new ChatMessage(str_repeat('', 2001)));
}
public function testSendWithErrorResponseThrows()

View File

@ -91,7 +91,10 @@ final class LinkedInTransport extends AbstractTransport
throw new TransportException(sprintf('Unable to post the Linkedin message : "%s".', $result['error']), $response);
}
return new SentMessage($message, (string) $this);
$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId($result['id']);
return $sentMessage;
}
private function bodyFromMessageWithNoOption(MessageInterface $message): array

View File

@ -44,6 +44,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

@ -49,6 +49,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();
@ -114,7 +121,7 @@ JSON;
];
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
$this->assertEquals($expectedBody, json_decode($options['body'], true));
$this->assertSame($expectedBody, json_decode($options['body'], true));
return $response;
});
@ -170,7 +177,7 @@ JSON;
];
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
$this->assertEquals($expectedBody, json_decode($options['body'], true));
$this->assertSame($expectedBody, json_decode($options['body'], true));
return $response;
});
@ -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');
}

View File

@ -15,12 +15,12 @@ use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Transport\Dsn;
class DsnTest extends TestCase
final class DsnTest extends TestCase
{
/**
* @dataProvider fromStringProvider
*/
public function testFromString(string $string, Dsn $expectedDsn): void
public function testFromString(string $string, Dsn $expectedDsn)
{
$actualDsn = Dsn::fromString($string);
@ -42,6 +42,26 @@ class DsnTest extends TestCase
new Dsn('scheme', 'localhost', null, null, null, [], null),
];
yield 'simple dsn including @ sign, but no user/password/token' => [
'scheme://@localhost',
new Dsn('scheme', 'localhost', null, null),
];
yield 'simple dsn including : sign and @ sign, but no user/password/token' => [
'scheme://:@localhost',
new Dsn('scheme', 'localhost', null, null),
];
yield 'simple dsn including user, : sign and @ sign, but no password' => [
'scheme://user1:@localhost',
new Dsn('scheme', 'localhost', 'user1', null),
];
yield 'simple dsn including : sign, password, and @ sign, but no user' => [
'scheme://:pass@localhost',
new Dsn('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),
@ -66,7 +86,7 @@ class DsnTest extends TestCase
/**
* @dataProvider invalidDsnProvider
*/
public function testInvalidDsn(string $dsn, string $exceptionMessage): void
public function testInvalidDsn(string $dsn, string $exceptionMessage)
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($exceptionMessage);
@ -91,7 +111,7 @@ class DsnTest extends TestCase
];
}
public function testGetOption(): void
public function testGetOption()
{
$options = ['with_value' => 'some value', 'nullable' => null];
$dsn = new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', $options, '/channel');

View File

@ -54,8 +54,8 @@ final class Dsn
throw new InvalidArgumentException(sprintf('The "%s" notifier DSN must contain a host (use "default" by default).', $dsn));
}
$user = isset($parsedDsn['user']) ? urldecode($parsedDsn['user']) : null;
$password = isset($parsedDsn['pass']) ? urldecode($parsedDsn['pass']) : null;
$user = '' !== ($parsedDsn['user'] ?? '') ? urldecode($parsedDsn['user']) : null;
$password = '' !== ($parsedDsn['pass'] ?? '') ? urldecode($parsedDsn['pass']) : null;
$port = $parsedDsn['port'] ?? null;
$path = $parsedDsn['path'] ?? null;
parse_str($parsedDsn['query'] ?? '', $query);

View File

@ -23,7 +23,7 @@ class ExpressionLanguageSyntaxValidatorTest extends ConstraintValidatorTestCase
return new ExpressionLanguageSyntaxValidator(new ExpressionLanguage());
}
public function testExpressionValid(): void
public function testExpressionValid()
{
$this->validator->validate('1 + 1', new ExpressionLanguageSyntax([
'message' => 'myMessage',
@ -33,7 +33,7 @@ class ExpressionLanguageSyntaxValidatorTest extends ConstraintValidatorTestCase
$this->assertNoViolation();
}
public function testExpressionWithoutNames(): void
public function testExpressionWithoutNames()
{
$this->validator->validate('1 + 1', new ExpressionLanguageSyntax([
'message' => 'myMessage',
@ -42,7 +42,7 @@ class ExpressionLanguageSyntaxValidatorTest extends ConstraintValidatorTestCase
$this->assertNoViolation();
}
public function testExpressionWithAllowedVariableName(): void
public function testExpressionWithAllowedVariableName()
{
$this->validator->validate('a + 1', new ExpressionLanguageSyntax([
'message' => 'myMessage',
@ -52,7 +52,7 @@ class ExpressionLanguageSyntaxValidatorTest extends ConstraintValidatorTestCase
$this->assertNoViolation();
}
public function testExpressionIsNotValid(): void
public function testExpressionIsNotValid()
{
$this->validator->validate('a + 1', new ExpressionLanguageSyntax([
'message' => 'myMessage',