forked from overtrue/easy-sms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YunpianGatewayTest.php
110 lines (97 loc) · 4.23 KB
/
YunpianGatewayTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?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\YunpianGateway;
use Overtrue\EasySms\Message;
use Overtrue\EasySms\PhoneNumber;
use Overtrue\EasySms\Support\Config;
use Overtrue\EasySms\Tests\TestCase;
class YunpianGatewayTest extends TestCase
{
public function testSend()
{
$config = [
'api_key' => 'mock-api-key',
];
$gateway = \Mockery::mock(YunpianGateway::class.'[request]', [$config])->shouldAllowMockingProtectedMethods();
$gateway->shouldReceive('request')->with('post', 'https://sms.yunpian.com/v2/sms/single_send.json', [
'form_params' => [
'apikey' => 'mock-api-key',
'mobile' => '18188888888',
'text' => '【overtrue】This is a test message.',
],
'exceptions' => false,
])->andReturn([
'code' => 0,
'msg' => '发送成功',
'count' => 1, //成功发送的短信计费条数
'fee' => 0.05, //扣费条数,70个字一条,超出70个字时按每67字一条计
'unit' => 'RMB', // 计费单位
'mobile' => '18188888888', // 发送手机号
'sid' => 3310228982, // 短信ID
], [
'code' => 100,
'msg' => '发送失败',
])->times(2);
$message = new Message(['content' => '【overtrue】This is a test message.']);
$config = new Config($config);
$this->assertSame([
'code' => 0,
'msg' => '发送成功',
'count' => 1, //成功发送的短信计费条数
'fee' => 0.05, //扣费条数,70个字一条,超出70个字时按每67字一条计
'unit' => 'RMB', // 计费单位
'mobile' => '18188888888', // 发送手机号
'sid' => 3310228982, // 短信ID
], $gateway->send(new PhoneNumber(18188888888), $message, $config));
$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(100);
$this->expectExceptionMessage('发送失败');
$gateway->send(new PhoneNumber(18188888888), $message, $config);
}
public function testDefaultSignature()
{
$config = [
'api_key' => 'mock-api-key',
'signature' => '【测试】',
];
$response = [
'code' => 0,
'msg' => '发送成功',
'count' => 1, //成功发送的短信计费条数
'fee' => 0.05, //扣费条数,70个字一条,超出70个字时按每67字一条计
'unit' => 'RMB', // 计费单位
'mobile' => '18188888888', // 发送手机号
'sid' => 3310228982, // 短信ID
];
$gateway = \Mockery::mock(YunpianGateway::class.'[request]', [$config])->shouldAllowMockingProtectedMethods();
$config = new Config($config);
$gateway->shouldReceive('request')->with('post', 'https://sms.yunpian.com/v2/sms/single_send.json', [
'form_params' => [
'apikey' => 'mock-api-key',
'mobile' => '18188888888',
'text' => '【测试】This is a 【test】 message.',
],
'exceptions' => false,
])->andReturn($response);
$this->assertSame($response, $gateway->send(new PhoneNumber(18188888888), new Message(['content' => 'This is a 【test】 message.']), $config));
// with signature
$gateway->shouldReceive('request')->with('post', 'https://sms.yunpian.com/v2/sms/single_send.json', [
'form_params' => [
'apikey' => 'mock-api-key',
'mobile' => '18188888888',
'text' => '【已经存在】This is a 【test】 message.',
],
'exceptions' => false,
])->andReturn($response);
$this->assertSame($response, $gateway->send(new PhoneNumber(18188888888), new Message(['content' => '【已经存在】This is a 【test】 message.']), $config));
}
}