-
Notifications
You must be signed in to change notification settings - Fork 2
/
DingtalkTest.php
193 lines (161 loc) · 6.47 KB
/
DingtalkTest.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php declare(strict_types=1);
/*
* This file is part of Pusher.
*
* (c) Jetsung Chan <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Pusher\Tests\Channels;
use PHPUnit\Framework\TestCase;
use Pusher\Channel\Dingtalk;
use Pusher\Message\DingtalkMessage;
class DingtalkTest extends TestCase
{
private string $token = '';
private string $secret = '';
## 钉钉限制每分钟只能发 20 条信息
private static bool $PASS = false;
public function setUp(): void
{
$token = getenv('DingtalkToken');
$secret = getenv('DingtalkSecret');
if ($token) {
$this->token = $token;
$secret && $this->secret = $secret;
} else {
self::$PASS = true;
}
}
public function skipTest(string $func, bool $skip = false): void
{
if (self::$PASS || $skip) {
$this->markTestSkipped("skip {$func}");
}
}
// 延时
public function timeSleep(int $time = 5): void
{
sleep($time);
}
public static function additionProviderTextMarkdown(): array
{
$at = [
'atMobiles' => [],
'atUserIds' => [ 'jetsung' ],
'isAtAll' => true,
];
$at2 = array_merge($at, [ 'isAtAll' => false ]);
return [
[DingtalkMessage::TYPE_TEXT, 'Pusher通知TEXT 消息内容', '', $at],
[DingtalkMessage::TYPE_TEXT, 'Pusher通知TEXT 消息内容 no at all', '', $at2],
[DingtalkMessage::TYPE_MARKDOWN, "#### Pusher通知杭州天气 @150XXXXXXXX \n > 9度,西北风1级,空气良89,相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n", '首屏会话透出的展示内容', $at],
];
}
public static function additionProviderActionCard(): array
{
$content = "![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)
### 乔布斯 20 年前想打造的苹果咖啡厅
Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划";
$title = 'Pusher通知乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身';
$btns = [
[ 'title' => '百度网址', 'actionURL' => 'https://www.baidu.com' ],
[ 'title' => '头条网址', 'actionURL' => 'https://www.toutiao.com' ],
[ 'title' => '腾讯网址', 'actionURL' => 'https://www.tencent.com' ],
];
return [
[ $content, $title, '0', '查看项目', 'https://github.com/idev-sig/pusher' ],
[ $content, $title, '1', '查看项目', 'https://github.com/idev-sig/pusher' ],
[ $content, $title, '0', '', '', $btns ],
[ $content, $title, '1', '', '', $btns ],
];
}
/**
* @dataProvider additionProviderTextMarkdown
*
* @return void
*/
public function testTextMarkdownCases(string $msgtype, string $content, string $title = '', array $at = []): void
{
$this->skipTest(__METHOD__);
$this->timeSleep(20);
$channel = new Dingtalk();
$channel->setSecret($this->secret)
->setToken($this->token);
$message = new DingtalkMessage($msgtype, $content, $title);
$message->setAtMobiles($at['atMobiles'])
->setAtUserIds($at['atUserIds'])
->setIsAll($at['isAtAll']);
$channel->request($message);
$this->assertTrue($channel->getStatus());
}
public function testLinkCases(): void
{
$this->skipTest(__METHOD__);
$this->timeSleep(20);
$channel = new Dingtalk();
$channel->setSecret($this->secret)
->setToken($this->token);
$message = new DingtalkMessage(DingtalkMessage::TYPE_LINK, '这个即将发布的新版本,创始人xx称它为红树林。而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是红树林', '时代的火车向前开');
$message->setPicUrl('https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png')
->setMessageUrl('https://www.aliyun.com');
$channel->request($message);
$this->assertTrue($channel->getStatus());
}
public function testFeedCardCases(): void
{
$this->skipTest(__METHOD__);
$this->timeSleep(20);
$channel = new Dingtalk();
$channel->setSecret($this->secret)
->setToken($this->token);
$links = [
[
'title' => '百度搜索',
'messageURL' => 'https://baidu.com',
'picURL' => 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png',
],
[
'title' => '360搜索',
'messageURL' => 'https://so.com',
'picURL' => 'https://p.ssl.qhimg.com/t012cdb572f41b93733.png',
],
];
$message = new DingtalkMessage(DingtalkMessage::TYPE_FEED_CARD);
$message->setLinks($links)
->addLink('跳转到项目地址', 'https://github.com/idev-sig/pusher', 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png');
$channel->request($message);
$this->assertTrue($channel->getStatus());
}
/**
* @dataProvider additionProviderActionCard
*
* @return void
*/
public function testActionCardCases(
string $content,
string $title,
string $btnOrientation,
string $singleTitle,
string $singleURL,
array $btns = [],
): void {
$this->skipTest(__METHOD__);
$this->timeSleep(20);
$channel = new Dingtalk();
$channel->setSecret($this->secret)
->setToken($this->token);
$message = new DingtalkMessage(DingtalkMessage::TYPE_ACTION_CARD, $content, $title);
$message->setBtnOrientation($btnOrientation);
if (count($btns) > 0) {
$message->setBtns($btns)
->addBtn('项目地址', 'https://github.com/idev-sig/pusher');
} else {
$message->setSingleTitle($singleTitle)
->setSingleURL($singleURL);
}
$channel->request($message);
$this->assertTrue($channel->getStatus());
}
}