Skip to content

Commit b35cd30

Browse files
timjulyovertrue
authored andcommitted
腾讯云阿里云发送短信可以指定模板 (#166)
* 短信签名 * phpunit * 5.6 * phpunit 7 * unit test * Update AliyunGateway.php * Update AliyunGateway.php * Update QcloudGateway.php * Update AliyunGateway.php * Update QcloudGateway.php
1 parent 3b45585 commit b35cd30

File tree

6 files changed

+78
-41
lines changed

6 files changed

+78
-41
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ sudo: false
1212

1313
install: travis_retry composer install --no-interaction --prefer-source
1414

15-
script: vendor/bin/phpunit --verbose
15+
script: ./vendor/bin/phpunit --verbose --coverage-text

composer.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"php": ">=5.6"
88
},
99
"require-dev": {
10-
"phpunit/phpunit": "^5.6",
10+
"phpunit/phpunit": "^5.7 || ^7.5",
1111
"mockery/mockery": "1.0.x-dev"
1212
},
1313
"autoload": {
@@ -21,10 +21,8 @@
2121
}
2222
},
2323
"license": "MIT",
24-
"authors": [
25-
{
26-
"name": "overtrue",
27-
"email": "[email protected]"
28-
}
29-
]
30-
}
24+
"authors": [{
25+
"name": "overtrue",
26+
"email": "[email protected]"
27+
}]
28+
}

phpunit.xml.dist

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
convertNoticesToExceptions="true"
88
convertWarningsToExceptions="true"
99
processIsolation="false"
10-
stopOnFailure="false"
11-
syntaxCheck="false">
10+
stopOnFailure="false">
1211
<testsuites>
1312
<testsuite name="Application Test Suite">
1413
<directory>./tests/</directory>

