Skip to content

Commit 55c703f

Browse files
authored
Swap Vonage Client to SMS instead of legacy message (#65)
* Changed the SMS channel to use the SMS client instead of the legacy message client. * Fixed/modified tests to reflect new logic
1 parent 0ca6688 commit 55c703f

File tree

2 files changed

+82
-72
lines changed

2 files changed

+82
-72
lines changed

src/Channels/VonageSmsChannel.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Notifications\Messages\VonageMessage;
66
use Illuminate\Notifications\Notification;
77
use Vonage\Client as VonageClient;
8+
use Vonage\SMS\Message\SMS;
89

910
class VonageSmsChannel
1011
{
@@ -54,18 +55,18 @@ public function send($notifiable, Notification $notification)
5455
$message = new VonageMessage($message);
5556
}
5657

57-
$payload = [
58-
'type' => $message->type,
59-
'from' => $message->from ?: $this->from,
60-
'to' => $to,
61-
'text' => trim($message->content),
62-
'client-ref' => $message->clientReference,
63-
];
58+
$vonageSms = new SMS(
59+
$to,
60+
$message->from ?: $this->from,
61+
trim($message->content)
62+
);
63+
64+
$vonageSms->setClientRef($message->clientReference);
6465

6566
if ($message->statusCallback) {
66-
$payload['callback'] = $message->statusCallback;
67+
$vonageSms->setDeliveryReceiptCallback($message->statusCallback);
6768
}
6869

69-
return ($message->client ?? $this->client)->message()->send($payload);
70+
return ($message->client ?? $this->client)->sms()->send($vonageSms);
7071
}
7172
}

tests/Unit/Channels/VonageSmsChannelTest.php

Lines changed: 72 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Notifications\Tests\Unit\Channels;
44

5+
use Hamcrest\Core\IsEqual;
56
use Illuminate\Notifications\Channels\VonageSmsChannel;
67
use Illuminate\Notifications\Messages\VonageMessage;
78
use Illuminate\Notifications\Notifiable;
@@ -10,6 +11,7 @@
1011
use Mockery as m;
1112
use PHPUnit\Framework\TestCase;
1213
use Vonage\Client;
14+
use Vonage\SMS\Message\SMS;
1315

1416
class VonageSmsChannelTest extends TestCase
1517
{
@@ -24,14 +26,14 @@ public function testSmsIsSentViaVonage()
2426
$vonage = m::mock(Client::class), '4444444444'
2527
);
2628

27-
$vonage->shouldReceive('message->send')
28-
->with([
29-
'type' => 'text',
30-
'from' => '4444444444',
31-
'to' => '5555555555',
32-
'text' => 'this is my message',
33-
'client-ref' => '',
34-
])
29+
$mockSms = (new SMS(
30+
'5555555555',
31+
'4444444444',
32+
'this is my message'
33+
));
34+
35+
$vonage->shouldReceive('sms->send')
36+
->with(IsEqual::equalTo($mockSms))
3537
->once();
3638

3739
$channel->send($notifiable, $notification);
@@ -40,14 +42,12 @@ public function testSmsIsSentViaVonage()
4042
public function testSmsIsSentViaVonageWithCustomClient()
4143
{
4244
$customVonage = m::mock(Client::class);
43-
$customVonage->shouldReceive('message->send')
44-
->with([
45-
'type' => 'text',
46-
'from' => '4444444444',
47-
'to' => '5555555555',
48-
'text' => 'this is my message',
49-
'client-ref' => '',
50-
])
45+
$customVonage->shouldReceive('sms->send')
46+
->with(IsEqual::equalTo(new SMS(
47+
'5555555555',
48+
'4444444444',
49+
'this is my message'
50+
)))
5151
->once();
5252

5353
$notification = new NotificationVonageSmsChannelTestCustomClientNotification($customVonage);
@@ -57,7 +57,7 @@ public function testSmsIsSentViaVonageWithCustomClient()
5757
$vonage = m::mock(Client::class), '4444444444'
5858
);
5959

60-
$vonage->shouldNotReceive('message->send');
60+
$vonage->shouldNotReceive('sms->send');
6161

6262
$channel->send($notifiable, $notification);
6363
}
@@ -71,14 +71,14 @@ public function testSmsIsSentViaVonageWithCustomFrom()
7171
$vonage = m::mock(Client::class), '4444444444'
7272
);
7373

74-
$vonage->shouldReceive('message->send')
75-
->with([
76-
'type' => 'unicode',
77-
'from' => '5554443333',
78-
'to' => '5555555555',
79-
'text' => 'this is my message',
80-
'client-ref' => '',
81-
])
74+
$mockSms = (new SMS(
75+
'5555555555',
76+
'5554443333',
77+
'this is my message'
78+
));
79+
80+
$vonage->shouldReceive('sms->send')
81+
->with(IsEqual::equalTo($mockSms))
8282
->once();
8383

