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.
Merge pull request overtrue#4 from phz/master
Add Submail
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?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\HasHttpRequest; | ||
|
||
/** | ||
* Class SubmailGateway. | ||
*/ | ||
class SubmailGateway extends Gateway | ||
{ | ||
|
||
use HasHttpRequest; | ||
|
||
const ENDPOINT_TEMPLATE = 'https://api.mysubmail.com/message/%s.%s'; | ||
const ENDPOINT_FORMAT = 'json'; | ||
|
||
/** | ||
* Send a short message. | ||
* | ||
* @param string|int $to | ||
* @param string $message | ||
* @param array $data | ||
* | ||
* @return mixed | ||
*/ | ||
public function send($to, $message, array $data = []) | ||
{ | ||
$endpoint = $this->buildEndpoint('xsend'); | ||
return $this->post($endpoint, [ | ||
'appid' => $this->config->get('app_id'), | ||
'signature' => $this->config->get('app_key'), | ||
'project' => $this->config->get('project'), | ||
'to' => $to, | ||
'vars' => json_encode($data), | ||
]); | ||
} | ||
|
||
/** | ||
* Build endpoint url. | ||
* | ||
* @param string $function | ||
* | ||
* @return string | ||
*/ | ||
protected function buildEndpoint($function) | ||
{ | ||
return sprintf(self::ENDPOINT_TEMPLATE,$function, self::ENDPOINT_FORMAT); | ||
} | ||
} |
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,35 @@ | ||
<?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\Gateways\SubmailGateway; | ||
use Overtrue\EasySms\Tests\TestCase; | ||
|
||
class SubmailGatewayTest extends TestCase | ||
{ | ||
public function testSend() | ||
{ | ||
$gateway = \Mockery::mock(SubmailGateway::class.'[post]', [[ | ||
'app_id' => 'mock-app-id', | ||
'app_key' => 'mock-app-key', | ||
'project' => 'mock-project' | ||
]])->shouldAllowMockingProtectedMethods(); | ||
|
||
$gateway->expects()->post('https://api.mysubmail.com/message/xsend.json', [ | ||
'appid' => 'mock-app-id', | ||
'signature' => 'mock-app-key', | ||
'project' => 'mock-project', | ||
'to' => 18188888888, | ||
'vars' => json_encode(array(['code'=>'123456','time'=>'15'])), | ||
])->andReturn('mock-result')->once(); | ||
|
||
$this->assertSame('mock-result', $gateway->send(18188888888, '',array(['code'=>'123456','time'=>'15']))); | ||
} | ||
} |