From 8609d67d2f0b6ae5adfba85021722443dd332fb8 Mon Sep 17 00:00:00 2001 From: Irfaq Syed Date: Sat, 15 Oct 2022 07:26:19 +0530 Subject: [PATCH] Add More Tests (#157) --- tests/Feature/TelegramChannelTest.php | 17 ++----- tests/Feature/TelegramContactTest.php | 31 ++++++++---- tests/Feature/TelegramLocationTest.php | 47 ++++++++++++------- tests/Feature/TelegramMessageTest.php | 19 ++++++++ tests/Feature/TelegramPollTest.php | 33 +++++++++++++ tests/TestCase.php | 45 ++++++++++++++++++ tests/TestSupport/TestContactNotification.php | 26 ++++++++++ .../TestSupport/TestLocationNotification.php | 31 ++++++++++++ tests/TestSupport/TestPollNotification.php | 24 ++++++++++ 9 files changed, 236 insertions(+), 37 deletions(-) create mode 100644 tests/TestSupport/TestContactNotification.php create mode 100644 tests/TestSupport/TestLocationNotification.php create mode 100644 tests/TestSupport/TestPollNotification.php diff --git a/tests/Feature/TelegramChannelTest.php b/tests/Feature/TelegramChannelTest.php index 2862bf7..03c1985 100644 --- a/tests/Feature/TelegramChannelTest.php +++ b/tests/Feature/TelegramChannelTest.php @@ -1,25 +1,16 @@ true, 'result' => ['message_id' => 123, 'chat' => ['id' => 12345]]]; + $notifiable = new TestNotifiable(); + $notification = new TestNotification(); - $this->telegram - ->shouldReceive('sendMessage') - ->once() - ->with([ - 'text' => 'Laravel Notification Channels are awesome!', - 'parse_mode' => 'Markdown', - 'chat_id' => 12345, - ]) - ->andReturns(new Response(200, [], json_encode($expectedResponse))); - - $actualResponse = $this->channel->send(new TestNotifiable(), new TestNotification()); + $expectedResponse = ['ok' => true, 'result' => ['message_id' => 123, 'chat' => ['id' => 12345]]]; + $actualResponse = $this->sendMockNotification('sendMessage', $notifiable, $notification, $expectedResponse); expect($actualResponse)->toBe($expectedResponse); }); diff --git a/tests/Feature/TelegramContactTest.php b/tests/Feature/TelegramContactTest.php index 15c7b17..7c82f9c 100644 --- a/tests/Feature/TelegramContactTest.php +++ b/tests/Feature/TelegramContactTest.php @@ -1,6 +1,8 @@ firstName('Faissal'); - expect($message->getPayloadValue('first_name'))->toEqual('Faissal'); + $message->firstName('John'); + expect($message->getPayloadValue('first_name'))->toEqual('John'); }); test('the last name can be set for the contact', function () { $message = new TelegramContact(); - $message->lastName('Wahabali'); - expect($message->getPayloadValue('last_name'))->toEqual('Wahabali'); + $message->lastName('Doe'); + expect($message->getPayloadValue('last_name'))->toEqual('Doe'); }); test('the card can be set for the contact', function () { @@ -53,16 +55,29 @@ it('can return the payload as an array', function () { $message = new TelegramContact('00000000'); $message->to(12345); - $message->firstName('Faissal'); - $message->lastName('Wahabali'); + $message->firstName('John'); + $message->lastName('Doe'); $message->vCard('vCard'); $expected = [ 'chat_id' => 12345, 'phone_number' => '00000000', - 'first_name' => 'Faissal', - 'last_name' => 'Wahabali', + 'first_name' => 'John', + 'last_name' => 'Doe', 'vcard' => 'vCard', ]; expect($message->toArray())->toEqual($expected); }); + +it('can send a contact', function () { + $notifiable = new TestNotifiable(); + $notification = new TestContactNotification(); + + $expectedResponse = $this->makeMockResponse([ + 'contact' => collect($notification->toTelegram($notifiable)->toArray())->except('chat_id')->toArray(), + ]); + + $actualResponse = $this->sendMockNotification('sendContact', $notifiable, $notification, $expectedResponse); + + expect($actualResponse)->toBe($expectedResponse); +}); diff --git a/tests/Feature/TelegramLocationTest.php b/tests/Feature/TelegramLocationTest.php index 94b008a..3bccb84 100644 --- a/tests/Feature/TelegramLocationTest.php +++ b/tests/Feature/TelegramLocationTest.php @@ -1,24 +1,26 @@ getPayloadValue('latitude')) - ->toEqual(TEST_LONG) + ->toEqual(TEST_LAT) ->and($message->getPayloadValue('longitude')) - ->toEqual(TEST_LAT); + ->toEqual(TEST_LONG); }); it('accepts content when created', function () { - $message = TelegramLocation::create(TEST_LONG, TEST_LAT); + $message = TelegramLocation::create(TEST_LAT, TEST_LONG); expect($message->getPayloadValue('latitude')) - ->toEqual(TEST_LONG) + ->toEqual(TEST_LAT) ->and($message->getPayloadValue('longitude')) - ->toEqual(TEST_LAT); + ->toEqual(TEST_LONG); }); test('the recipients chat id can be set', function () { @@ -27,16 +29,16 @@ expect($message->getPayloadValue('chat_id'))->toEqual(12345); }); -test('the notification longitude can be set', function () { +test('the notification latitude can be set', function () { $message = new TelegramLocation(); - $message->longitude(TEST_LAT); - expect($message->getPayloadValue('longitude'))->toEqual(TEST_LAT); + $message->latitude(TEST_LAT); + expect($message->getPayloadValue('latitude'))->toEqual(TEST_LAT); }); -test('the notification latitude can be set', function () { +test('the notification longitude can be set', function () { $message = new TelegramLocation(); - $message->latitude(TEST_LONG); - expect($message->getPayloadValue('latitude'))->toEqual(TEST_LONG); + $message->longitude(TEST_LONG); + expect($message->getPayloadValue('longitude'))->toEqual(TEST_LONG); }); test('additional options can be set for the message', function () { @@ -54,15 +56,28 @@ }); it('can return the payload as an array', function () { - $message = new TelegramLocation(TEST_LONG, TEST_LAT); + $message = new TelegramLocation(TEST_LAT, TEST_LONG); $message->to(12345); $message->options(['foo' => 'bar']); $expected = [ 'chat_id' => 12345, 'foo' => 'bar', - 'longitude' => TEST_LAT, - 'latitude' => TEST_LONG, + 'latitude' => TEST_LAT, + 'longitude' => TEST_LONG, ]; expect($message->toArray())->toEqual($expected); }); + +it('can send a location', function () { + $notifiable = new TestNotifiable(); + $notification = new TestLocationNotification(TEST_LAT, TEST_LONG); + + $expectedResponse = $this->makeMockResponse([ + 'location' => collect($notification->toTelegram($notifiable)->toArray())->except('chat_id')->toArray(), + ]); + + $actualResponse = $this->sendMockNotification('sendLocation', $notifiable, $notification, $expectedResponse); + + expect($actualResponse)->toBe($expectedResponse); +}); diff --git a/tests/Feature/TelegramMessageTest.php b/tests/Feature/TelegramMessageTest.php index 7380225..87ebcf8 100644 --- a/tests/Feature/TelegramMessageTest.php +++ b/tests/Feature/TelegramMessageTest.php @@ -111,3 +111,22 @@ expect($message->getPayloadValue('reply_markup'))->toEqual('{"inline_keyboard":[[{"text":"Laravel","url":"https:\/\/laravel.com"},{"text":"Github","url":"https:\/\/github.com"}]]}'); }); + +it('can set token', function () { + $message = TelegramMessage::create()->token('12345'); + + expect($message->hasToken()) + ->toBeTrue() + ->and($message->token) + ->toEqual('12345'); +}); + +it('can set the parse mode', function () { + $message = TelegramMessage::create()->options(['parse_mode' => 'HTML']); + expect($message->getPayloadValue('parse_mode'))->toEqual('HTML'); +}); + +it('can set the disable web page preview', function () { + $message = TelegramMessage::create()->options(['disable_web_page_preview' => true]); + expect($message->getPayloadValue('disable_web_page_preview'))->toBeTrue(); +}); diff --git a/tests/Feature/TelegramPollTest.php b/tests/Feature/TelegramPollTest.php index ddda6c3..68fd1f0 100644 --- a/tests/Feature/TelegramPollTest.php +++ b/tests/Feature/TelegramPollTest.php @@ -1,6 +1,8 @@ toArray())->toEqual($expected); }); + +it('can send a poll', function () { + $notifiable = new TestNotifiable(); + $notification = new TestPollNotification(); + + $expectedResponse = $this->makeMockResponse([ + 'poll' => [ + 'id' => '1234567890101112', + 'question' => "Isn't Telegram Notification Channel Awesome?", + 'options' => [ + [ + 'text' => 'Yes', + 'voter_count' => 0, + ], + [ + 'text' => 'No', + 'voter_count' => 0, + ], + ], + 'total_voter_count' => 0, + 'is_closed' => false, + 'is_anonymous' => true, + 'type' => 'regular', + 'allows_multiple_answers' => false, + ], + ]); + + $actualResponse = $this->sendMockNotification('sendPoll', $notifiable, $notification, $expectedResponse); + + expect($actualResponse)->toBe($expectedResponse); +}); diff --git a/tests/TestCase.php b/tests/TestCase.php index 725ea56..00bc07a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,7 +2,9 @@ namespace NotificationChannels\Telegram\Tests; +use GuzzleHttp\Psr7\Response; use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Notifications\Notification; use Mockery; use NotificationChannels\Telegram\Telegram; use NotificationChannels\Telegram\TelegramChannel; @@ -31,6 +33,49 @@ protected function setUp(): void $this->channel = new TelegramChannel($this->dispatcher); } + protected function sendMockNotification( + string $shouldReceive, + mixed $notifiable, + Notification $notification, + array $expectedResponse + ) { + $this->telegram + ->shouldReceive($shouldReceive) + ->with($notification->toTelegram($notifiable)->toArray()) + ->once() + ->andReturns(new Response(200, [], json_encode($expectedResponse))); + + return $this->channel->send($notifiable, $notification); + } + + protected function makeMockResponse(array $result) + { + $payload = [ + 'ok' => true, + 'result' => [ + 'message_id' => 9090, + 'from' => [ + 'id' => 12345678, + 'is_bot' => true, + 'first_name' => 'MyBot', + 'username' => 'MyBot', + ], + 'chat' => [ + 'id' => 90909090, + 'first_name' => 'John', + 'last_name' => 'Doe', + 'username' => 'testuser', + 'type' => 'private', + ], + 'date' => 1600000000, + ], + ]; + + $payload['result'] = array_merge($payload['result'], $result); + + return $payload; + } + protected function getPackageProviders($app): array { return [ diff --git a/tests/TestSupport/TestContactNotification.php b/tests/TestSupport/TestContactNotification.php new file mode 100644 index 0000000..589db3d --- /dev/null +++ b/tests/TestSupport/TestContactNotification.php @@ -0,0 +1,26 @@ +to(12345) + ->phoneNumber('123456789') + ->firstName('John') + ->lastName('Doe') + ->vCard('vCard'); + } +} diff --git a/tests/TestSupport/TestLocationNotification.php b/tests/TestSupport/TestLocationNotification.php new file mode 100644 index 0000000..e42de05 --- /dev/null +++ b/tests/TestSupport/TestLocationNotification.php @@ -0,0 +1,31 @@ +to(12345) + ->latitude($this->latitude) + ->longitude($this->longitude) + ->options(['horizontal_accuracy' => 100]); + } +} diff --git a/tests/TestSupport/TestPollNotification.php b/tests/TestSupport/TestPollNotification.php new file mode 100644 index 0000000..0ea363e --- /dev/null +++ b/tests/TestSupport/TestPollNotification.php @@ -0,0 +1,24 @@ +to(12345) + ->question("Isn't Telegram Notification Channel Awesome?") + ->choices(['Yes', 'No']); + } +}