-
-
Notifications
You must be signed in to change notification settings - Fork 958
Expand file tree
/
Copy pathTelegramTest.php
More file actions
261 lines (218 loc) · 8.48 KB
/
TelegramTest.php
File metadata and controls
261 lines (218 loc) · 8.48 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Tests\Unit;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use Dummy\AdminCommands\DummyAdminCommand;
use Dummy\SystemCommands\DummySystemCommand;
use Dummy\UserCommands\DummyUserCommand;
use Longman\TelegramBot\Commands\UserCommands\StartCommand;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\TelegramLog;
/**
* @link https://github.com/php-telegram-bot/core
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @package TelegramTest
*/
class TelegramTest extends TestCase
{
use ArraySubsetAsserts;
/**
* @var Telegram
*/
private $telegram;
/**
* @var array A few dummy custom commands paths
*/
private $custom_commands_paths = [
'/tmp/php-telegram-bot-custom-commands-1',
'/tmp/php-telegram-bot-custom-commands-2',
'/tmp/php-telegram-bot-custom-commands-3',
];
protected function setUp(): void
{
$this->telegram = new Telegram(self::$dummy_api_key, 'testbot');
// Create a few dummy custom commands paths.
foreach ($this->custom_commands_paths as $custom_path) {
mkdir($custom_path);
}
}
protected function tearDown(): void
{
// Clean up the custom commands paths.
foreach ($this->custom_commands_paths as $custom_path) {
rmdir($custom_path);
}
}
public function testNewInstanceWithoutApiKeyParam(): void
{
$this->expectException(TelegramException::class);
$this->expectExceptionMessage('API KEY not defined!');
new Telegram('');
}
public function testNewInstanceWithInvalidApiKeyParam(): void
{
$this->expectException(TelegramException::class);
$this->expectExceptionMessage('Invalid API KEY defined!');
new Telegram('invalid-api-key-format');
}
public function testGetApiKey(): void
{
self::assertEquals(self::$dummy_api_key, $this->telegram->getApiKey());
}
public function testGetBotUsername(): void
{
self::assertEquals('testbot', $this->telegram->getBotUsername());
}
public function testEnableAdmins(): void
{
$tg = $this->telegram;
self::assertEmpty($tg->getAdminList());
// Single
$tg->enableAdmin(1);
self::assertCount(1, $tg->getAdminList());
// Multiple
$tg->enableAdmins([2, 3]);
self::assertCount(3, $tg->getAdminList());
// Already added
$tg->enableAdmin(2);
self::assertCount(3, $tg->getAdminList());
}
public function testAddCustomCommandsPaths(): void
{
$tg = $this->telegram;
self::assertCount(1, $tg->getCommandsPaths());
$tg->addCommandsPath($this->custom_commands_paths[0]);
self::assertCount(2, $tg->getCommandsPaths());
self::assertArraySubset(
[$this->custom_commands_paths[0]],
$tg->getCommandsPaths()
);
$tg->addCommandsPath('/invalid/path');
self::assertCount(2, $tg->getCommandsPaths());
$tg->addCommandsPaths([
$this->custom_commands_paths[1],
$this->custom_commands_paths[2],
]);
self::assertCount(4, $tg->getCommandsPaths());
self::assertArraySubset(
array_reverse($this->custom_commands_paths),
$tg->getCommandsPaths()
);
$tg->addCommandsPath($this->custom_commands_paths[0]);
self::assertCount(4, $tg->getCommandsPaths());
}
public function testAddCustomCommandsClass(): void
{
$tg = $this->telegram;
// Require dummy commands to test with
require_once __DIR__ . '/Commands/CustomTestCommands/DummySystemCommand.php';
require_once __DIR__ . '/Commands/CustomTestCommands/DummyAdminCommand.php';
require_once __DIR__ . '/Commands/CustomTestCommands/DummyUserCommand.php';
// Test for base arrays (System, Admin, User)
self::assertCount(3, $tg->getCommandClasses());
// Test for invalid command classes
try {
$tg->addCommandClass('');
} catch (\InvalidArgumentException $ex) {
}
self::assertEmpty(array_filter($tg->getCommandClasses()));
try {
$tg->addCommandClass('not\exist\Class');
} catch (\InvalidArgumentException $ex) {
}
self::assertEmpty(array_filter($tg->getCommandClasses()));
// Add valid command classes
$tg->addCommandClass(DummySystemCommand::class);
$tg->addCommandClasses([
DummyAdminCommand::class,
DummyUserCommand::class,
]);
$command_classes = $tg->getCommandClasses();
self::assertSame(['dummy_system' => 'Dummy\SystemCommands\DummySystemCommand'], $command_classes['System']);
self::assertSame(['dummy_admin' => 'Dummy\AdminCommands\DummyAdminCommand'], $command_classes['Admin']);
self::assertSame(['dummy_user' => 'Dummy\UserCommands\DummyUserCommand'], $command_classes['User']);
}
public function testSettingDownloadUploadPaths(): void
{
self::assertEmpty($this->telegram->getDownloadPath());
self::assertEmpty($this->telegram->getUploadPath());
$this->telegram->setDownloadPath('/down/below');
$this->telegram->setUploadPath('/up/above');
self::assertSame('/down/below', $this->telegram->getDownloadPath());
self::assertSame('/up/above', $this->telegram->getUploadPath());
}
public function testGetCommandsList(): void
{
$commands = $this->telegram->getCommandsList();
self::assertIsArray($commands);
self::assertNotCount(0, $commands);
}
public function testGetCommandClass(): void
{
$className = StartCommand::class;
$commands = $this->telegram->getCommandClasses();
self::assertIsArray($commands);
self::assertCount(3, $commands);
$class = $this->telegram->getCommandClassName('user', 'notexist');
self::assertNull($class);
$this->telegram->addCommandClass($className);
$class = $this->telegram->getCommandClassName('user', 'start');
self::assertNotNull($class);
self::assertSame($className, $class);
}
public function testUpdateFilter(): void
{
$rawUpdate = '{
"update_id": 513400512,
"message": {
"message_id": 3,
"from": {
"id": 313534466,
"first_name": "first",
"last_name": "last",
"username": "username"
},
"chat": {
"id": 313534466,
"first_name": "first",
"last_name": "last",
"username": "username",
"type": "private"
},
"date": 1499402829,
"text": "hi"
}
}';
$debug_log_file = '/tmp/php-telegram-bot-update-filter-debug.log';
TelegramLog::initialize(
new \Monolog\Logger('bot_log', [
(new \Monolog\Handler\StreamHandler($debug_log_file, \Monolog\Logger::DEBUG))->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true)),
])
);
$update = new Update(json_decode($rawUpdate, true), $this->telegram->getBotUsername());
$this->telegram->setUpdateFilter(function (Update $update, Telegram $telegram, &$reason) {
if ($update->getMessage()->getChat()->getId() === 313534466) {
$reason = 'Invalid user, update denied.';
return false;
}
return true;
});
$response = $this->telegram->processUpdate($update);
self::assertFalse($response->isOk());
// Check that the reason is written to the debug log.
$debug_log = file_get_contents($debug_log_file);
self::assertStringContainsString('Invalid user, update denied.', $debug_log);
file_exists($debug_log_file) && unlink($debug_log_file);
}
}