-
-
Notifications
You must be signed in to change notification settings - Fork 964
/
Copy pathKeyboardTest.php
208 lines (173 loc) · 7.86 KB
/
KeyboardTest.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
<?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\Entities;
use Longman\TelegramBot\Entities\Keyboard;
use Longman\TelegramBot\Entities\KeyboardButton;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Tests\Unit\TestCase;
/**
* @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 KeyboardTest extends TestCase
{
public function testKeyboardDataMalformedField(): void
{
$this->expectException(TelegramException::class);
$this->expectExceptionMessage('keyboard field is not an array!');
new Keyboard(['keyboard' => 'wrong']);
}
public function testKeyboardDataMalformedSubfield(): void
{
$this->expectException(TelegramException::class);
$this->expectExceptionMessage('keyboard subfield is not an array!');
new Keyboard(['keyboard' => ['wrong']]);
}
public function testKeyboardSingleButtonSingleRow(): void
{
$keyboard = (new Keyboard('Button Text 1'))->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
$keyboard = (new Keyboard(['Button Text 2']))->getProperty('keyboard');
self::assertSame('Button Text 2', $keyboard[0][0]->getText());
}
public function testKeyboardSingleButtonMultipleRows(): void
{
$keyboard = (new Keyboard(
'Button Text 1',
'Button Text 2',
'Button Text 3'
))->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
self::assertSame('Button Text 2', $keyboard[1][0]->getText());
self::assertSame('Button Text 3', $keyboard[2][0]->getText());
$keyboard = (new Keyboard(
['Button Text 4'],
['Button Text 5'],
['Button Text 6']
))->getProperty('keyboard');
self::assertSame('Button Text 4', $keyboard[0][0]->getText());
self::assertSame('Button Text 5', $keyboard[1][0]->getText());
self::assertSame('Button Text 6', $keyboard[2][0]->getText());
}
public function testKeyboardMultipleButtonsSingleRow(): void
{
$keyboard = (new Keyboard(['Button Text 1', 'Button Text 2']))->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
self::assertSame('Button Text 2', $keyboard[0][1]->getText());
}
public function testKeyboardMultipleButtonsMultipleRows(): void
{
$keyboard = (new Keyboard(
['Button Text 1', 'Button Text 2'],
['Button Text 3', 'Button Text 4']
))->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
self::assertSame('Button Text 2', $keyboard[0][1]->getText());
self::assertSame('Button Text 3', $keyboard[1][0]->getText());
self::assertSame('Button Text 4', $keyboard[1][1]->getText());
}
public function testKeyboardWithButtonObjects(): void
{
$keyboard = (new Keyboard(
new KeyboardButton('Button Text 1')
))->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
$keyboard = (new Keyboard(
new KeyboardButton('Button Text 2'),
new KeyboardButton('Button Text 3')
))->getProperty('keyboard');
self::assertSame('Button Text 2', $keyboard[0][0]->getText());
self::assertSame('Button Text 3', $keyboard[1][0]->getText());
$keyboard = (new Keyboard(
[new KeyboardButton('Button Text 4')],
[new KeyboardButton('Button Text 5'), new KeyboardButton('Button Text 6')]
))->getProperty('keyboard');
self::assertSame('Button Text 4', $keyboard[0][0]->getText());
self::assertSame('Button Text 5', $keyboard[1][0]->getText());
self::assertSame('Button Text 6', $keyboard[1][1]->getText());
}
public function testKeyboardWithDataArray(): void
{
$resize_keyboard = (bool) mt_rand(0, 1);
$one_time_keyboard = (bool) mt_rand(0, 1);
$input_field_placeholder = 'placeholder';
$selective = (bool) mt_rand(0, 1);
$keyboard_obj = new Keyboard([
'resize_keyboard' => $resize_keyboard,
'one_time_keyboard' => $one_time_keyboard,
'input_field_placeholder' => $input_field_placeholder,
'selective' => $selective,
'keyboard' => [['Button Text 1']],
]);
$keyboard = $keyboard_obj->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
self::assertSame($resize_keyboard, $keyboard_obj->getResizeKeyboard());
self::assertSame($one_time_keyboard, $keyboard_obj->getOneTimeKeyboard());
self::assertSame($input_field_placeholder, $keyboard_obj->getInputFieldPlaceholder());
self::assertSame($selective, $keyboard_obj->getSelective());
}
public function testPredefinedKeyboards(): void
{
$keyboard_remove = Keyboard::remove();
self::assertTrue($keyboard_remove->getProperty('remove_keyboard'));
$keyboard_force_reply = Keyboard::forceReply();
self::assertTrue($keyboard_force_reply->getProperty('force_reply'));
}
public function testKeyboardMethods(): void
{
$keyboard_obj = new Keyboard([]);
self::assertEmpty($keyboard_obj->getOneTimeKeyboard());
self::assertEmpty($keyboard_obj->getResizeKeyboard());
self::assertEmpty($keyboard_obj->getSelective());
$keyboard_obj->setOneTimeKeyboard(true);
self::assertTrue($keyboard_obj->getOneTimeKeyboard());
$keyboard_obj->setOneTimeKeyboard(false);
self::assertFalse($keyboard_obj->getOneTimeKeyboard());
$keyboard_obj->setResizeKeyboard(true);
self::assertTrue($keyboard_obj->getResizeKeyboard());
$keyboard_obj->setResizeKeyboard(false);
self::assertFalse($keyboard_obj->getResizeKeyboard());
$keyboard_obj->setSelective(true);
self::assertTrue($keyboard_obj->getSelective());
$keyboard_obj->setSelective(false);
self::assertFalse($keyboard_obj->getSelective());
}
public function testKeyboardAddRows(): void
{
$keyboard_obj = new Keyboard([]);
$keyboard_obj->addRow('Button Text 1');
$keyboard = $keyboard_obj->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
$keyboard_obj->addRow('Button Text 2', 'Button Text 3');
$keyboard = $keyboard_obj->getProperty('keyboard');
self::assertSame('Button Text 2', $keyboard[1][0]->getText());
self::assertSame('Button Text 3', $keyboard[1][1]->getText());
$keyboard_obj->addRow(['text' => 'Button Text 4']);
$keyboard = $keyboard_obj->getProperty('keyboard');
self::assertSame('Button Text 4', $keyboard[2][0]->getText());
}
public function testSetterMethods(): void
{
$keyboard = (new Keyboard(
[
['text' => 'One'],
]
))->setResizeKeyboard(true);
$array = json_decode($keyboard->toJson(), true);
$this->assertIsArray($array);
$this->assertArrayHasKey('keyboard', $array);
$this->assertArrayHasKey('resize_keyboard', $array);
$this->assertIsArray($array['keyboard']);
$this->assertEquals(true, $array['resize_keyboard']);
}
}