-
-
Notifications
You must be signed in to change notification settings - Fork 963
/
Copy pathTestHelpers.php
251 lines (230 loc) · 7.65 KB
/
TestHelpers.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
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
<?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 Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\User;
use Longman\TelegramBot\Exception\TelegramException;
/**
* @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 TestHelpers
{
/**
* Data template of a user.
*
* @var array
*/
protected static $user_template = [
'id' => 1,
'first_name' => 'first',
'last_name' => 'last',
'username' => 'user',
];
/**
* Data template of a chat.
*
* @var array
*/
protected static $chat_template = [
'id' => 1,
'first_name' => 'first',
'last_name' => 'last',
'username' => 'name',
'type' => 'private',
'all_members_are_administrators' => false,
];
/**
* Set the value of a private/protected property of an object
*
* @param object $object Object that contains the property
* @param string $property Name of the property who's value we want to set
* @param mixed $value The value to set to the property
*
* @throws \ReflectionException
*/
public static function setObjectProperty(object $object, string $property, $value): void
{
$ref_object = new \ReflectionObject($object);
$ref_property = $ref_object->getProperty($property);
$ref_property->setAccessible(true);
$ref_property->setValue($object, $value);
}
/**
* Set the value of a private/protected static property of a class
*
* @param string $class Class that contains the static property
* @param string $property Name of the property who's value we want to set
* @param mixed $value The value to set to the property
*
* @throws \ReflectionException
*/
public static function setStaticProperty(string $class, string $property, $value): void
{
$ref_property = new \ReflectionProperty($class, $property);
$ref_property->setAccessible(true);
$ref_property->setValue(null, $value);
}
/**
* Return a simple fake Update object
*
* @param array $data Pass custom data array if needed
*
* @return Update
*/
public static function getFakeUpdateObject(array $data = []): Update
{
$data = $data ?: [
'update_id' => mt_rand(),
'message' => [
'message_id' => mt_rand(),
'chat' => [
'id' => mt_rand(),
],
'date' => time(),
],
];
return new Update($data, 'testbot');
}
/**
* Return a fake command object for the passed command text
*
* @param string $command_text
*
* @return Update
*/
public static function getFakeUpdateCommandObject(string $command_text): Update
{
$data = [
'update_id' => mt_rand(),
'message' => [
'message_id' => mt_rand(),
'from' => self::$user_template,
'chat' => self::$chat_template,
'date' => time(),
'text' => $command_text,
],
];
return self::getFakeUpdateObject($data);
}
/**
* Return a fake user object.
*
* @param array $data Pass custom data array if needed
*
* @return User
*/
public static function getFakeUserObject(array $data = []): User
{
($data === null) && $data = [];
return new User($data + self::$user_template);
}
/**
* Return a fake chat object.
*
* @param array $data Pass custom data array if needed
*
* @return Chat
*/
public static function getFakeChatObject(array $data = []): Chat
{
return new Chat($data + self::$chat_template);
}
/**
* Get fake recorded audio track
*
* @return array
*/
public static function getFakeRecordedAudio(): array
{
$mime_type = ['audio/ogg', 'audio/mpeg', 'audio/vnd.wave', 'audio/x-ms-wma', 'audio/basic'];
return [
'file_id' => mt_rand(1, 999),
'duration' => mt_rand(1, 99) . ':' . mt_rand(1, 60),
'performer' => 'phpunit',
'title' => 'track from phpunit',
'mime_type' => $mime_type[array_rand($mime_type, 1)],
'file_size' => mt_rand(1, 99999),
];
}
/**
* Return a fake message object using the passed ids.
*
* @param array $message_data Pass custom message data array if needed
* @param array $user_data Pass custom user data array if needed
* @param array $chat_data Pass custom chat data array if needed
*
* @return Message
*/
public static function getFakeMessageObject(array $message_data = [], array $user_data = [], array $chat_data = []): Message
{
return new Message($message_data + [
'message_id' => mt_rand(),
'from' => $user_data + self::$user_template,
'chat' => $chat_data + self::$chat_template,
'date' => time(),
'text' => 'dummy',
], 'testbot');
}
/**
* Start a fake conversation for the passed command and return the randomly generated ids.
*
* @return array|false
*/
public static function startFakeConversation()
{
if (!DB::isDbConnected()) {
return false;
}
//Just get some random values.
$message_id = mt_rand();
$user_id = mt_rand();
$chat_id = mt_rand();
try {
//Make sure we have a valid user and chat available.
$message = self::getFakeMessageObject(['message_id' => $message_id], ['id' => $user_id], ['id' => $chat_id]);
DB::insertMessageRequest($message);
DB::insertUser($message->getFrom(), null, $message->getChat());
return compact('message_id', 'user_id', 'chat_id');
} catch (TelegramException $e) {
return false;
}
}
/**
* Empty all tables for the passed database
*
* @param array $credentials
*/
public static function emptyDb(array $credentials): void
{
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database'];
if (!empty($credentials['port'])) {
$dsn .= ';port=' . $credentials['port'];
}
$options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'];
$pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options);
$pdo->prepare('
DELETE FROM `conversation`;
DELETE FROM `telegram_update`;
DELETE FROM `chosen_inline_result`;
DELETE FROM `inline_query`;
DELETE FROM `message`;
DELETE FROM `user_chat`;
DELETE FROM `chat`;
DELETE FROM `user`;
')->execute();
}
}