Skip to content

Commit

Permalink
✨ Add Juhe gateway.
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed May 2, 2017
1 parent a3a56b3 commit f465467
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ composer.lock
.subsplit
.php_cs.cache
.idea
index.php
config.php
/index.php
/config.php
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions src/Gateways/JuheGateway.php
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);
}
}
64 changes: 64 additions & 0 deletions tests/Gateways/JuheGatewayTest.php
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);
}
}

0 comments on commit f465467

Please sign in to comment.