-
-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,5 @@ composer.lock | |
.subsplit | ||
.php_cs.cache | ||
.idea | ||
index.php | ||
config.php | ||
/index.php | ||
/config.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/* | ||
* This file is part of the sora. | ||
* | ||
* (c) 2016 weibo.com | ||
*/ | ||
|
||
namespace Overtrue\EasySms\Gateways; | ||
|
||
|
||
use Overtrue\EasySms\Contracts\MessageInterface; | ||
use Overtrue\EasySms\Exceptions\GatewayErrorException; | ||
use Overtrue\EasySms\Support\Config; | ||
use Overtrue\EasySms\Traits\HasHttpRequest; | ||
|
||
class JuheGateway extends Gateway | ||
{ | ||
use HasHttpRequest; | ||
|
||
const ENDPOINT_URL = 'http://v.juhe.cn/sms/send'; | ||
const ENDPOINT_FORMAT = 'json'; | ||
|
||
/** | ||
* @param array|int|string $to | ||
* @param \Overtrue\EasySms\Contracts\MessageInterface $message | ||
* @param \Overtrue\EasySms\Support\Config $config | ||
* | ||
* @return array | ||
* | ||
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException; | ||
*/ | ||
public function send($to, MessageInterface $message, Config $config) | ||
{ | ||
$params = [ | ||
'mobile' => $to, | ||
'tpl_id' => $message->getTemplate($this), | ||
'tpl_value' => $this->formatTemplateVars($message->getData($this)), | ||
'dtype' => self::ENDPOINT_FORMAT, | ||
'key' => $config->get('key'), | ||
]; | ||
|
||
$result = $this->get(self::ENDPOINT_URL, $params); | ||
|
||
if ($result['error_code']) { | ||
throw new GatewayErrorException($result['reason'], $result['error_code'], $result); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @param array $vars | ||
* | ||
* @return string | ||
*/ | ||
protected function formatTemplateVars(array $vars) | ||
{ | ||
$formatted = []; | ||
|
||
foreach ($vars as $key => $value) { | ||
$formatted[sprintf('#%s#', trim($key, '#'))] = $value; | ||
} | ||
|
||
return http_build_query($formatted); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the overtrue/easy-sms. | ||
* (c) overtrue <[email protected]> | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Overtrue\EasySms\Tests\Gateways; | ||
|
||
use Overtrue\EasySms\Exceptions\GatewayErrorException; | ||
use Overtrue\EasySms\Gateways\JuheGateway; | ||
use Overtrue\EasySms\Message; | ||
use Overtrue\EasySms\Support\Config; | ||
use Overtrue\EasySms\Tests\TestCase; | ||
|
||
class JuheGatewayTest extends TestCase | ||
{ | ||
public function testSend() | ||
{ | ||
$config = [ | ||
'key' => 'mock-key', | ||
]; | ||
$gateway = \Mockery::mock(JuheGateway::class.'[get]', [$config])->shouldAllowMockingProtectedMethods(); | ||
|
||
$params = [ | ||
'mobile' => 18188888888, | ||
'tpl_id' => 'mock-tpl-id', | ||
'tpl_value' => http_build_query(['#code#' => 1234]), | ||
'dtype' => 'json', | ||
'key' => 'mock-key', | ||
]; | ||
$gateway->shouldReceive('get')->with(JuheGateway::ENDPOINT_URL, $params) | ||
->andReturn([ | ||
'reason' => "操作成功", | ||
'error_code' => 0, | ||
], [ | ||
'reason' => "操作失败", | ||
'error_code' => 21000, | ||
])->times(2); | ||
|
||
$message = new Message([ | ||
'content' => 'This is a huyi test message.', | ||
'template' => 'mock-tpl-id', | ||
'data' => [ | ||
'code' => 1234 | ||
], | ||
]); | ||
|
||
$config = new Config($config); | ||
|
||
$this->assertSame([ | ||
'reason' => "操作成功", | ||
'error_code' => 0, | ||
], $gateway->send(18188888888, $message, $config)); | ||
|
||
$this->expectException(GatewayErrorException::class); | ||
$this->expectExceptionCode(21000); | ||
$this->expectExceptionMessage('操作失败'); | ||
|
||
$gateway->send(18188888888, $message, $config); | ||
} | ||
} |