From d9a48b66b3851d0527022cce4dc57b9912ea584a Mon Sep 17 00:00:00 2001 From: Falcon Ho Date: Fri, 21 Jan 2022 22:04:21 +0800 Subject: [PATCH] test(service): add prepare --- src/Services/Every8d.php | 40 +++++++++----- src/Services/Infobip.php | 41 +++++++++----- src/Services/Kotsms.php | 44 +++++++++------ src/Services/Mitake.php | 37 +++++++++---- src/TaiwanSms.php | 10 ++-- tests/Feature/TaiwanSmsTest.php | 94 +++++++++++++++++++++++++++++++++ 6 files changed, 212 insertions(+), 54 deletions(-) diff --git a/src/Services/Every8d.php b/src/Services/Every8d.php index f35faf7..3d8a9c9 100644 --- a/src/Services/Every8d.php +++ b/src/Services/Every8d.php @@ -24,18 +24,7 @@ public function __construct() public function send(): array { - if(empty($this->destination)) throw new InvalidSms('The empty destination is invalid.'); - if(empty($this->text)) throw new InvalidSms('The empty text is invalid.'); - if($this->isGlobalPhoneNumber()) $this->destination = '0' . substr($this->destination, 3, 9); - - $params = [ - config('taiwan_sms.services.every8d.username'), - config('taiwan_sms.services.every8d.password'), - $this->subject, - urlencode($this->text), - $this->destination - ]; - $this->url = sprintf(config('taiwan_sms.services.every8d.url'), ...$params); + $this->prepare(); $response = $this->client->request('GET', $this->url); return [ @@ -51,4 +40,31 @@ public function isGlobalPhoneNumber(): bool { return strlen($this->destination) == 12 && substr($this->destination, 0, -9) == '886'; } + + /** + * @return void + * @throws InvalidSms + */ + public function prepare(): void + { + if (empty($this->destination)) throw new InvalidSms('The empty destination is invalid.'); + if (empty($this->text)) throw new InvalidSms('The empty text is invalid.'); + if ($this->isGlobalPhoneNumber()) $this->destination = '0' . substr($this->destination, 3, 9); + + $params = [ + config('taiwan_sms.services.every8d.username'), + config('taiwan_sms.services.every8d.password'), + $this->subject, + urlencode($this->text), + $this->destination + ]; + $this->url = sprintf(config('taiwan_sms.services.every8d.url'), ...$params); + } + + public function test() + { + $this->prepare(); + + return ['url' => $this->url]; + } } diff --git a/src/Services/Infobip.php b/src/Services/Infobip.php index c734219..b3a2d41 100644 --- a/src/Services/Infobip.php +++ b/src/Services/Infobip.php @@ -36,18 +36,7 @@ public function __construct() public function send(): array { - if(empty($this->destination)) throw new InvalidSms('The empty destination is invalid.'); - if(empty($this->text)) throw new InvalidSms('The empty text is invalid.'); - if($this->isTaiwanPhoneNumber()) $this->destination = '886' . substr($this->destination, 1, 9); - - - $destination = (new SmsDestination())->setTo($this->destination); - $message = (new SmsTextualMessage()) - ->setFrom($this->subject) - ->setText($this->text) - ->setDestinations([$destination]); - $request = (new SmsAdvancedTextualRequest()) - ->setMessages([$message]); + $request = $this->prepare(); $response = $this->api->sendSmsMessage($request); @@ -68,4 +57,32 @@ public function isTaiwanPhoneNumber(): bool { return strlen($this->destination) == 10 && substr($this->destination, 0, -8) == '09'; } + + /** + * @return SmsAdvancedTextualRequest + * @throws InvalidSms + */ + public function prepare(): SmsAdvancedTextualRequest + { + if (empty($this->destination)) throw new InvalidSms('The empty destination is invalid.'); + if (empty($this->text)) throw new InvalidSms('The empty text is invalid.'); + if ($this->isTaiwanPhoneNumber()) $this->destination = '886' . substr($this->destination, 1, 9); + + $destination = (new SmsDestination())->setTo($this->destination); + $message = (new SmsTextualMessage()) + ->setFrom($this->subject) + ->setText($this->text) + ->setDestinations([$destination]); + return (new SmsAdvancedTextualRequest()) + ->setMessages([$message]); + } + + public function test() + { + $request = $this->prepare(); + + return [ + 'data' => json_encode($request->getMessages()), + ]; + } } diff --git a/src/Services/Kotsms.php b/src/Services/Kotsms.php index c9b183e..aac56dc 100644 --- a/src/Services/Kotsms.php +++ b/src/Services/Kotsms.php @@ -9,7 +9,7 @@ class Kotsms extends BaseSms { protected $client; - protected $url; + public $url; public const CGI_ERROR = '-1'; public const AUTH_ERROR = '-2'; @@ -52,10 +52,6 @@ public function __construct() if(empty(config('taiwan_sms.services.kotsms.username'))) throw new InvalidSms('kotsms need username'); if(empty(config('taiwan_sms.services.kotsms.password'))) throw new InvalidSms('kotsms need password'); - $this->url = config('taiwan_sms.services.kotsms.url') . - '?username=' . config('taiwan_sms.services.kotsms.username') . - '&password=' . config('taiwan_sms.services.kotsms.password'); - $this->client = new Client([ 'timeout' => config('taiwan_sms.timeout', 5), 'connect_timeout' => config('taiwan_sms.timeout', 5), @@ -64,17 +60,7 @@ public function __construct() public function send(): array { - if(empty($this->destination)) throw new InvalidSms('The empty destination is invalid.'); - if(empty($this->text)) throw new InvalidSms('The empty text is invalid.'); - if($this->isGlobalPhoneNumber()) $this->destination = '0' . substr($this->destination, 3, 9); - - $params = [ - config('taiwan_sms.services.kotsms.username'), - config('taiwan_sms.services.kotsms.password'), - $this->destination, - urlencode(iconv(mb_detect_encoding($this->text), "big5", $this->text)) - ]; - $this->url = sprintf(config('taiwan_sms.services.kotsms.url'), ...$params); + $this->prepare(); $response = $this->client->get($this->url); @@ -132,4 +118,30 @@ public function isGlobalPhoneNumber(): bool { return strlen($this->destination) == 12 && substr($this->destination, 0, -9) == '886'; } + + /** + * @return void + * @throws InvalidSms + */ + public function prepare(): void + { + if (empty($this->destination)) throw new InvalidSms('The empty destination is invalid.'); + if (empty($this->text)) throw new InvalidSms('The empty text is invalid.'); + if ($this->isGlobalPhoneNumber()) $this->destination = '0' . substr($this->destination, 3, 9); + + $params = [ + config('taiwan_sms.services.kotsms.username'), + config('taiwan_sms.services.kotsms.password'), + $this->destination, + urlencode(iconv(mb_detect_encoding($this->text), "big5", $this->text)) + ]; + $this->url = sprintf(config('taiwan_sms.services.kotsms.url'), ...$params); + } + + public function test() + { + $this->prepare(); + + return ['url' => $this->url]; + } } diff --git a/src/Services/Mitake.php b/src/Services/Mitake.php index 3aeed58..7d6e895 100644 --- a/src/Services/Mitake.php +++ b/src/Services/Mitake.php @@ -30,16 +30,7 @@ public function __construct() public function send(): array { - if(empty($this->destination)) throw new InvalidSms('The empty destination is invalid.'); - if(empty($this->text)) throw new InvalidSms('The empty text is invalid.'); - if($this->isGlobalPhoneNumber()) $this->destination = '0' . substr($this->destination, 3, 9); - - $data = [ - 'username'=> config('taiwan_sms.services.mitake.username'), - 'password'=> config('taiwan_sms.services.mitake.password'), - 'dstaddr' => $this->destination, - 'smbody' => iconv(mb_detect_encoding($this->text), "UTF-8", $this->text), - ]; + $data = $this->prepare(); $response = $this->client->post($this->url, $data); @@ -60,4 +51,30 @@ public function isGlobalPhoneNumber(): bool { return strlen($this->destination) == 12 && substr($this->destination, 0, -9) == '886'; } + + /** + * @return array + * @throws InvalidSms + */ + public function prepare(): array + { + if (empty($this->destination)) throw new InvalidSms('The empty destination is invalid.'); + if (empty($this->text)) throw new InvalidSms('The empty text is invalid.'); + if ($this->isGlobalPhoneNumber()) $this->destination = '0' . substr($this->destination, 3, 9); + + return [ + 'username' => config('taiwan_sms.services.mitake.username'), + 'password' => config('taiwan_sms.services.mitake.password'), + 'dstaddr' => $this->destination, + 'smbody' => iconv(mb_detect_encoding($this->text), "UTF-8", $this->text), + ]; + } + + public function test() + { + $data = $this->prepare(); + + return ['url' => $this->url, 'data' => http_build_query($data)]; + } + } diff --git a/src/TaiwanSms.php b/src/TaiwanSms.php index b69a9aa..9760881 100644 --- a/src/TaiwanSms.php +++ b/src/TaiwanSms.php @@ -5,14 +5,14 @@ class TaiwanSms { - public static function send($destination, $text) + public static function send($destination, $text, $test = false) { try { - $response = self::process(self::getPrimaryClassName(), $text, $destination); + $response = self::process(self::getPrimaryClassName(), $text, $destination, $test); }catch (\Exception $exception) { if(empty(self::getFailoverClassName())) throw new InvalidSms($exception->getMessage()); try { - $response = self::process(self::getFailoverClassName(), $text, $destination); + $response = self::process(self::getFailoverClassName(), $text, $destination, $test); }catch (\Exception $exception) { throw new InvalidSms($exception->getMessage()); } @@ -27,11 +27,13 @@ public static function send($destination, $text) * @param $destination * @return array */ - public static function process(string $class, $text, $destination): array + public static function process(string $class, $text, $destination, $test = false): array { $api = new $class(); $api->setText($text); $api->setDestination($destination); + if($test) return $api->test(); + return $api->send(); } diff --git a/tests/Feature/TaiwanSmsTest.php b/tests/Feature/TaiwanSmsTest.php index 838e048..6ceb674 100644 --- a/tests/Feature/TaiwanSmsTest.php +++ b/tests/Feature/TaiwanSmsTest.php @@ -2,11 +2,105 @@ namespace Jarvisho\TaiwanSmsLaravel\Tests\Feature; +use Illuminate\Support\Str; use Jarvisho\TaiwanSmsLaravel\TaiwanSms; use Jarvisho\TaiwanSmsLaravel\Tests\TestCase; class TaiwanSmsTest extends TestCase { + /** @test */ + function kotsms() + { + $username = Str::random(8); + $password = Str::random(8); + $phone = '09' . random_int(10000,99999) . random_int(100,999); + $text = Str::random(8); + + config(['taiwan_sms' => ['primary' => 'kotsms', + 'failover' => '', + 'timeout' => 5, + 'services' => [ + 'kotsms' => [ + 'url' => env('KOTSMS_URL', 'https://api.kotsms.com.tw/kotsmsapi-1.php?username=%s&password=%s&dstaddr=%s&smbody=%s&response='), + 'username' => $username, + 'password' => $password, + ] + ]]]); + + $result = TaiwanSms::send($phone, $text, true); + expect(data_get($result, 'url'))->toBe('https://api.kotsms.com.tw/kotsmsapi-1.php?username='. $username .'&password='. $password . '&dstaddr='. $phone .'&smbody=' . $text . '&response='); + } + + /** @test */ + function every8d() + { + $username = Str::random(8); + $password = Str::random(8); + $phone = '09' . random_int(10000,99999) . random_int(100,999); + $text = Str::random(8); + + config(['taiwan_sms' => ['primary' => 'every8d', + 'failover' => '', + 'timeout' => 5, + 'services' => [ + 'every8d' => [ + 'url' => env('EVERY8D_URL', 'http://biz3.every8d.com.tw/prepaid/API21/HTTP/sendSMS.ashx?UID=%s&PWD=%s&SB=%s&MSG=%s&DEST=%s'), + 'username' => $username, + 'password' => $password, + ] + ]]]); + + $result = TaiwanSms::send($phone, $text, true); + expect(data_get($result, 'url'))->toBe('http://biz3.every8d.com.tw/prepaid/API21/HTTP/sendSMS.ashx?UID='. $username .'&PWD='. $password . '&SB=&MSG=' . $text . '&DEST=' . $phone); + } + + /** @test */ + function mitake() + { + $username = Str::random(8); + $password = Str::random(8); + $phone = '09' . random_int(10000,99999) . random_int(100,999); + $text = Str::random(8); + + config(['taiwan_sms' => ['primary' => 'mitake', + 'failover' => '', + 'timeout' => 5, + 'services' => [ + 'mitake' => [ + 'url' => env('MITAKE_URL', 'https://sms.mitake.com.tw/b2c/mtk/SmSend?CharsetURL=UTF-8'), + 'username' => $username, + 'password' => $password, + ] + ]]]); + + $result = TaiwanSms::send($phone, $text, true); + expect(data_get($result, 'url'))->toBe('https://sms.mitake.com.tw/b2c/mtk/SmSend?CharsetURL=UTF-8'); + expect(data_get($result, 'data'))->toBe('username='. $username .'&password='. $password . '&dstaddr=' . $phone . '&smbody=' . $text); + } + + /** @test */ + function infobip() + { + $username = Str::random(8); + $password = Str::random(8); + $phone = '8869' . random_int(10000,99999) . random_int(100,999); + $text = Str::random(8); + + config(['taiwan_sms' => ['primary' => 'infobip', + 'failover' => '', + 'timeout' => 5, + 'services' => [ + 'infobip' => [ + 'url' => env('INFOBIP_URL', 'https://vqlkm.api.infobip.com'), + 'username' => $username, + 'password' => $password, + ] + ]]]); + + $result = TaiwanSms::send($phone, $text, true); + expect(data_get($result, 'data'))->toBe('[{"destinations":[{"to":"' . $phone . '"}],"from":"","text":"'. $text .'"}]'); + } + /** @test */ function getPrimaryClassNameTest() {