Skip to content

Commit

Permalink
test(service): add prepare
Browse files Browse the repository at this point in the history
  • Loading branch information
JarvisHo committed Jan 21, 2022
1 parent 0d570cd commit d9a48b6
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 54 deletions.
40 changes: 28 additions & 12 deletions src/Services/Every8d.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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];
}
}
41 changes: 29 additions & 12 deletions src/Services/Infobip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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()),
];
}
}
44 changes: 28 additions & 16 deletions src/Services/Kotsms.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class Kotsms extends BaseSms
{
protected $client;
protected $url;
public $url;

public const CGI_ERROR = '-1';
public const AUTH_ERROR = '-2';
Expand Down Expand Up @@ -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),
Expand All @@ -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);

Expand Down Expand Up @@ -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];
}
}
37 changes: 27 additions & 10 deletions src/Services/Mitake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)];
}

}
10 changes: 6 additions & 4 deletions src/TaiwanSms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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();
}

Expand Down
94 changes: 94 additions & 0 deletions tests/Feature/TaiwanSmsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit d9a48b6

Please sign in to comment.