From 786b95e2a36156853669a8d0399c4222ac533205 Mon Sep 17 00:00:00 2001 From: atymic Date: Sun, 12 Apr 2020 12:12:54 +1000 Subject: [PATCH] feat: add support for debug to --- config/twilio-notification-channel.php | 8 ++++ src/Twilio.php | 14 ++++-- src/TwilioConfig.php | 9 ++++ tests/TwilioTest.php | 62 ++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 4 deletions(-) diff --git a/config/twilio-notification-channel.php b/config/twilio-notification-channel.php index 9bafade..277541d 100644 --- a/config/twilio-notification-channel.php +++ b/config/twilio-notification-channel.php @@ -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 */ diff --git a/src/Twilio.php b/src/Twilio.php index c1bb22e..8411960 100644 --- a/src/Twilio.php +++ b/src/Twilio.php @@ -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 @@ -62,7 +62,7 @@ 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 @@ -70,6 +70,12 @@ public function sendMessage(TwilioMessage $message, ?string $to, bool $useAlphan */ protected function sendSmsMessage(TwilioSmsMessage $message, ?string $to): MessageInstance { + $debugTo = $this->config->getDebugTo(); + + if ($debugTo !== null) { + $to = $debugTo; + } + $params = [ 'body' => trim($message->content), ]; @@ -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 diff --git a/src/TwilioConfig.php b/src/TwilioConfig.php index 52a4378..5334258 100644 --- a/src/TwilioConfig.php +++ b/src/TwilioConfig.php @@ -60,6 +60,11 @@ 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'] ?? []; @@ -67,6 +72,10 @@ public function getIgnoredErrorCodes(): array public function isIgnoredErrorCode(int $code): bool { + if (in_array('*', $this->getIgnoredErrorCodes(), true)) { + return true; + } + return in_array($code, $this->getIgnoredErrorCodes(), true); } } diff --git a/tests/TwilioTest.php b/tests/TwilioTest.php index 399ac0a..d4a7fa9 100644 --- a/tests/TwilioTest.php +++ b/tests/TwilioTest.php @@ -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); @@ -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', [ @@ -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', [ @@ -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', [ @@ -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); @@ -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