Skip to content

Commit

Permalink
feat: add support for debug to
Browse files Browse the repository at this point in the history
  • Loading branch information
atymic committed Apr 12, 2020
1 parent 4a12b17 commit 786b95e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
8 changes: 8 additions & 0 deletions config/twilio-notification-channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@
'from' => env('TWILIO_FROM'), // optional
'alphanumeric_sender' => env('TWILIO_ALPHA_SENDER'),

/**
* Specify a number where all calls/messages should be routed. This can be used in development/staging environments
* for testing.
*/
'debug_to' => env('TWILIO_DEBUG_TO'),

/**
* If an exception is thrown with one of these error codes, it will be caught & suppressed.
* To replicate the 2.x behaviour, specify '*' in the array, which will cause all exceptions to be suppressed.
* Suppressed errors will still be logged.
*
* @see https://www.twilio.com/docs/api/errors
*/
Expand Down
14 changes: 10 additions & 4 deletions src/Twilio.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function __construct(TwilioService $twilioService, TwilioConfig $config)
* Send a TwilioMessage to the a phone number.
*
* @param TwilioMessage $message
* @param string|null $to
* @param bool $useAlphanumericSender
* @param string|null $to
* @param bool $useAlphanumericSender
*
* @return mixed
* @throws TwilioException
Expand Down Expand Up @@ -62,14 +62,20 @@ public function sendMessage(TwilioMessage $message, ?string $to, bool $useAlphan
* Send an sms message using the Twilio Service.
*
* @param TwilioSmsMessage $message
* @param string|null $to
* @param string|null $to
*
* @return MessageInstance
* @throws CouldNotSendNotification
* @throws TwilioException
*/
protected function sendSmsMessage(TwilioSmsMessage $message, ?string $to): MessageInstance
{
$debugTo = $this->config->getDebugTo();

if ($debugTo !== null) {
$to = $debugTo;
}

$params = [
'body' => trim($message->content),
];
Expand Down Expand Up @@ -109,7 +115,7 @@ protected function sendSmsMessage(TwilioSmsMessage $message, ?string $to): Messa
* Make a call using the Twilio Service.
*
* @param TwilioCallMessage $message
* @param string|null $to
* @param string|null $to
*
* @return CallInstance
* @throws TwilioException
Expand Down
9 changes: 9 additions & 0 deletions src/TwilioConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,22 @@ public function getServiceSid(): ?string
return $this->config['sms_service_sid'] ?? null;
}

public function getDebugTo(): ?string
{
return $this->config['debug_to'] ?? null;
}

public function getIgnoredErrorCodes(): array
{
return $this->config['ignored_error_codes'] ?? [];
}

public function isIgnoredErrorCode(int $code): bool
{
if (in_array('*', $this->getIgnoredErrorCodes(), true)) {
return true;
}

return in_array($code, $this->getIgnoredErrorCodes(), true);
}
}
62 changes: 62 additions & 0 deletions tests/TwilioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public function it_can_send_a_sms_message_to_twilio()
->once()
->andReturn('+1234567890');

$this->config->shouldReceive('getDebugTo')
->once()
->andReturn(null);

$this->config->shouldReceive('getServiceSid')
->once()
->andReturn(null);
Expand Down Expand Up @@ -104,6 +108,10 @@ public function it_can_send_a_mms_message_to_twilio()
->once()
->andReturn(null);

$this->config->shouldReceive('getDebugTo')
->once()
->andReturn(null);

$this->twilioService->messages->shouldReceive('create')
->atLeast()->once()
->with('+1111111111', [
Expand Down Expand Up @@ -137,6 +145,10 @@ public function it_can_send_a_sms_message_to_twilio_with_alphanumeric_sender()
->once()
->andReturn(null);

$this->config->shouldReceive('getDebugTo')
->once()
->andReturn(null);

$this->twilioService->messages->shouldReceive('create')
->atLeast()->once()
->with('+1111111111', [
Expand All @@ -161,6 +173,10 @@ public function it_can_send_a_sms_message_to_twilio_with_messaging_service()
->once()
->andReturn('service_sid');

$this->config->shouldReceive('getDebugTo')
->once()
->andReturn(null);

$this->twilioService->messages->shouldReceive('create')
->atLeast()->once()
->with('+1111111111', [
Expand Down Expand Up @@ -209,6 +225,10 @@ public function it_will_throw_an_exception_in_case_of_a_missing_from_number()

$smsMessage = new TwilioSmsMessage('Message text');

$this->config->shouldReceive('getDebugTo')
->once()
->andReturn(null);

$this->config->shouldReceive('getFrom')
->once()
->andReturn(null);
Expand All @@ -228,6 +248,48 @@ public function it_will_throw_an_exception_in_case_of_an_unrecognized_message_ob

$this->twilio->sendMessage(new InvalidMessage(), null);
}

/** @test */
public function it_should_use_universal_to()
{
$debugTo = '+1222222222';

$message = new TwilioSmsMessage('Message text');
$message->statusCallback('http://example.com');
$message->statusCallbackMethod('PUT');
$message->applicationSid('ABCD1234');
$message->maxPrice(0.05);
$message->provideFeedback(true);
$message->validityPeriod(120);

$this->config->shouldReceive('getFrom')
->once()
->andReturn('+1234567890');

$this->config->shouldReceive('getDebugTo')
->once()
->andReturn($debugTo);

$this->config->shouldReceive('getServiceSid')
->once()
->andReturn(null);

$this->twilioService->messages->shouldReceive('create')
->atLeast()->once()
->with($debugTo, [
'from' => '+1234567890',
'body' => 'Message text',
'statusCallback' => 'http://example.com',
'statusCallbackMethod' => 'PUT',
'applicationSid' => 'ABCD1234',
'maxPrice' => 0.05,
'provideFeedback' => true,
'validityPeriod' => 120,
])
->andReturn(Mockery::mock(MessageInstance::class));

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

class InvalidMessage extends TwilioMessage
Expand Down

0 comments on commit 786b95e

Please sign in to comment.