KeyboardTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
eloc 55
c 3
b 0
f 0
dl 0
loc 108
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create_a_keyboard() 0 14 1
A test_send_message_with_keyboard() 0 31 1
A test_the_result() 0 19 1
A test_create_for_answer_query() 0 16 1
A create_a_inline_keyboard() 0 13 1
1
<?php
2
3
namespace TelegramBotTest;
4
5
use TelegramBot\Entities\InlineKeyboard;
6
use TelegramBot\Entities\InlineKeyboardButton;
7
use TelegramBot\Entities\Keyboard;
8
use TelegramBot\Entities\KeyboardButton;
9
use TelegramBot\Request;
10
11
class KeyboardTest extends \PHPUnit\Framework\TestCase
12
{
13
14
    public function test_the_result(): void
15
    {
16
        $raw_data = $this->create_a_inline_keyboard();
17
18
        $this->assertEquals(true, $raw_data['one_time_keyboard']);
19
        $this->assertEquals(true, $raw_data['selective']);
20
21
        $keyboard = json_encode([
22
            [
23
                ['text' => 'Button 1', 'web_app' => ['url' => 'https://google.com']],
24
                ['text' => 'Button 2', 'url' => 'https://google.com'],
25
            ],
26
            [
27
                ['text' => 'Button 3', 'callback_data' => 'callback_data'],
28
                ['text' => 'Button 4', 'callback_data' => 'callback_data'],
29
            ],
30
        ]);
31
32
        $this->assertEquals($keyboard, json_encode($raw_data['inline_keyboard']));
33
    }
34
35
    public function create_a_inline_keyboard(): array
36
    {
37
        return InlineKeyboard::make()
38
            ->setOneTimeKeyboard(true)
39
            ->setSelective(true)
40
            ->setKeyboard([
41
                [
42
                    InlineKeyboardButton::make('Button 1')->setWebApp('https://google.com'),
43
                    InlineKeyboardButton::make('Button 2')->setUrl('https://google.com'),
44
                ],
45
                [
46
                    InlineKeyboardButton::make('Button 3')->setCallbackData('callback_data'),
47
                    InlineKeyboardButton::make('Button 4')->setCallbackData('callback_data'),
48
                ],
49
            ]);
50
    }
51
52
    public function test_send_message_with_keyboard(): void
53
    {
54
        $raw_data = $this->create_a_keyboard();
55
56
        $result = Request::create('sendMessage', [
57
            'chat_id' => 'chat_id',
58
            'text' => 'text',
59
            'parse_mode' => 'Markdown',
60
            'reply_markup' => $raw_data,
61
        ]);
62
63
        $keyboard = json_decode($result['options']['query']['reply_markup'], true);
64
65
        $this->assertEquals(true, $keyboard['resize_keyboard']);
66
67
        $this->assertEquals(true, $keyboard['one_time_keyboard']);
68
69
        $this->assertEquals(true, $keyboard['selective']);
70
71
        $compare_with = json_encode([
72
            [
73
                ['text' => 'Button 1', 'web_app' => ['url' => 'https://google.com']],
74
                ['text' => 'Button 2'],
75
            ],
76
            [
77
                ['text' => 'Button 3'],
78
                ['text' => 'Button 4'],
79
            ],
80
        ]);
81
82
        $this->assertEquals($compare_with, json_encode($keyboard['keyboard']));
83
    }
84
85
    public function create_a_keyboard(): array
86
    {
87
        return Keyboard::make()
88
            ->setResizeKeyboard(true)
89
            ->setOneTimeKeyboard(true)
90
            ->setSelective(true)
91
            ->setKeyboard([
92
                [
93
                    KeyboardButton::make('Button 1')->setWebApp('https://google.com'),
94
                    KeyboardButton::make('Button 2'),
95
                ],
96
                [
97
                    KeyboardButton::make('Button 3'),
98
                    KeyboardButton::make('Button 4'),
99
                ],
100
            ]);
101
    }
102
103
    public function test_create_for_answer_query(): void
104
    {
105
        $raw_data = $this->create_a_inline_keyboard();
106
        $result = Request::create('answerCallbackQuery', [
107
            'callback_query_id' => 'callback_query_id',
108
            'text' => 'text',
109
            'show_alert' => true,
110
            'url' => 'https://google.com',
111
            'cache_time' => 10,
112
            'reply_markup' => $raw_data,
113
        ]);
114
115
        $this->assertEquals(true, $result['options']['query']['show_alert']);
116
        $this->assertEquals('https://google.com', $result['options']['query']['url']);
117
        $this->assertEquals(10, $result['options']['query']['cache_time']);
118
        $this->assertEquals(json_encode($raw_data), $result['options']['query']['reply_markup']);
119
    }
120
121
}
122