Skip to content

Commit

Permalink
Merge pull request overtrue#4 from phz/master
Browse files Browse the repository at this point in the history
Add Submail
  • Loading branch information
overtrue authored Mar 16, 2017
2 parents ce34209 + cd0e88d commit 85bd0be
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/Gateways/SubmailGateway.php
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);
}
}
35 changes: 35 additions & 0 deletions tests/Gateways/SubmailGatewayTest.php
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'])));
}
}

0 comments on commit 85bd0be

Please sign in to comment.