8484
$channel->send($notifiable, $notification);
@@ -87,14 +87,16 @@ public function testSmsIsSentViaVonageWithCustomFrom()
8787
public function testSmsIsSentViaVonageWithCustomFromAndClient()
8888
{
8989
$customVonage = m::mock(Client::class);
90-
$customVonage->shouldReceive('message->send')
91-
->with([
92-
'type' => 'unicode',
93-
'from' => '5554443333',
94-
'to' => '5555555555',
95-
'text' => 'this is my message',
96-
'client-ref' => '',
97-
])
90+
91+
$mockSms = new SMS(
92+
'5555555555',
93+
'5554443333',
94+
'this is my message',
95+
'unicode'
96+
);
97+
98+
$customVonage->shouldReceive('sms->send')
99+
->with(IsEqual::equalTo($mockSms))
98100
->once();
99101

100102
$notification = new NotificationVonageSmsChannelTestCustomFromAndClientNotification($customVonage);
@@ -104,7 +106,7 @@ public function testSmsIsSentViaVonageWithCustomFromAndClient()
104106
$vonage = m::mock(Client::class), '4444444444'
105107
);
106108

107-
$vonage->shouldNotReceive('message->send');
109+
$vonage->shouldNotReceive('sms->send');
108110

109111
$channel->send($notifiable, $notification);
110112
}
@@ -118,14 +120,17 @@ public function testSmsIsSentViaVonageWithCustomFromAndClientRef()
118120
$vonage = m::mock(Client::class), '4444444444'
119121
);
120122

121-
$vonage->shouldReceive('message->send')
122-
->with([
123-
'type' => 'unicode',
124-
'from' => '5554443333',
125-
'to' => '5555555555',
126-
'text' => 'this is my message',
127-
'client-ref' => '11',
128-
])
123+
$mockSms = new SMS(
124+
'5555555555',
125+
'5554443333',
126+
'this is my message',
127+
'unicode'
128+
);
129+
130+
$mockSms->setClientRef('11');
131+
132+
$vonage->shouldReceive('sms->send')
133+
->with(IsEqual::equalTo($mockSms))
129134
->once();
130135

131136
$channel->send($notifiable, $notification);
@@ -134,14 +139,18 @@ public function testSmsIsSentViaVonageWithCustomFromAndClientRef()
134139
public function testSmsIsSentViaVonageWithCustomClientFromAndClientRef()
135140
{
136141
$customVonage = m::mock(Client::class);
137-
$customVonage->shouldReceive('message->send')
138-
->with([
139-
'type' => 'unicode',
140-
'from' => '5554443333',
141-
'to' => '5555555555',
142-
'text' => 'this is my message',
143-
'client-ref' => '11',
144-
])
142+
143+
$mockSms = new SMS(
144+
'5555555555',
145+
'5554443333',
146+
'this is my message',
147+
'unicode'
148+
);
149+
150+
$mockSms->setClientRef('11');
151+
152+
$customVonage->shouldReceive('sms->send')
153+
->with(IsEqual::equalTo($mockSms))
145154
->once();
146155

147156
$notification = new NotificationVonageSmsChannelTestCustomClientFromAndClientRefNotification($customVonage);
@@ -151,7 +160,7 @@ public function testSmsIsSentViaVonageWithCustomClientFromAndClientRef()
151160
$vonage = m::mock(Client::class), '4444444444'
152161
);
153162

154-
$vonage->shouldNotReceive('message->send');
163+
$vonage->shouldNotReceive('sms->send');
155164

156165
$channel->send($notifiable, $notification);
157166
}
@@ -165,16 +174,17 @@ public function testCallbackIsApplied()
165174
$vonage = m::mock(Client::class), '4444444444'
166175
);
167176

168-
$vonage->shouldReceive('message->send')
169-
->with([
170-
'type' => 'text',
171-
'from' => '4444444444',
172-
'to' => '5555555555',
173-
'text' => 'this is my message',
174-
'client-ref' => '',
175-
'callback' => 'https://example.com',
176-
])
177-
->once();
177+
$mockSms = (new SMS(
178+
'5555555555',
179+
'4444444444',
180+
'this is my message'
181+
));
182+
183+
$mockSms->setDeliveryReceiptCallback('https://example.com');
184+
185+
$vonage->shouldReceive('sms->send')
186+
->with(IsEqual::equalTo($mockSms))
187+
->once();
178188

179189
$channel->send($notifiable, $notification);
180190
}
@@ -269,7 +279,6 @@ class NotificationVonageSmsChannelTestCallback extends Notification
269279
{
270280
public function toVonage($notifiable)
271281
{
272-
return (new VonageMessage('this is my message'))
273-
->statusCallback('https://example.com');
282+
return (new VonageMessage('this is my message'))->statusCallback('https://example.com');
274283
}
275284
}

0 commit comments

Comments
 (0)