Skip to content

Commit cd789f1

Browse files
authored
[3.x] Switch to sms() Client, improved GSM-7 Handling (#72)
* Changed the SMS channel to use the SMS client instead of the legacy message client. * Fixed/modified tests to reflect new logic * Rehashed PR to use new SMS Client, bring in v4 of the core library to make GSM-7 default. * core version bump to squash bug
1 parent 1427790 commit cd789f1

File tree

3 files changed

+117
-78
lines changed

3 files changed

+117
-78
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"php": "^8.0",
1414
"illuminate/notifications": "^8.0|^9.0|^10.0",
1515
"illuminate/support": "^8.0|^9.0|^10.0",
16-
"vonage/client-core": "^3.0"
16+
"vonage/client-core": "^4.0.4"
1717
},
1818
"require-dev": {
1919
"guzzlehttp/guzzle": "^7.2",

src/Channels/VonageSmsChannel.php

Lines changed: 12 additions & 10 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
{
@@ -40,7 +41,7 @@ public function __construct(VonageClient $client, $from)
4041
*
4142
* @param mixed $notifiable
4243
* @param \Illuminate\Notifications\Notification $notification
43-
* @return \Vonage\Message\Message
44+
* @return \Vonage\SMS\Collection|null
4445
*/
4546
public function send($notifiable, Notification $notification)
4647
{
@@ -54,18 +55,19 @@ 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+
$message->type
63+
);
64+
65+
$vonageSms->setClientRef($message->clientReference);
6466

6567
if ($message->statusCallback) {
66-
$payload['callback'] = $message->statusCallback;
68+
$vonageSms->setDeliveryReceiptCallback($message->statusCallback);
6769
}
6870

69-
return ($message->client ?? $this->client)->message()->send($payload);
71+
return ($message->client ?? $this->client)->sms()->send($vonageSms);
7072
}
7173
}

tests/Unit/Channels/VonageSmsChannelTest.php

Lines changed: 104 additions & 67 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,30 +26,52 @@ 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+
'text'
34+
));
35+
36+
$vonage->shouldReceive('sms->send')
37+
->with(IsEqual::equalTo($mockSms))
3538
->once();
3639

3740
$channel->send($notifiable, $notification);
3841
}
3942

43+
public function testSmsWillSendAsUnicode()
44+
{
45+
$notification = new NotificationVonageUnicodeSmsChannelTestNotification;
46+
$notifiable = new NotificationVonageSmsChannelTestNotifiable;
47+
48+
$channel = new VonageSmsChannel(
49+
$vonage = m::mock(Client::class), '4444444444'
50+
);
51+
52+
$mockSms = (new SMS(
53+
'5555555555',
54+
'4444444444',
55+
'this is my message',
56+
'unicode'
57+
));
58+
59+
$vonage->shouldReceive('sms->send')
60+
->with(IsEqual::equalTo($mockSms))
61+
->once();
62+
63+
$channel->send($notifiable, $notification);
64+
}
65+
4066
public function testSmsIsSentViaVonageWithCustomClient()
4167
{
4268
$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-
])
69+
$customVonage->shouldReceive('sms->send')
70+
->with(IsEqual::equalTo(new SMS(
71+
'5555555555',
72+
'4444444444',
73+
'this is my message'
74+
)))
5175
->once();
5276

5377
$notification = new NotificationVonageSmsChannelTestCustomClientNotification($customVonage);
@@ -57,7 +81,7 @@ public function testSmsIsSentViaVonageWithCustomClient()
5781
$vonage = m::mock(Client::class), '4444444444'
5882
);
5983

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

6286
$channel->send($notifiable, $notification);
6387
}
@@ -71,14 +95,14 @@ public function testSmsIsSentViaVonageWithCustomFrom()
7195
$vonage = m::mock(Client::class), '4444444444'
7296
);
7397

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-
])
98+
$mockSms = (new SMS(
99+
'5555555555',
100+
'5554443333',
101+
'this is my message'
102+
));
103+
104+
$vonage->shouldReceive('sms->send')
105+
->with(IsEqual::equalTo($mockSms))
82106
->once();
83107

84108
$channel->send($notifiable, $notification);
@@ -87,14 +111,15 @@ public function testSmsIsSentViaVonageWithCustomFrom()
87111
public function testSmsIsSentViaVonageWithCustomFromAndClient()
88112
{
89113
$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-
])
114+
115+
$mockSms = new SMS(
116+
'5555555555',
117+
'5554443333',
118+
'this is my message',
119+
);
120+
121+
$customVonage->shouldReceive('sms->send')
122+
->with(IsEqual::equalTo($mockSms))
98123
->once();
99124

100125
$notification = new NotificationVonageSmsChannelTestCustomFromAndClientNotification($customVonage);
@@ -104,7 +129,7 @@ public function testSmsIsSentViaVonageWithCustomFromAndClient()
104129
$vonage = m::mock(Client::class), '4444444444'
105130
);
106131

