Skip to content

Commit

Permalink
[3.x] Switch to sms() Client, improved GSM-7 Handling (#72)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
SecondeJK authored Feb 3, 2023
1 parent 1427790 commit cd789f1
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 78 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"php": "^8.0",
"illuminate/notifications": "^8.0|^9.0|^10.0",
"illuminate/support": "^8.0|^9.0|^10.0",
"vonage/client-core": "^3.0"
"vonage/client-core": "^4.0.4"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.2",
Expand Down
22 changes: 12 additions & 10 deletions src/Channels/VonageSmsChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Notifications\Messages\VonageMessage;
use Illuminate\Notifications\Notification;
use Vonage\Client as VonageClient;
use Vonage\SMS\Message\SMS;

class VonageSmsChannel
{
Expand Down Expand Up @@ -40,7 +41,7 @@ public function __construct(VonageClient $client, $from)
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return \Vonage\Message\Message
* @return \Vonage\SMS\Collection|null
*/
public function send($notifiable, Notification $notification)
{
Expand All @@ -54,18 +55,19 @@ public function send($notifiable, Notification $notification)
$message = new VonageMessage($message);
}

$payload = [
'type' => $message->type,
'from' => $message->from ?: $this->from,
'to' => $to,
'text' => trim($message->content),
'client-ref' => $message->clientReference,
];
$vonageSms = new SMS(
$to,
$message->from ?: $this->from,
trim($message->content),
$message->type
);

$vonageSms->setClientRef($message->clientReference);

if ($message->statusCallback) {
$payload['callback'] = $message->statusCallback;
$vonageSms->setDeliveryReceiptCallback($message->statusCallback);
}

return ($message->client ?? $this->client)->message()->send($payload);
return ($message->client ?? $this->client)->sms()->send($vonageSms);
}
}
171 changes: 104 additions & 67 deletions tests/Unit/Channels/VonageSmsChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Notifications\Tests\Unit\Channels;

use Hamcrest\Core\IsEqual;
use Illuminate\Notifications\Channels\VonageSmsChannel;
use Illuminate\Notifications\Messages\VonageMessage;
use Illuminate\Notifications\Notifiable;
Expand All @@ -10,6 +11,7 @@
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Vonage\Client;
use Vonage\SMS\Message\SMS;

class VonageSmsChannelTest extends TestCase
{
Expand All @@ -24,30 +26,52 @@ public function testSmsIsSentViaVonage()
$vonage = m::mock(Client::class), '4444444444'
);

$vonage->shouldReceive('message->send')
->with([
'type' => 'text',
'from' => '4444444444',
'to' => '5555555555',
'text' => 'this is my message',
'client-ref' => '',
])
$mockSms = (new SMS(
'5555555555',
'4444444444',
'this is my message',
'text'
));

$vonage->shouldReceive('sms->send')
->with(IsEqual::equalTo($mockSms))
->once();

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

