From 4e36ff09b3eefb365b316dfd51be71b8b0faf1d0 Mon Sep 17 00:00:00 2001 From: Robert Lester Date: Thu, 20 Jul 2023 11:33:30 -0400 Subject: [PATCH] add faker --- src/Support/FakeFacade.php | 198 +++++++++++++++++++++++++++++++++ src/Support/Laravel/Facade.php | 8 ++ 2 files changed, 206 insertions(+) create mode 100644 src/Support/FakeFacade.php diff --git a/src/Support/FakeFacade.php b/src/Support/FakeFacade.php new file mode 100644 index 0000000..5ccba42 --- /dev/null +++ b/src/Support/FakeFacade.php @@ -0,0 +1,198 @@ + [ + 'sid' => 'sid', + 'token' => 'token', + 'from' => '+18005551234', + ] + ]; + + /** + * @param string $token + * @param string $from + * @param string $sid + * @param bool $sslVerify + */ + + public function __construct(Twilio $instance) + { + $this->instance = $instance; + $this->history = collect([]); + } + + public function instance(string $sid, string $token, string $from, bool $sslVerify = true) + { + $this->sid = $sid; + $this->token = $token; + $this->from = $from; + $this->sslVerify = $sslVerify; + + return $this; + } + + /** + * @param string $to + * @param string $message + * @param array $mediaUrls + * @param array $params + * + * @see https://www.twilio.com/docs/api/messaging/send-messages Documentation + * + * @throws ConfigurationException + * @throws TwilioException + * + * @return MessageInstance + */ + public function message(string $to, string $message, array $mediaUrls = [], array $params = []): MessageInstance + { + $params['body'] = $message; + + if (!isset($params['from'])) { + $params['from'] = $this->from; + } + + if (!empty($mediaUrls)) { + $params['mediaUrl'] = $mediaUrls; + } + + $this->history["sms_$to"] = new MessageInstance(new V2010(new Api(new Client('nonsense', 'nonsense'))), [], ''); + + return $this->history["sms_$to"]; + } + + /** + * @param string $to + * @param callable|string|TwiML $message + * @param array $params + * + * @throws TwilioException + * + * @see https://www.twilio.com/docs/api/voice/making-calls Documentation + * + * @return CallInstance + */ + public function call(string $to, $message, array $params = []): CallInstance + { + if (is_callable($message)) { + $message = $this->twiml($message); + } + + if ($message instanceof TwiML) { + $params['twiml'] = (string) $message; + } else { + $params['url'] = $message; + } + + $this->history["call_$to"] = new CallInstance(new V2010(new Api(new Client('nonsense', 'nonsense'))), [], ''); + + return $this->history["call_$to"]; + } + + /** + * @throws ConfigurationException + * + * @return Client + */ + public function getTwilio(): Client + { + if ($this->twilio) { + return $this->twilio; + } + + return $this->twilio = new Client($this->sid, $this->token); + } + + /** + * @param callable $callback + * + * @return TwiML + */ + private function twiml(callable $callback): TwiML + { + $message = new VoiceResponse(); + + call_user_func($callback, $message); + + return $message; + } + + /** + * @return TwilioInterface + */ + public function defaultConnection(): TwilioInterface + { + return $this->from('default'); + } + + /** + * @param string $connection + * + * @return TwilioInterface + */ + public function from(string $connection): TwilioInterface + { + if (!isset($this->settings[$connection])) { + throw new InvalidArgumentException("Connection \"{$connection}\" is not configured."); + } + + $settings = $this->settings[$connection]; + + return $this->instance($settings['sid'], $settings['token'], $settings['from']); + } + + public function assertMessageSent(string $to) + { + Assert::assertInstanceOf(MessageInstance::class, $this->history["sms_$to"] ?? null); + } + + public function assertCallSent(string $to) + { + Assert::assertInstanceOf(CallInstance::class, $this->history["call_$to"] ?? null); + } +} diff --git a/src/Support/Laravel/Facade.php b/src/Support/Laravel/Facade.php index 38605eb..e26d0ab 100644 --- a/src/Support/Laravel/Facade.php +++ b/src/Support/Laravel/Facade.php @@ -3,6 +3,8 @@ namespace Aloha\Twilio\Support\Laravel; use Illuminate\Support\Facades\Facade as BaseFacade; +use Aloha\Twilio\Support\FakeFacade; +use Aloha\Twilio\Dummy; class Facade extends BaseFacade { @@ -15,4 +17,10 @@ protected static function getFacadeAccessor(): string { return 'twilio'; } + + public static function fake($jobsToFake = []) + { + static::swap($fake = new FakeFacade(static::getFacadeRoot())); + return $fake; + } }