107-
$vonage->shouldNotReceive('message->send');
132+
$vonage->shouldNotReceive('sms->send');
108133

109134
$channel->send($notifiable, $notification);
110135
}
@@ -118,14 +143,16 @@ public function testSmsIsSentViaVonageWithCustomFromAndClientRef()
118143
$vonage = m::mock(Client::class), '4444444444'
119144
);
120145

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-
])
146+
$mockSms = new SMS(
147+
'5555555555',
148+
'5554443333',
149+
'this is my message',
150+
);
151+
152+
$mockSms->setClientRef('11');
153+
154+
$vonage->shouldReceive('sms->send')
155+
->with(IsEqual::equalTo($mockSms))
129156
->once();
130157

131158
$channel->send($notifiable, $notification);
@@ -134,14 +161,17 @@ public function testSmsIsSentViaVonageWithCustomFromAndClientRef()
134161
public function testSmsIsSentViaVonageWithCustomClientFromAndClientRef()
135162
{
136163
$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-
])
164+
165+
$mockSms = new SMS(
166+
'5555555555',
167+
'5554443333',
168+
'this is my message',
169+
);
170+
171+
$mockSms->setClientRef('11');
172+
173+
$customVonage->shouldReceive('sms->send')
174+
->with(IsEqual::equalTo($mockSms))
145175
->once();
146176

147177
$notification = new NotificationVonageSmsChannelTestCustomClientFromAndClientRefNotification($customVonage);
@@ -151,7 +181,7 @@ public function testSmsIsSentViaVonageWithCustomClientFromAndClientRef()
151181
$vonage = m::mock(Client::class), '4444444444'
152182
);
153183

154-
$vonage->shouldNotReceive('message->send');
184+
$vonage->shouldNotReceive('sms->send');
155185

156186
$channel->send($notifiable, $notification);
157187
}
@@ -165,16 +195,17 @@ public function testCallbackIsApplied()
165195
$vonage = m::mock(Client::class), '4444444444'
166196
);
167197

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();
198+
$mockSms = (new SMS(
199+
'5555555555',
200+
'4444444444',
201+
'this is my message'
202+
));
203+
204+
$mockSms->setDeliveryReceiptCallback('https://example.com');
205+
206+
$vonage->shouldReceive('sms->send')
207+
->with(IsEqual::equalTo($mockSms))
208+
->once();
178209

179210
$channel->send($notifiable, $notification);
180211
}
@@ -200,6 +231,14 @@ public function toVonage($notifiable)
200231
}
201232
}
202233

234+
class NotificationVonageUnicodeSmsChannelTestNotification extends Notification
235+
{
236+
public function toVonage($notifiable)
237+
{
238+
return (new VonageMessage('this is my message'))->unicode();
239+
}
240+
}
241+
203242
class NotificationVonageSmsChannelTestCustomClientNotification extends Notification
204243
{
205244
private $client;
@@ -219,7 +258,7 @@ class NotificationVonageSmsChannelTestCustomFromNotification extends Notificatio
219258
{
220259
public function toVonage($notifiable)
221260
{
222-
return (new VonageMessage('this is my message'))->from('5554443333')->unicode();
261+
return (new VonageMessage('this is my message'))->from('5554443333');
223262
}
224263
}
225264

@@ -234,15 +273,15 @@ public function __construct(Client $client)
234273

235274
public function toVonage($notifiable)
236275
{
237-
return (new VonageMessage('this is my message'))->from('5554443333')->unicode()->usingClient($this->client);
276+
return (new VonageMessage('this is my message'))->from('5554443333')->usingClient($this->client);
238277
}
239278
}
240279

241280
class NotificationVonageSmsChannelTestCustomFromAndClientRefNotification extends Notification
242281
{
243282
public function toVonage($notifiable)
244283
{
245-
return (new VonageMessage('this is my message'))->from('5554443333')->unicode()->clientReference('11');
284+
return (new VonageMessage('this is my message'))->from('5554443333')->clientReference('11');
246285
}
247286
}
248287

@@ -259,7 +298,6 @@ public function toVonage($notifiable)
259298
{
260299
return (new VonageMessage('this is my message'))
261300
->from('5554443333')
262-
->unicode()
263301
->clientReference('11')
264302
->usingClient($this->client);
265303
}
@@ -269,7 +307,6 @@ class NotificationVonageSmsChannelTestCallback extends Notification
269307
{
270308
public function toVonage($notifiable)
271309
{
272-
return (new VonageMessage('this is my message'))
273-
->statusCallback('https://example.com');
310+
return (new VonageMessage('this is my message'))->statusCallback('https://example.com');
274311
}
275312
}

0 commit comments

Comments
 (0)