public function testSmsWillSendAsUnicode()
{
$notification = new NotificationVonageUnicodeSmsChannelTestNotification;
$notifiable = new NotificationVonageSmsChannelTestNotifiable;

$channel = new VonageSmsChannel(
$vonage = m::mock(Client::class), '4444444444'
);

$mockSms = (new SMS(
'5555555555',
'4444444444',
'this is my message',
'unicode'
));

$vonage->shouldReceive('sms->send')
->with(IsEqual::equalTo($mockSms))
->once();

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

public function testSmsIsSentViaVonageWithCustomClient()
{
$customVonage = m::mock(Client::class);
$customVonage->shouldReceive('message->send')
->with([
'type' => 'text',
'from' => '4444444444',
'to' => '5555555555',
'text' => 'this is my message',
'client-ref' => '',
])
$customVonage->shouldReceive('sms->send')
->with(IsEqual::equalTo(new SMS(
'5555555555',
'4444444444',
'this is my message'
)))
->once();

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

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

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

$vonage->shouldReceive('message->send')
->with([
'type' => 'unicode',
'from' => '5554443333',
'to' => '5555555555',
'text' => 'this is my message',
'client-ref' => '',
])
$mockSms = (new SMS(
'5555555555',
'5554443333',
'this is my message'
));

$vonage->shouldReceive('sms->send')
->with(IsEqual::equalTo($mockSms))
->once();

$channel->send($notifiable, $notification);
Expand All @@ -87,14 +111,15 @@ public function testSmsIsSentViaVonageWithCustomFrom()
public function testSmsIsSentViaVonageWithCustomFromAndClient()
{
$customVonage = m::mock(Client::class);
$customVonage->shouldReceive('message->send')
->with([
'type' => 'unicode',
'from' => '5554443333',
'to' => '5555555555',
'text' => 'this is my message',
'client-ref' => '',
])

$mockSms = new SMS(
'5555555555',
'5554443333',
'this is my message',
);

$customVonage->shouldReceive('sms->send')
->with(IsEqual::equalTo($mockSms))
->once();

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

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

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

$vonage->shouldReceive('message->send')
->with([
'type' => 'unicode',
'from' => '5554443333',
'to' => '5555555555',
'text' => 'this is my message',
'client-ref' => '11',
])
$mockSms = new SMS(
'5555555555',
'5554443333',
'this is my message',
);

$mockSms->setClientRef('11');

$vonage->shouldReceive('sms->send')
->with(IsEqual::equalTo($mockSms))
->once();

$channel->send($notifiable, $notification);
Expand All @@ -134,14 +161,17 @@ public function testSmsIsSentViaVonageWithCustomFromAndClientRef()
public function testSmsIsSentViaVonageWithCustomClientFromAndClientRef()
{
$customVonage = m::mock(Client::class);
$customVonage->shouldReceive('message->send')
->with([
'type' => 'unicode',
'from' => '5554443333',
'to' => '5555555555',
'text' => 'this is my message',
'client-ref' => '11',
])

$mockSms = new SMS(
'5555555555',
'5554443333',
'this is my message',
);

$mockSms->setClientRef('11');

$customVonage->shouldReceive('sms->send')
->with(IsEqual::equalTo($mockSms))
->once();

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

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

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

$vonage->shouldReceive('message->send')
->with([
'type' => 'text',
'from' => '4444444444',
'to' => '5555555555',
'text' => 'this is my message',
'client-ref' => '',
'callback' => 'https://example.com',
])
->once();
$mockSms = (new SMS(
'5555555555',
'4444444444',
'this is my message'
));

$mockSms->setDeliveryReceiptCallback('https://example.com');

$vonage->shouldReceive('sms->send')
->with(IsEqual::equalTo($mockSms))
->once();

$channel->send($notifiable, $notification);
}
Expand All @@ -200,6 +231,14 @@ public function toVonage($notifiable)
}
}

class NotificationVonageUnicodeSmsChannelTestNotification extends Notification
{
public function toVonage($notifiable)
{
return (new VonageMessage('this is my message'))->unicode();
}
}

class NotificationVonageSmsChannelTestCustomClientNotification extends Notification
{
private $client;
Expand All @@ -219,7 +258,7 @@ class NotificationVonageSmsChannelTestCustomFromNotification extends Notificatio
{
public function toVonage($notifiable)
{
return (new VonageMessage('this is my message'))->from('5554443333')->unicode();
return (new VonageMessage('this is my message'))->from('5554443333');
}
}

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

public function toVonage($notifiable)
{
return (new VonageMessage('this is my message'))->from('5554443333')->unicode()->usingClient($this->client);
return (new VonageMessage('this is my message'))->from('5554443333')->usingClient($this->client);
}
}

class NotificationVonageSmsChannelTestCustomFromAndClientRefNotification extends Notification
{
public function toVonage($notifiable)
{
return (new VonageMessage('this is my message'))->from('5554443333')->unicode()->clientReference('11');
return (new VonageMessage('this is my message'))->from('5554443333')->clientReference('11');
}
}

Expand All @@ -259,7 +298,6 @@ public function toVonage($notifiable)
{
return (new VonageMessage('this is my message'))
->from('5554443333')
->unicode()
->clientReference('11')
->usingClient($this->client);
}
Expand All @@ -269,7 +307,6 @@ class NotificationVonageSmsChannelTestCallback extends Notification
{
public function toVonage($notifiable)
{
return (new VonageMessage('this is my message'))
->statusCallback('https://example.com');
return (new VonageMessage('this is my message'))->statusCallback('https://example.com');
}
}

0 comments on commit cd789f1

Please sign in to comment.