Skip to content

Commit cdeaff4

Browse files
committed
Updated to use latest Twilio sdk
1 parent b1c42b1 commit cdeaff4

File tree

6 files changed

+71
-54
lines changed

6 files changed

+71
-54
lines changed

src/Twilio.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace NotificationChannels\Twilio;
44

55
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification;
6-
use Services_Twilio as TwilioService;
6+
use Twilio\Rest\Client as TwilioService;
77

88
class Twilio
99
{
@@ -57,21 +57,24 @@ public function sendMessage(TwilioMessage $message, $to, $useAlphanumericSender
5757

5858
protected function sendSmsMessage($message, $to)
5959
{
60-
return $this->twilioService->account->messages->sendMessage(
61-
$this->getFrom($message),
62-
$to,
63-
trim($message->content),
64-
null,
65-
$this->config->getSmsParams()
66-
);
60+
$params = [
61+
'from' => $this->getFrom($message),
62+
'body' => trim($message->content),
63+
];
64+
65+
if ($serviceSid = $this->config->getServiceSid()) {
66+
$params['messagingServiceSid'] = $serviceSid;
67+
}
68+
69+
$this->twilioService->messages->create($to, $params);
6770
}
6871

6972
protected function makeCall($message, $to)
7073
{
71-
return $this->twilioService->account->calls->create(
72-
$this->getFrom($message),
74+
return $this->twilioService->calls->create(
7375
$to,
74-
trim($message->content)
76+
$this->getFrom($message),
77+
['url' => trim($message->content)]
7578
);
7679
}
7780

src/TwilioConfig.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@ public function getAlphanumericSender()
4545
return null;
4646
}
4747

48-
public function getSmsParams()
48+
public function getServiceSid()
4949
{
50-
$params = [];
51-
5250
if (isset($this->config['sms_service_sid'])) {
53-
$params['MessagingServiceSid'] = $this->config['sms_service_sid'];
51+
return $this->config['sms_service_sid'];
5452
}
5553

56-
return $params;
54+
return null;
5755
}
5856
}

src/TwilioProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace NotificationChannels\Twilio;
44

55
use Illuminate\Support\ServiceProvider;
6-
use Services_Twilio as TwilioService;
6+
use Twilio\Rest\Client as TwilioService;
77

88
class TwilioProvider extends ServiceProvider
99
{

tests/IntegrationTest.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
use Illuminate\Contracts\Events\Dispatcher;
66
use Illuminate\Notifications\Notification;
77
use Mockery;
8+
use Mockery\Adapter\Phpunit\MockeryTestCase;
89
use NotificationChannels\Twilio\TwilioCallMessage;
910
use NotificationChannels\Twilio\TwilioChannel;
1011
use NotificationChannels\Twilio\TwilioConfig;
1112
use NotificationChannels\Twilio\TwilioSmsMessage;
12-
use PHPUnit_Framework_TestCase;
1313
use NotificationChannels\Twilio\Twilio;
14-
use Services_Twilio as TwilioService;
15-
use Services_Twilio_Rest_Calls;
16-
use Services_Twilio_Rest_Messages;
14+
use Twilio\Rest\Client as TwilioService;
15+
use Twilio\Rest\Api\V2010\Account\MessageList;
16+
use Twilio\Rest\Api\V2010\Account\CallList;
1717

18-
class IntegrationTest extends PHPUnit_Framework_TestCase
18+
class IntegrationTest extends MockeryTestCase
1919
{
2020
/** @var TwilioService */
2121
protected $twilioService;
@@ -31,9 +31,8 @@ public function setUp()
3131
parent::setUp();
3232

3333
$this->twilioService = Mockery::mock(TwilioService::class);
34-
$this->twilioService->account = new \stdClass();
35-
$this->twilioService->account->messages = Mockery::mock(Services_Twilio_Rest_Messages::class);
36-
$this->twilioService->account->calls = Mockery::mock(Services_Twilio_Rest_Calls::class);
34+
$this->twilioService->messages = Mockery::mock(MessageList::class);
35+
$this->twilioService->calls = Mockery::mock(CallList::class);
3736

3837
$this->events = Mockery::mock(Dispatcher::class);
3938
$this->notification = Mockery::mock(Notification::class);
@@ -51,7 +50,10 @@ public function it_can_send_a_sms_message()
5150
$twilio = new Twilio($this->twilioService, $config);
5251
$channel = new TwilioChannel($twilio, $this->events);
5352

54-
$this->smsMessageWillBeSentToTwilioWith('+31612345678', '+22222222222', 'Message text', null, []);
53+
$this->smsMessageWillBeSentToTwilioWith('+22222222222', [
54+
'from' => '+31612345678',
55+
'body' => 'Message text'
56+
]);
5557

5658
$channel->send(new NotifiableWithAttribute(), $this->notification);
5759
}
@@ -69,8 +71,10 @@ public function it_can_send_a_sms_message_using_service()
6971
$twilio = new Twilio($this->twilioService, $config);
7072
$channel = new TwilioChannel($twilio, $this->events);
7173

72-
$this->smsMessageWillBeSentToTwilioWith('+31612345678', '+22222222222', 'Message text', null, [
73-
'MessagingServiceSid' => '0123456789'
74+
$this->smsMessageWillBeSentToTwilioWith('+22222222222', [
75+
'from' => '+31612345678',
76+
'body' => 'Message text',
77+
'messagingServiceSid' => '0123456789'
7478
]);
7579

7680
$channel->send(new NotifiableWithAttribute(), $this->notification);
@@ -89,7 +93,10 @@ public function it_can_send_a_sms_message_using_alphanumeric_sender()
8993
$twilio = new Twilio($this->twilioService, $config);
9094
$channel = new TwilioChannel($twilio, $this->events);
9195

92-
$this->smsMessageWillBeSentToTwilioWith('TwilioTest', '+33333333333', 'Message text', null, []);
96+
$this->smsMessageWillBeSentToTwilioWith('+33333333333', [
97+
'from' => 'TwilioTest',
98+
'body' => 'Message text'
99+
]);
93100

94101
$channel->send(new NotifiableWithAlphanumericSender(), $this->notification);
95102
}
@@ -106,22 +113,24 @@ public function it_can_make_a_call()
106113
$twilio = new Twilio($this->twilioService, $config);
107114
$channel = new TwilioChannel($twilio, $this->events);
108115

109-
$this->callWillBeSentToTwilioWith('+31612345678', '+22222222222', 'http://example.com');
116+
$this->callWillBeSentToTwilioWith('+22222222222', '+31612345678', [
117+
'url' => 'http://example.com'
118+
]);
110119

111120
$channel->send(new NotifiableWithAttribute(), $this->notification);
112121
}
113122

114123
protected function smsMessageWillBeSentToTwilioWith(...$args)
115124
{
116-
$this->twilioService->account->messages->shouldReceive('sendMessage')
125+
$this->twilioService->messages->shouldReceive('create')
117126
->atLeast()->once()
118127
->with(...$args)
119128
->andReturn(true);
120129
}
121130

122131
protected function callWillBeSentToTwilioWith(...$args)
123132
{
124-
$this->twilioService->account->calls->shouldReceive('create')
133+
$this->twilioService->calls->shouldReceive('create')
125134
->atLeast()->once()
126135
->with(...$args)
127136
->andReturn(true);

tests/TwilioProviderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use NotificationChannels\Twilio\TwilioChannel;
99
use NotificationChannels\Twilio\TwilioConfig;
1010
use NotificationChannels\Twilio\TwilioProvider;
11-
use Services_Twilio as TwilioService;
11+
use Twilio\Rest\Client as TwilioService;
1212
use NotificationChannels\Twilio\Twilio;
1313
use ArrayAccess;
1414

tests/TwilioTest.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use NotificationChannels\Twilio\Twilio;
1414
use Services_Twilio_Rest_Calls;
1515
use Services_Twilio_Rest_Messages;
16-
use Services_Twilio as TwilioService;
16+
use Twilio\Rest\Client as TwilioService;
1717

1818
class TwilioTest extends MockeryTestCase
1919
{
@@ -39,9 +39,8 @@ public function setUp()
3939
$this->dispatcher = Mockery::mock(Dispatcher::class);
4040
$this->config = Mockery::mock(TwilioConfig::class);
4141

42-
$this->twilioService->account = new \stdClass();
43-
$this->twilioService->account->messages = Mockery::mock(Services_Twilio_Rest_Messages::class);
44-
$this->twilioService->account->calls = Mockery::mock(Services_Twilio_Rest_Calls::class);
42+
$this->twilioService->messages = Mockery::mock(Services_Twilio_Rest_Messages::class);
43+
$this->twilioService->calls = Mockery::mock(Services_Twilio_Rest_Calls::class);
4544

4645
$this->twilio = new Twilio($this->twilioService, $this->config);
4746
}
@@ -55,13 +54,16 @@ public function it_can_send_a_sms_message_to_twilio()
5554
->once()
5655
->andReturn('+1234567890');
5756

58-
$this->config->shouldReceive('getSmsParams')
57+
$this->config->shouldReceive('getServiceSid')
5958
->once()
60-
->andReturn([]);
59+
->andReturn(null);
6160

62-
$this->twilioService->account->messages->shouldReceive('sendMessage')
61+
$this->twilioService->messages->shouldReceive('create')
6362
->atLeast()->once()
64-
->with('+1234567890', '+1111111111', 'Message text', null, [])
63+
->with('+1111111111', [
64+
'from' => '+1234567890',
65+
'body' => 'Message text',
66+
])
6567
->andReturn(true);
6668

6769
$this->twilio->sendMessage($message, '+1111111111');
@@ -78,13 +80,16 @@ public function it_can_send_a_sms_message_to_twilio_with_alphanumeric_sender()
7880

7981
$this->config->shouldNotReceive('getFrom');
8082

81-
$this->config->shouldReceive('getSmsParams')
83+
$this->config->shouldReceive('getServiceSid')
8284
->once()
83-
->andReturn([]);
85+
->andReturn(null);
8486

85-
$this->twilioService->account->messages->shouldReceive('sendMessage')
87+
$this->twilioService->messages->shouldReceive('create')
8688
->atLeast()->once()
87-
->with('TwilioTest', '+1111111111', 'Message text', null, [])
89+
->with('+1111111111', [
90+
'from' => 'TwilioTest',
91+
'body' => 'Message text'
92+
])
8893
->andReturn(true);
8994

9095
$this->twilio->sendMessage($message, '+1111111111', true);
@@ -99,16 +104,16 @@ public function it_can_send_a_sms_message_to_twilio_with_messaging_service()
99104
->once()
100105
->andReturn('+1234567890');
101106

102-
$this->config->shouldReceive('getSmsParams')
107+
$this->config->shouldReceive('getServiceSid')
103108
->once()
104-
->andReturn([
105-
'MessagingServiceSid' => 'service_sid'
106-
]);
109+
->andReturn('service_sid');
107110

108-
$this->twilioService->account->messages->shouldReceive('sendMessage')
111+
$this->twilioService->messages->shouldReceive('create')
109112
->atLeast()->once()
110-
->with('+1234567890', '+1111111111', 'Message text', null, [
111-
'MessagingServiceSid' => 'service_sid'
113+
->with('+1111111111', [
114+
'from' => '+1234567890',
115+
'body' => 'Message text',
116+
'messagingServiceSid' => 'service_sid'
112117
])
113118
->andReturn(true);
114119

@@ -121,9 +126,11 @@ public function it_can_send_a_call_to_twilio()
121126
$message = new TwilioCallMessage('http://example.com');
122127
$message->from = '+2222222222';
123128

124-
$this->twilioService->account->calls->shouldReceive('create')
129+
$this->twilioService->calls->shouldReceive('create')
125130
->atLeast()->once()
126-
->with('+2222222222', '+1111111111', 'http://example.com')
131+
->with('+1111111111', '+2222222222', [
132+
'url' => 'http://example.com'
133+
])
127134
->andReturn(true);
128135

129136
$this->twilio->sendMessage($message, '+1111111111');

0 commit comments

Comments
 (0)