src/Gateways/AliyunGateway.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class AliyunGateway extends Gateway
5353
*/
5454
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
5555
{
56+
$data = $message->getData($this);
57+
58+
$signName = !empty($data['sign_name']) ? $data['sign_name'] : $config->get('sign_name');
59+
60+
unset($data['sign_name']);
61+
5662
$params = [
5763
'RegionId' => self::ENDPOINT_REGION_ID,
5864
'AccessKeyId' => $config->get('access_key_id'),
@@ -64,9 +70,9 @@ public function send(PhoneNumberInterface $to, MessageInterface $message, Config
6470
'Action' => self::ENDPOINT_METHOD,
6571
'Version' => self::ENDPOINT_VERSION,
6672
'PhoneNumbers' => !\is_null($to->getIDDCode()) ? strval($to->getZeroPrefixedNumber()) : $to->getNumber(),
67-
'SignName' => $config->get('sign_name'),
73+
'SignName' => $signName,
6874
'TemplateCode' => $message->getTemplate($this),
69-
'TemplateParam' => json_encode($message->getData($this), JSON_FORCE_OBJECT),
75+
'TemplateParam' => json_encode($data, JSON_FORCE_OBJECT),
7076
];
7177

7278
$params['Signature'] = $this->generateSign($params);

src/Gateways/QcloudGateway.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ class QcloudGateway extends Gateway
4545
*/
4646
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
4747
{
48-
$type = !empty($message->getData($this)['type']) ? $message->getData($this)['type'] : 0;
48+
$data = $message->getData($this);
49+
50+
$signName = !empty($data['sign_name']) ? $data['sign_name'] : $config->get('sign_name');
51+
52+
unset($data['sign_name']);
53+
54+
$type = !empty($data['type']) ? $data['type'] : 0;
4955
$params = [
5056
'tel' => [
5157
'nationcode' => $to->getIDDCode() ?: 86,
@@ -57,11 +63,11 @@ public function send(PhoneNumberInterface $to, MessageInterface $message, Config
5763
'extend' => '',
5864
'ext' => '',
5965
];
60-
if (!is_null($message->getTemplate($this)) && is_array($message->getData($this))) {
66+
if (!is_null($message->getTemplate($this)) && is_array($data)) {
6167
unset($params['msg']);
62-
$params['params'] = array_values($message->getData($this));
68+
$params['params'] = array_values($data);
6369
$params['tpl_id'] = $message->getTemplate($this);
64-
$params['sign'] = $config->get('sign_name') ? $config->get('sign_name') : '';
70+
$params['sign'] = $signName;
6571
}
6672
$random = substr(uniqid(), -10);
6773

tests/Gateways/QcloudGatewayTest.php

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Overtrue\EasySms\Tests\Gateways;
1313

14+
use Overtrue\EasySms\Contracts\GatewayInterface;
1415
use Overtrue\EasySms\Exceptions\GatewayErrorException;
1516
use Overtrue\EasySms\Gateways\QcloudGateway;
1617
use Overtrue\EasySms\Message;
@@ -28,18 +29,6 @@ public function testSend()
2829
];
2930
$gateway = \Mockery::mock(QcloudGateway::class.'[request]', [$config])->shouldAllowMockingProtectedMethods();
3031

31-
$expected = [
32-
'tel' => [
33-
'nationcode' => '86',
34-
'mobile' => strval(new PhoneNumber(18888888888)),
35-
],
36-
'type' => 0,
37-
'msg' => 'This is a test message.',
38-
'timestamp' => time(),
39-
'extend' => '',
40-
'ext' => '',
41-
];
42-
4332
$gateway->shouldReceive('request')
4433
->andReturn([
4534
'result' => 0,
@@ -79,18 +68,6 @@ public function testSendUsingNationCode()
7968
];
8069
$gateway = \Mockery::mock(QcloudGateway::class.'[request]', [$config])->shouldAllowMockingProtectedMethods();
8170

82-
$expected = [
83-
'tel' => [
84-
'nationcode' => 251,
85-
'mobile' => 18888888888,
86-
],
87-
'type' => 0,
88-
'msg' => 'This is a test message.',
89-
'timestamp' => time(),
90-
'extend' => '',
91-
'ext' => '',
92-
];
93-
9471
$gateway->shouldReceive('request')
9572
->andReturn([
9673
'result' => 0,
@@ -121,4 +98,55 @@ public function testSendUsingNationCode()
12198

12299
$gateway->send(new PhoneNumber(18888888888, 251), $message, $config);
123100
}
101+
102+
public function testSendUsingTemplate()
103+
{
104+
$config = [
105+
'sdk_app_id' => 'mock-sdk-app-id',
106+
'app_key' => 'mock-api-key',
107+
];
108+
$gateway = \Mockery::mock(QcloudGateway::class.'[request]', [$config])->shouldAllowMockingProtectedMethods();
109+
110+
$gateway->shouldReceive('request')
111+
->with(\Mockery::any(), \Mockery::any(), \Mockery::subset([
112+
'json' => [
113+
'tpl_id' => 'template-id',
114+
'params' => [
115+
'param1',
116+
'param2',
117+
],
118+
],
119+
]))
120+
->andReturn([
121+
'result' => 0,
122+
'errmsg' => 'OK',
123+
'ext' => '',
124+
'sid' => 3310228982,
125+
'fee' => 1,
126+
], [
127+
'result' => 1001,
128+
'errmsg' => 'sig校验失败',
129+
])->twice();
130+
131+
$message = \Mockery::mock(Message::class);
132+
$message->allows()->getTemplate()->withArgs([GatewayInterface::class])->andReturns('template-id');
133+
$message->allows()->getData()->withArgs([GatewayInterface::class])->andReturns(['param1', 'param2', 'sign_name' => 'sign']);
134+
$message->allows()->getContent()->withArgs([GatewayInterface::class])->andReturns(null);
135+
136+
$config = new Config($config);
137+
138+
$this->assertSame([
139+
'result' => 0,
140+
'errmsg' => 'OK',
141+
'ext' => '',
142+
'sid' => 3310228982,
143+
'fee' => 1,
144+
], $gateway->send(new PhoneNumber(18888888888), $message, $config));
145+
146+
$this->expectException(GatewayErrorException::class);
147+
$this->expectExceptionCode(1001);
148+
$this->expectExceptionMessage('sig校验失败');
149+
150+
$gateway->send(new PhoneNumber(18888888888), $message, $config);
151+
}
124152
}

0 commit comments

Comments
 (0)