-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] Add telegram tests #34539
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportFactoryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Telegram\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory; | ||
use Symfony\Component\Notifier\Exception\IncompleteDsnException; | ||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
|
||
final class TelegramTransportFactoryTest extends TestCase | ||
{ | ||
public function testCreateWithDsn(): void | ||
{ | ||
$factory = new TelegramTransportFactory(); | ||
|
||
$host = 'testHost'; | ||
$channel = 'testChannel'; | ||
|
||
$transport = $factory->create(Dsn::fromString(sprintf('telegram://%s@%s/?channel=%s', 'testUser:testPassword', $host, $channel))); | ||
|
||
$this->assertSame(sprintf('telegram://%s?channel=%s', $host, $channel), (string) $transport); | ||
} | ||
|
||
public function testCreateWithNoPasswordThrowsMalformed(): void | ||
{ | ||
$factory = new TelegramTransportFactory(); | ||
|
||
$this->expectException(IncompleteDsnException::class); | ||
$factory->create(Dsn::fromString(sprintf('telegram://%s@%s/?channel=%s', 'simpleToken', 'testHost', 'testChannel'))); | ||
} | ||
|
||
public function testCreateWithNoTokenThrowsMalformed(): void | ||
{ | ||
$factory = new TelegramTransportFactory(); | ||
|
||
$this->expectException(IncompleteDsnException::class); | ||
$factory->create(Dsn::fromString(sprintf('telegram://%s/?channel=%s', 'testHost', 'testChannel'))); | ||
} | ||
|
||
public function testSupportsTelegramScheme(): void | ||
{ | ||
$factory = new TelegramTransportFactory(); | ||
|
||
$this->assertTrue($factory->supports(Dsn::fromString('telegram://host/?channel=testChannel'))); | ||
$this->assertFalse($factory->supports(Dsn::fromString('somethingElse://host/?channel=testChannel'))); | ||
} | ||
|
||
public function testNonTelegramSchemeThrows(): void | ||
{ | ||
$factory = new TelegramTransportFactory(); | ||
|
||
$this->expectException(UnsupportedSchemeException::class); | ||
$factory->create(Dsn::fromString('somethingElse://user:pwd@host/?channel=testChannel')); | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Telegram\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransport; | ||
use Symfony\Component\Notifier\Exception\LogicException; | ||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Message\ChatMessage; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\MessageOptionsInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
final class TelegramTransportTest extends TestCase | ||
{ | ||
public function testToStringContainsProperties(): void | ||
{ | ||
$channel = 'testChannel'; | ||
|
||
$transport = new TelegramTransport('testToken', $channel, $this->createMock(HttpClientInterface::class)); | ||
$transport->setHost('testHost'); | ||
|
||
$this->assertSame(sprintf('telegram://%s?channel=%s', 'testHost', $channel), (string) $transport); | ||
} | ||
|
||
public function testSupportsChatMessage(): void | ||
{ | ||
$transport = new TelegramTransport('testToken', 'testChannel', $this->createMock(HttpClientInterface::class)); | ||
|
||
$this->assertTrue($transport->supports(new ChatMessage('testChatMessage'))); | ||
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class))); | ||
} | ||
|
||
public function testSendNonChatMessageThrows(): void | ||
{ | ||
$this->expectException(LogicException::class); | ||
$transport = new TelegramTransport('testToken', 'testChannel', $this->createMock(HttpClientInterface::class)); | ||
|
||
$transport->send($this->createMock(MessageInterface::class)); | ||
} | ||
|
||
public function testSendWithErrorResponseThrows(): void | ||
{ | ||
$this->expectException(TransportException::class); | ||
$this->expectExceptionMessageRegExp('/testDescription.+testErrorCode/'); | ||
|
||
$response = $this->createMock(ResponseInterface::class); | ||
$response->expects($this->exactly(2)) | ||
->method('getStatusCode') | ||
->willReturn(400); | ||
$response->expects($this->once()) | ||
->method('getContent') | ||
->willReturn(json_encode(['description' => 'testDescription', 'error_code' => 'testErrorCode'])); | ||
|
||
$client = new MockHttpClient(static function () use ($response): ResponseInterface { | ||
return $response; | ||
}); | ||
|
||
$transport = new TelegramTransport('testToken', 'testChannel', $client); | ||
|
||
$transport->send(new ChatMessage('testMessage')); | ||
} | ||
|
||
public function testSendWithOptions(): void | ||
{ | ||
$channel = 'testChannel'; | ||
|
||
$response = $this->createMock(ResponseInterface::class); | ||
$response->expects($this->exactly(2)) | ||
->method('getStatusCode') | ||
->willReturn(200); | ||
$response->expects($this->once()) | ||
->method('getContent') | ||
->willReturn(''); | ||
|
||
$expectedBody = [ | ||
'chat_id' => $channel, | ||
'text' => 'testMessage', | ||
'parse_mode' => 'Markdown', | ||
]; | ||
|
||
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface { | ||
$this->assertEquals($expectedBody, json_decode($options['body'], true)); | ||
|
||
return $response; | ||
}); | ||
|
||
$transport = new TelegramTransport('testToken', $channel, $client); | ||
|
||
$transport->send(new ChatMessage('testMessage')); | ||
} | ||
|
||
public function testSendWithChannelOverride(): void | ||
{ | ||
$channelOverride = 'channelOverride'; | ||
|
||
$response = $this->createMock(ResponseInterface::class); | ||
$response->expects($this->exactly(2)) | ||
->method('getStatusCode') | ||
->willReturn(200); | ||
$response->expects($this->once()) | ||
->method('getContent') | ||
->willReturn(''); | ||
|
||
$expectedBody = [ | ||
'chat_id' => $channelOverride, | ||
'text' => 'testMessage', | ||
'parse_mode' => 'Markdown', | ||
]; | ||
|
||
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface { | ||
$this->assertEquals($expectedBody, json_decode($options['body'], true)); | ||
|
||
return $response; | ||
}); | ||
|
||
$transport = new TelegramTransport('testToken', 'defaultChannel', $client); | ||
|
||
$messageOptions = $this->createMock(MessageOptionsInterface::class); | ||
$messageOptions | ||
->expects($this->once()) | ||
->method('getRecipientId') | ||
->willReturn($channelOverride); | ||
|
||
$transport->send(new ChatMessage('testMessage', $messageOptions)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added because the
AbstractTransport
uses it in it's constructor. Not sure if it should actually be inrequire
?