Skip to content

Commit

Permalink
Add More Tests (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
irazasyed authored Oct 15, 2022
1 parent 6ca14f4 commit 8609d67
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 37 deletions.
17 changes: 4 additions & 13 deletions tests/Feature/TelegramChannelTest.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
<?php

use GuzzleHttp\Psr7\Response;
use Illuminate\Notifications\Events\NotificationFailed;
use NotificationChannels\Telegram\Exceptions\CouldNotSendNotification;
use NotificationChannels\Telegram\Tests\TestSupport\TestNotifiable;
use NotificationChannels\Telegram\Tests\TestSupport\TestNotification;

it('can send a message', function () {
$expectedResponse = ['ok' => 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);
});
Expand Down
31 changes: 23 additions & 8 deletions tests/Feature/TelegramContactTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use NotificationChannels\Telegram\TelegramContact;
use NotificationChannels\Telegram\Tests\TestSupport\TestContactNotification;
use NotificationChannels\Telegram\Tests\TestSupport\TestNotifiable;

it('accepts phone number when constructed', function () {
$message = new TelegramContact('00000000');
Expand All @@ -26,14 +28,14 @@

test('the first name can be set for the contact', function () {
$message = new TelegramContact();
$message->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 () {
Expand All @@ -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);
});
47 changes: 31 additions & 16 deletions tests/Feature/TelegramLocationTest.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
<?php

use NotificationChannels\Telegram\TelegramLocation;
use NotificationChannels\Telegram\Tests\TestSupport\TestLocationNotification;
use NotificationChannels\Telegram\Tests\TestSupport\TestNotifiable;

const TEST_LONG = -77.0364;
const TEST_LAT = 38.8951;
const TEST_LONG = -77.0364;

it('accepts content when constructed', function () {
$message = new TelegramLocation(TEST_LONG, TEST_LAT);
$message = new TelegramLocation(TEST_LAT, TEST_LONG);
expect($message->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 () {
Expand All @@ -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 () {
Expand All @@ -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);
});
19 changes: 19 additions & 0 deletions tests/Feature/TelegramMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
33 changes: 33 additions & 0 deletions tests/Feature/TelegramPollTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use NotificationChannels\Telegram\TelegramPoll;
use NotificationChannels\Telegram\Tests\TestSupport\TestNotifiable;
use NotificationChannels\Telegram\Tests\TestSupport\TestPollNotification;

it('accepts question when constructed', function () {
$message = new TelegramPoll("Aren't Laravel Notification Channels awesome?");
Expand Down Expand Up @@ -50,3 +52,34 @@

expect($message->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);
});
45 changes: 45 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 [
Expand Down
26 changes: 26 additions & 0 deletions tests/TestSupport/TestContactNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace NotificationChannels\Telegram\Tests\TestSupport;

use Illuminate\Notifications\Notification;
use NotificationChannels\Telegram\TelegramContact;

/**
* Class TestContactNotification.
*/
class TestContactNotification extends Notification
{
/**
* @param $notifiable
* @return TelegramContact
*/
public function toTelegram($notifiable): TelegramContact
{
return TelegramContact::create()
->to(12345)
->phoneNumber('123456789')
->firstName('John')
->lastName('Doe')
->vCard('vCard');
}
}
31 changes: 31 additions & 0 deletions tests/TestSupport/TestLocationNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace NotificationChannels\Telegram\Tests\TestSupport;

use Illuminate\Notifications\Notification;
use NotificationChannels\Telegram\TelegramLocation;

/**
* Class TestLocationNotification.
*/
class TestLocationNotification extends Notification
{
public function __construct(
private float|string $latitude,
private float|string $longitude
) {
}

/**
* @param $notifiable
* @return TelegramLocation
*/
public function toTelegram($notifiable): TelegramLocation
{
return TelegramLocation::create()
->to(12345)
->latitude($this->latitude)
->longitude($this->longitude)
->options(['horizontal_accuracy' => 100]);
}
}
24 changes: 24 additions & 0 deletions tests/TestSupport/TestPollNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace NotificationChannels\Telegram\Tests\TestSupport;

use Illuminate\Notifications\Notification;
use NotificationChannels\Telegram\TelegramPoll;

/**
* Class TestPollNotification.
*/
class TestPollNotification extends Notification
{
/**
* @param $notifiable
* @return TelegramPoll
*/
public function toTelegram($notifiable): TelegramPoll
{
return TelegramPoll::create()
->to(12345)
->question("Isn't Telegram Notification Channel Awesome?")
->choices(['Yes', 'No']);
}
}

0 comments on commit 8609d67

Please sign in to comment.