diff --git a/.gitignore b/.gitignore index 3c7666a..f0cec89 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,5 @@ composer.lock .subsplit .php_cs.cache .idea -index.php -config.php \ No newline at end of file +/index.php +/config.php \ No newline at end of file diff --git a/README.md b/README.md index 936f3f9..a4de1b1 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ $easySms->send(13188888888, $message); - [阿里大鱼](https://github.com/overtrue/easy-sms/wiki/GateWays---AliDayu) - [容联云通讯](https://github.com/overtrue/easy-sms/wiki/GateWays---Yuntongxun) - [互亿无线](https://github.com/overtrue/easy-sms/wiki/GateWays---Huyi) +- [聚合数据](https://github.com/overtrue/easy-sms/wiki/GateWays---Juhe) - SendCloud # License diff --git a/src/Gateways/JuheGateway.php b/src/Gateways/JuheGateway.php new file mode 100644 index 0000000..65a3258 --- /dev/null +++ b/src/Gateways/JuheGateway.php @@ -0,0 +1,66 @@ + $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); + } +} \ No newline at end of file diff --git a/tests/Gateways/JuheGatewayTest.php b/tests/Gateways/JuheGatewayTest.php new file mode 100644 index 0000000..89dd773 --- /dev/null +++ b/tests/Gateways/JuheGatewayTest.php @@ -0,0 +1,64 @@ + + * 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); + } +}