Skip to content

Commit 8609d67

Browse files
authored
Add More Tests (#157)
1 parent 6ca14f4 commit 8609d67

9 files changed

+236
-37
lines changed

tests/Feature/TelegramChannelTest.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
<?php
22

3-
use GuzzleHttp\Psr7\Response;
43
use Illuminate\Notifications\Events\NotificationFailed;
54
use NotificationChannels\Telegram\Exceptions\CouldNotSendNotification;
65
use NotificationChannels\Telegram\Tests\TestSupport\TestNotifiable;
76
use NotificationChannels\Telegram\Tests\TestSupport\TestNotification;
87

98
it('can send a message', function () {
10-
$expectedResponse = ['ok' => true, 'result' => ['message_id' => 123, 'chat' => ['id' => 12345]]];
9+
$notifiable = new TestNotifiable();
10+
$notification = new TestNotification();
1111

12-
$this->telegram
13-
->shouldReceive('sendMessage')
14-
->once()
15-
->with([
16-
'text' => 'Laravel Notification Channels are awesome!',
17-
'parse_mode' => 'Markdown',
18-
'chat_id' => 12345,
19-
])
20-
->andReturns(new Response(200, [], json_encode($expectedResponse)));
21-
22-
$actualResponse = $this->channel->send(new TestNotifiable(), new TestNotification());
12+
$expectedResponse = ['ok' => true, 'result' => ['message_id' => 123, 'chat' => ['id' => 12345]]];
13+
$actualResponse = $this->sendMockNotification('sendMessage', $notifiable, $notification, $expectedResponse);
2314

2415
expect($actualResponse)->toBe($expectedResponse);
2516
});

tests/Feature/TelegramContactTest.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
use NotificationChannels\Telegram\TelegramContact;
4+
use NotificationChannels\Telegram\Tests\TestSupport\TestContactNotification;
5+
use NotificationChannels\Telegram\Tests\TestSupport\TestNotifiable;
46

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

2729
test('the first name can be set for the contact', function () {
2830
$message = new TelegramContact();
29-
$message->firstName('Faissal');
30-
expect($message->getPayloadValue('first_name'))->toEqual('Faissal');
31+
$message->firstName('John');
32+
expect($message->getPayloadValue('first_name'))->toEqual('John');
3133
});
3234

3335
test('the last name can be set for the contact', function () {
3436
$message = new TelegramContact();
35-
$message->lastName('Wahabali');
36-
expect($message->getPayloadValue('last_name'))->toEqual('Wahabali');
37+
$message->lastName('Doe');
38+
expect($message->getPayloadValue('last_name'))->toEqual('Doe');
3739
});
3840

3941
test('the card can be set for the contact', function () {
@@ -53,16 +55,29 @@
5355
it('can return the payload as an array', function () {
5456
$message = new TelegramContact('00000000');
5557
$message->to(12345);
56-
$message->firstName('Faissal');
57-
$message->lastName('Wahabali');
58+
$message->firstName('John');
59+
$message->lastName('Doe');
5860
$message->vCard('vCard');
5961
$expected = [
6062
'chat_id' => 12345,
6163
'phone_number' => '00000000',
62-
'first_name' => 'Faissal',
63-
'last_name' => 'Wahabali',
64+
'first_name' => 'John',
65+
'last_name' => 'Doe',
6466
'vcard' => 'vCard',
6567
];
6668

6769
expect($message->toArray())->toEqual($expected);
6870
});
71+
72+
it('can send a contact', function () {
73+
$notifiable = new TestNotifiable();
74+
$notification = new TestContactNotification();
75+
76+
$expectedResponse = $this->makeMockResponse([
77+
'contact' => collect($notification->toTelegram($notifiable)->toArray())->except('chat_id')->toArray(),
78+
]);
79+
80+
$actualResponse = $this->sendMockNotification('sendContact', $notifiable, $notification, $expectedResponse);
81+
82+
expect($actualResponse)->toBe($expectedResponse);
83+
});
Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
<?php
22

33
use NotificationChannels\Telegram\TelegramLocation;
4+
use NotificationChannels\Telegram\Tests\TestSupport\TestLocationNotification;
5+
use NotificationChannels\Telegram\Tests\TestSupport\TestNotifiable;
46

5-
const TEST_LONG = -77.0364;
67
const TEST_LAT = 38.8951;
8+
const TEST_LONG = -77.0364;
79

810
it('accepts content when constructed', function () {
9-
$message = new TelegramLocation(TEST_LONG, TEST_LAT);
11+
$message = new TelegramLocation(TEST_LAT, TEST_LONG);
1012
expect($message->getPayloadValue('latitude'))
11-
->toEqual(TEST_LONG)
13+
->toEqual(TEST_LAT)
1214
->and($message->getPayloadValue('longitude'))
13-
->toEqual(TEST_LAT);
15+
->toEqual(TEST_LONG);
1416
});
1517

1618
it('accepts content when created', function () {
17-
$message = TelegramLocation::create(TEST_LONG, TEST_LAT);
19+
$message = TelegramLocation::create(TEST_LAT, TEST_LONG);
1820
expect($message->getPayloadValue('latitude'))
19-
->toEqual(TEST_LONG)
21+
->toEqual(TEST_LAT)
2022
->and($message->getPayloadValue('longitude'))
21-
->toEqual(TEST_LAT);
23+
->toEqual(TEST_LONG);
2224
});
2325

2426
test('the recipients chat id can be set', function () {
@@ -27,16 +29,16 @@
2729
expect($message->getPayloadValue('chat_id'))->toEqual(12345);
2830
});
2931

30-
test('the notification longitude can be set', function () {
32+
test('the notification latitude can be set', function () {
3133
$message = new TelegramLocation();
32-
$message->longitude(TEST_LAT);
33-
expect($message->getPayloadValue('longitude'))->toEqual(TEST_LAT);
34+
$message->latitude(TEST_LAT);
35+
expect($message->getPayloadValue('latitude'))->toEqual(TEST_LAT);
3436
});
3537

36-
test('the notification latitude can be set', function () {
38+
test('the notification longitude can be set', function () {
3739
$message = new TelegramLocation();
38-
$message->latitude(TEST_LONG);
39-
expect($message->getPayloadValue('latitude'))->toEqual(TEST_LONG);
40+
$message->longitude(TEST_LONG);
41+
expect($message->getPayloadValue('longitude'))->toEqual(TEST_LONG);
4042
});
4143

4244
test('additional options can be set for the message', function () {
@@ -54,15 +56,28 @@
5456
});
5557

5658
it('can return the payload as an array', function () {
57-
$message = new TelegramLocation(TEST_LONG, TEST_LAT);
59+
$message = new TelegramLocation(TEST_LAT, TEST_LONG);
5860
$message->to(12345);
5961
$message->options(['foo' => 'bar']);
6062
$expected = [
6163
'chat_id' => 12345,
6264
'foo' => 'bar',
63-
'longitude' => TEST_LAT,
64-
'latitude' => TEST_LONG,
65+
'latitude' => TEST_LAT,
66+
'longitude' => TEST_LONG,
6567
];
6668

6769
expect($message->toArray())->toEqual($expected);
6870
});
71+
72+
it('can send a location', function () {
73+
$notifiable = new TestNotifiable();
74+
$notification = new TestLocationNotification(TEST_LAT, TEST_LONG);
75+
76+
$expectedResponse = $this->makeMockResponse([
77+
'location' => collect($notification->toTelegram($notifiable)->toArray())->except('chat_id')->toArray(),
78+
]);
79+
80+
$actualResponse = $this->sendMockNotification('sendLocation', $notifiable, $notification, $expectedResponse);
81+
82+
expect($actualResponse)->toBe($expectedResponse);
83+
});

tests/Feature/TelegramMessageTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,22 @@
111111

112112
expect($message->getPayloadValue('reply_markup'))->toEqual('{"inline_keyboard":[[{"text":"Laravel","url":"https:\/\/laravel.com"},{"text":"Github","url":"https:\/\/github.com"}]]}');
113113
});
114+
115+
it('can set token', function () {
116+
$message = TelegramMessage::create()->token('12345');
117+
118+
expect($message->hasToken())
119+
->toBeTrue()
120+
->and($message->token)
121+
->toEqual('12345');
122+
});
123+
124+
it('can set the parse mode', function () {
125+
$message = TelegramMessage::create()->options(['parse_mode' => 'HTML']);
126+
expect($message->getPayloadValue('parse_mode'))->toEqual('HTML');
127+
});
128+
129+
it('can set the disable web page preview', function () {
130+
$message = TelegramMessage::create()->options(['disable_web_page_preview' => true]);
131+
expect($message->getPayloadValue('disable_web_page_preview'))->toBeTrue();
132+
});

tests/Feature/TelegramPollTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
use NotificationChannels\Telegram\TelegramPoll;
4+
use NotificationChannels\Telegram\Tests\TestSupport\TestNotifiable;
5+
use NotificationChannels\Telegram\Tests\TestSupport\TestPollNotification;
46

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

5153
expect($message->toArray())->toEqual($expected);
5254
});
55+
56+
it('can send a poll', function () {
57+
$notifiable = new TestNotifiable();
58+
$notification = new TestPollNotification();
59+
60+
$expectedResponse = $this->makeMockResponse([
61+
'poll' => [
62+
'id' => '1234567890101112',
63+
'question' => "Isn't Telegram Notification Channel Awesome?",
64+
'options' => [
65+
[
66+
'text' => 'Yes',
67+
'voter_count' => 0,
68+
],
69+
[
70+
'text' => 'No',
71+
'voter_count' => 0,
72+
],
73+
],
74+
'total_voter_count' => 0,
75+
'is_closed' => false,
76+
'is_anonymous' => true,
77+
'type' => 'regular',
78+
'allows_multiple_answers' => false,
79+
],
80+
]);
81+
82+
$actualResponse = $this->sendMockNotification('sendPoll', $notifiable, $notification, $expectedResponse);
83+
84+
expect($actualResponse)->toBe($expectedResponse);
85+
});

tests/TestCase.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace NotificationChannels\Telegram\Tests;
44

5+
use GuzzleHttp\Psr7\Response;
56
use Illuminate\Contracts\Events\Dispatcher;
7+
use Illuminate\Notifications\Notification;
68
use Mockery;
79
use NotificationChannels\Telegram\Telegram;
810
use NotificationChannels\Telegram\TelegramChannel;
@@ -31,6 +33,49 @@ protected function setUp(): void
3133
$this->channel = new TelegramChannel($this->dispatcher);
3234
}
3335

36+
protected function sendMockNotification(
37+
string $shouldReceive,
38+
mixed $notifiable,
39+
Notification $notification,
40+
array $expectedResponse
41+
) {
42+
$this->telegram
43+
->shouldReceive($shouldReceive)
44+
->with($notification->toTelegram($notifiable)->toArray())
45+
->once()
46+
->andReturns(new Response(200, [], json_encode($expectedResponse)));
47+
48+
return $this->channel->send($notifiable, $notification);
49+
}
50+
51+
protected function makeMockResponse(array $result)
52+
{
53+
$payload = [
54+
'ok' => true,
55+
'result' => [
56+
'message_id' => 9090,
57+
'from' => [
58+
'id' => 12345678,
59+
'is_bot' => true,
60+
'first_name' => 'MyBot',
61+
'username' => 'MyBot',
62+
],
63+
'chat' => [
64+
'id' => 90909090,
65+
'first_name' => 'John',
66+
'last_name' => 'Doe',
67+
'username' => 'testuser',
68+
'type' => 'private',
69+
],
70+
'date' => 1600000000,
71+
],
72+
];
73+
74+
$payload['result'] = array_merge($payload['result'], $result);
75+
76+
return $payload;
77+
}
78+
3479
protected function getPackageProviders($app): array
3580
{
3681
return [
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace NotificationChannels\Telegram\Tests\TestSupport;
4+
5+
use Illuminate\Notifications\Notification;
6+
use NotificationChannels\Telegram\TelegramContact;
7+
8+
/**
9+
* Class TestContactNotification.
10+
*/
11+
class TestContactNotification extends Notification
12+
{
13+
/**
14+
* @param $notifiable
15+
* @return TelegramContact
16+
*/
17+
public function toTelegram($notifiable): TelegramContact
18+
{
19+
return TelegramContact::create()
20+
->to(12345)
21+
->phoneNumber('123456789')
22+
->firstName('John')
23+
->lastName('Doe')
24+
->vCard('vCard');
25+
}
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace NotificationChannels\Telegram\Tests\TestSupport;
4+
5+
use Illuminate\Notifications\Notification;
6+
use NotificationChannels\Telegram\TelegramLocation;
7+
8+
/**
9+
* Class TestLocationNotification.
10+
*/
11+
class TestLocationNotification extends Notification
12+
{
13+
public function __construct(
14+
private float|string $latitude,
15+
private float|string $longitude
16+
) {
17+
}
18+
19+
/**
20+
* @param $notifiable
21+
* @return TelegramLocation
22+
*/
23+
public function toTelegram($notifiable): TelegramLocation
24+
{
25+
return TelegramLocation::create()
26+
->to(12345)
27+
->latitude($this->latitude)
28+
->longitude($this->longitude)
29+
->options(['horizontal_accuracy' => 100]);
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace NotificationChannels\Telegram\Tests\TestSupport;
4+
5+
use Illuminate\Notifications\Notification;
6+
use NotificationChannels\Telegram\TelegramPoll;
7+
8+
/**
9+
* Class TestPollNotification.
10+
*/
11+
class TestPollNotification extends Notification
12+
{
13+
/**
14+
* @param $notifiable
15+
* @return TelegramPoll
16+
*/
17+
public function toTelegram($notifiable): TelegramPoll
18+
{
19+
return TelegramPoll::create()
20+
->to(12345)
21+
->question("Isn't Telegram Notification Channel Awesome?")
22+
->choices(['Yes', 'No']);
23+
}
24+
}

0 commit comments

Comments
 (0)