forked from overtrue/easy-sms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature add sms Gateway DreamNet and Huaxin (overtrue#33)
* Feature add gateways Dreamnet and Huanxin ✨ * Feature test the gateway Dreamnet and huaxin ✅ * Fix code style * Fix drop DreamNet gateway and fix code style 🐛
- Loading branch information
Showing
3 changed files
with
178 additions
and
15 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
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,75 @@ | ||
<?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\Gateways; | ||
|
||
use Overtrue\EasySms\Contracts\MessageInterface; | ||
use Overtrue\EasySms\Exceptions\GatewayErrorException; | ||
use Overtrue\EasySms\Support\Config; | ||
use Overtrue\EasySms\Traits\HasHttpRequest; | ||
|
||
/** | ||
* Class HuaxinGateway. | ||
* | ||
* @see http://www.ipyy.com/help/ | ||
*/ | ||
class HuaxinGateway extends Gateway | ||
{ | ||
use HasHttpRequest; | ||
|
||
const ENDPOINT_TEMPLATE = 'http://%s/smsJson.aspx'; | ||
|
||
/** | ||
* @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) | ||
{ | ||
$endpoint = $this->buildEndpoint( | ||
$config->get('ip') | ||
); | ||
|
||
$result = $this->post($endpoint, [ | ||
'userid' => $config->get('user_id'), | ||
'account' => $config->get('account'), | ||
'password' => $config->get('password'), | ||
'mobile' => $to, | ||
'content' => $message->getContent(), | ||
'sendTime' => '', | ||
'action' => 'send', | ||
'extno' => $config->get('ext_no'), | ||
]); | ||
|
||
if ($result['returnstatus'] !== 'Success') { | ||
throw new GatewayErrorException($result['message'], 500, $result); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Build endpoint url. | ||
* | ||
* @param string $ip | ||
* @param string $port | ||
* | ||
* @return string | ||
*/ | ||
protected function buildEndpoint($ip) | ||
{ | ||
return sprintf(self::ENDPOINT_TEMPLATE, $ip); | ||
} | ||
} |
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,75 @@ | ||
<?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\HuaxinGateway; | ||
use Overtrue\EasySms\Message; | ||
use Overtrue\EasySms\Support\Config; | ||
use Overtrue\EasySms\Tests\TestCase; | ||
|
||
class HuaxinGatewayTest extends TestCase | ||
{ | ||
public function testSend() | ||
{ | ||
$config = [ | ||
'user_id' => 'mock-user-id', | ||
'password' => 'mock-password', | ||
'account' => 'mock-account', | ||
'ip' => '127.0.0.1', | ||
'ext_no' => '', | ||
]; | ||
$gateway = \Mockery::mock(HuaxinGateway::class.'[post]', [$config])->shouldAllowMockingProtectedMethods(); | ||
|
||
$gateway->shouldReceive('post')->with('http://127.0.0.1/smsJson.aspx', [ | ||
'userid' => 'mock-user-id', | ||
'password' => 'mock-password', | ||
'account' => 'mock-account', | ||
'mobile' => 18188888888, | ||
'content' => '【TIGERB】This is a test message.', | ||
'sendTime' => '', | ||
'action' => 'send', | ||
'extno' => '', | ||
])->andReturn([ | ||
'returnstatus' => 'Success', | ||
'message' => '操作成功', | ||
'remainpoint' => '100', | ||
'taskID' => '1504080852350206', | ||
'successCounts' => '1', | ||
], [ | ||
'returnstatus' => 'Faild', | ||
'message' => '操作失败', | ||
'remainpoint' => '0', | ||
'taskID' => '0', | ||
'successCounts' => '0', | ||
])->times(2); | ||
|
||
$message = new Message(['content' => '【TIGERB】This is a test message.']); | ||
$config = new Config($config); | ||
$this->assertSame( | ||
[ | ||
'returnstatus' => 'Success', | ||
'message' => '操作成功', | ||
'remainpoint' => '100', | ||
'taskID' => '1504080852350206', | ||
'successCounts' => '1', | ||
], | ||
$gateway->send(18188888888, $message, $config) | ||
); | ||
|
||
$this->expectException(GatewayErrorException::class); | ||
$this->expectExceptionCode(500); | ||
$this->expectExceptionMessage('操作失败'); | ||
|
||
$gateway->send(18188888888, $message, $config); | ||
} | ||
} |