From 0298049ca213fdd0a3cff8f644baf2f44a9d4975 Mon Sep 17 00:00:00 2001 From: netkas Date: Wed, 9 Oct 2024 17:50:57 -0400 Subject: [PATCH] Add keyboard markup tests and constructors to objects --- src/TgBotLib/Objects/ForceReply.php | 10 ++ src/TgBotLib/Objects/KeyboardButton.php | 142 +++++++++++++++++-- src/TgBotLib/Objects/ReplyKeyboardMarkup.php | 23 ++- src/TgBotLib/Objects/ReplyKeyboardRemove.php | 9 ++ tests/TgBotLib/Methods/SendMessageTest.php | 84 +++++++++++ 5 files changed, 255 insertions(+), 13 deletions(-) diff --git a/src/TgBotLib/Objects/ForceReply.php b/src/TgBotLib/Objects/ForceReply.php index 6147c41..da0bf7c 100644 --- a/src/TgBotLib/Objects/ForceReply.php +++ b/src/TgBotLib/Objects/ForceReply.php @@ -11,6 +11,16 @@ private ?string $inline_field_placeholder; private bool $selective; + /** + * ForceReply constructor. + */ + public function __construct() + { + $this->force_reply = false; + $this->inline_field_placeholder = null; + $this->selective = false; + } + /** * Shows reply interface to the user, as if they manually selected the bot's message and tapped 'Reply' * diff --git a/src/TgBotLib/Objects/KeyboardButton.php b/src/TgBotLib/Objects/KeyboardButton.php index 4954bd0..43cf758 100644 --- a/src/TgBotLib/Objects/KeyboardButton.php +++ b/src/TgBotLib/Objects/KeyboardButton.php @@ -15,6 +15,20 @@ private ?KeyboardButtonPollType $request_poll; private ?WebAppInfo $web_app; + /** + * KeyboardButton constructor. + */ + public function __construct() + { + $this->text = (string) null; + $this->request_user = null; + $this->request_chat = null; + $this->request_contact = false; + $this->request_location = false; + $this->request_poll = null; + $this->web_app = null; + } + /** * Text of the button. If none of the optional fields are used, it will be sent as a message when the * button is pressed @@ -26,6 +40,18 @@ return $this->text; } + /** + * Set the text of the button + * + * @param string $text + * @return KeyboardButton + */ + public function setText(string $text): KeyboardButton + { + $this->text = $text; + return $this; + } + /** * Optional. If specified, pressing the button will open a list of suitable users. Tapping on any user will * send their identifier to the bot in a “user_shared” service message. Available in private chats only. @@ -37,6 +63,18 @@ return $this->request_user; } + /** + * Set the request user + * + * @param KeyboardButtonRequestUser $request_user + * @return KeyboardButton + */ + public function setRequestUser(KeyboardButtonRequestUser $request_user): KeyboardButton + { + $this->request_user = $request_user; + return $this; + } + /** * Optional. If specified, pressing the button will open a list of suitable chats. Tapping on a chat will send * its identifier to the bot in a “chat_shared” service message. Available in private chats only. @@ -48,6 +86,18 @@ return $this->request_chat; } + /** + * Set the request chat + * + * @param KeyboardButtonRequestChat $request_chat + * @return KeyboardButton + */ + public function setRequestChat(KeyboardButtonRequestChat $request_chat): KeyboardButton + { + $this->request_chat = $request_chat; + return $this; + } + /** * Optional. If True, the user's phone number will be sent as a contact when the button is pressed. * Available in private chats only. @@ -59,6 +109,18 @@ return $this->request_contact; } + /** + * Set the request contact + * + * @param bool $request_contact + * @return KeyboardButton + */ + public function setRequestContact(bool $request_contact): KeyboardButton + { + $this->request_contact = $request_contact; + return $this; + } + /** * Optional. If True, the user's current location will be sent when the button is pressed. * Available in private chats only. @@ -70,6 +132,18 @@ return $this->request_location; } + /** + * Set the request location + * + * @param bool $request_location + * @return KeyboardButton + */ + public function setRequestLocation(bool $request_location): KeyboardButton + { + $this->request_location = $request_location; + return $this; + } + /** * Optional. If specified, the user will be asked to create a poll and send it to the bot when the button * is pressed. Available in private chats only. @@ -81,6 +155,18 @@ return $this->request_poll; } + /** + * Set the request poll + * + * @param KeyboardButtonPollType $request_poll + * @return KeyboardButton + */ + public function setRequestPoll(KeyboardButtonPollType $request_poll): KeyboardButton + { + $this->request_poll = $request_poll; + return $this; + } + /** * Optional. If specified, the described Web App will be launched when the button is pressed. The * Web App will be able to send a “web_app_data” service message. Available in private chats only. @@ -93,6 +179,18 @@ return $this->web_app; } + /** + * Set the web app + * + * @param WebAppInfo $web_app + * @return KeyboardButton + */ + public function setWebApp(WebAppInfo $web_app): KeyboardButton + { + $this->web_app = $web_app; + return $this; + } + /** * Returns an array representation of the object * @@ -100,15 +198,41 @@ */ public function toArray(): array { - return [ + $array = [ 'text' => $this->text, - 'request_user' => $this->request_user?->toArray(), - 'request_chat' => $this->request_chat?->toArray(), - 'request_contact' => $this->request_contact, - 'request_location' => $this->request_location, - 'request_poll' => $this->request_poll?->toArray(), - 'web_app' => $this->web_app?->toArray(), ]; + + if($this->request_user !== null) + { + $array['request_user'] = $this->request_user->toArray(); + } + + if($this->request_chat !== null) + { + $array['request_chat'] = $this->request_chat->toArray(); + } + + if($this->request_contact !== false) + { + $array['request_contact'] = $this->request_contact; + } + + if($this->request_location !== false) + { + $array['request_location'] = $this->request_location; + } + + if($this->request_poll !== null) + { + $array['request_poll'] = $this->request_poll->toArray(); + } + + if($this->web_app !== null) + { + $array['web_app'] = $this->web_app->toArray(); + } + + return $array; } /** @@ -125,8 +249,8 @@ $object->text = $data['text'] ?? null; $object->request_user = isset($data['request_user']) ? KeyboardButtonRequestUser::fromArray($data['request_user']) : null; $object->request_chat = isset($data['request_chat']) ? KeyboardButtonRequestChat::fromArray($data['request_chat']) : null; - $object->request_contact = $data['request_contact'] ?? null; - $object->request_location = $data['request_location'] ?? null; + $object->request_contact = $data['request_contact'] ?? false; + $object->request_location = $data['request_location'] ?? false; $object->request_poll = isset($data['request_poll']) ? KeyboardButtonPollType::fromArray($data['request_poll']) : null; $object->web_app = isset($data['web_app']) ? WebAppInfo::fromArray($data['web_app']) : null; diff --git a/src/TgBotLib/Objects/ReplyKeyboardMarkup.php b/src/TgBotLib/Objects/ReplyKeyboardMarkup.php index c8a792c..10b7ce4 100644 --- a/src/TgBotLib/Objects/ReplyKeyboardMarkup.php +++ b/src/TgBotLib/Objects/ReplyKeyboardMarkup.php @@ -236,18 +236,26 @@ public function toArray(): array { $array = [ - 'keyboard' => $this->keyboard, 'is_persistent' => $this->is_persistent, 'resize_keyboard' => $this->resize_keyboard, 'one_time_keyboard' => $this->one_time_keyboard, + 'input_field_placeholder' => $this->input_field_placeholder, 'selective' => $this->selective ]; - if($this->input_field_placeholder !== null) + $keyboard = []; + foreach($this->keyboard as $row) { - $array['input_field_placeholder'] = $this->input_field_placeholder; + $buttonRow = []; + foreach($row as $button) + { + $buttonRow[] = $button->toArray(); + } + + $keyboard[] = $buttonRow; } + $array['keyboard'] = $keyboard; return $array; } @@ -261,8 +269,15 @@ $object->keyboard = []; foreach($data['keyboard'] as $keyboard) { - $object->keyboard[] = KeyboardButton::fromArray($keyboard); + $buttons = []; + foreach($keyboard as $button) + { + $buttons[] = KeyboardButton::fromArray($button); + } + + $object->addRow(...$buttons); } + $object->is_persistent = $data['is_persistent'] ?? false; $object->resize_keyboard = $data['resize_keyboard'] ?? false; $object->one_time_keyboard = $data['one_time_keyboard'] ?? false; diff --git a/src/TgBotLib/Objects/ReplyKeyboardRemove.php b/src/TgBotLib/Objects/ReplyKeyboardRemove.php index 257e5f0..c782bd0 100644 --- a/src/TgBotLib/Objects/ReplyKeyboardRemove.php +++ b/src/TgBotLib/Objects/ReplyKeyboardRemove.php @@ -10,6 +10,15 @@ private bool $remove_keyboard; private bool $selective; + /** + * ReplyKeyboardRemove constructor. + */ + public function __construct() + { + $this->remove_keyboard = false; + $this->selective = false; + } + /** * Requests clients to remove the custom keyboard (user will not be able to summon this keyboard; if you want * to hide the keyboard from sight but keep it accessible, use one_time_keyboard in ReplyKeyboardMarkup) diff --git a/tests/TgBotLib/Methods/SendMessageTest.php b/tests/TgBotLib/Methods/SendMessageTest.php index 73b67e1..36281c2 100644 --- a/tests/TgBotLib/Methods/SendMessageTest.php +++ b/tests/TgBotLib/Methods/SendMessageTest.php @@ -5,8 +5,14 @@ namespace TgBotLib\Methods; use PHPUnit\Framework\TestCase; use TgBotLib\Bot; use TgBotLib\Enums\Types\ParseMode; +use TgBotLib\Objects\ForceReply; +use TgBotLib\Objects\InlineKeyboardButton; +use TgBotLib\Objects\InlineKeyboardMarkup; +use TgBotLib\Objects\KeyboardButton; use TgBotLib\Objects\LinkPreviewOptions; use TgBotLib\Objects\Message; +use TgBotLib\Objects\ReplyKeyboardMarkup; +use TgBotLib\Objects\ReplyKeyboardRemove; class SendMessageTest extends TestCase { @@ -196,4 +202,82 @@ class SendMessageTest extends TestCase $this->assertInstanceOf(Message::class, $result); $this->assertEquals(TEST_CHAT_ID, $result->getChat()->getId()); } + + public function testKeyboardMarkup(): void + { + $replyMarkup = new InlineKeyboardMarkup(); + $replyMarkup->addRow( + (new InlineKeyboardButton())->setText('Button 1')->setCallbackData('button1'), + (new InlineKeyboardButton())->setText('Button 2')->setCallbackData('button2') + ); + + $result = self::$bot->sendMessage( + chat_id: TEST_CHAT_ID, + text: 'Test Unit: testKeyboardMarkup', + reply_markup: $replyMarkup + ); + + $this->assertInstanceOf(Message::class, $result); + $this->assertEquals(TEST_CHAT_ID, $result->getChat()->getId()); + } + + /** + * Tests the `sendMessage` functionality of the bot with a reply markup. + * The message includes a reply markup with two buttons, and a placeholder text for the input field. + * + * @return void + */ + public function testReplyMarkup(): void + { + $result = self::$bot->sendMessage( + chat_id: TEST_CHAT_ID, + text: 'Test Unit: testReplyMarkup', + reply_markup: (new ReplyKeyboardMarkup())->addRow( + (new KeyboardButton())->setText('Button 1'), + (new KeyboardButton())->setText('Button 2') + )->setInputFieldPlaceholder('Placeholder') + ); + + $this->assertInstanceOf(Message::class, $result); + $this->assertEquals(TEST_CHAT_ID, $result->getChat()->getId()); + + // Remove + $result = self::$bot->sendMessage( + chat_id: TEST_CHAT_ID, + text: 'Test Unit: testReplyMarkup', + reply_markup: (new ReplyKeyboardRemove())->setRemoveKeyboard(true) + ); + + $this->assertInstanceOf(Message::class, $result); + $this->assertEquals(TEST_CHAT_ID, $result->getChat()->getId()); + } + + /** + * Tests the `sendMessage` functionality of the bot with a force reply. + * + * The message includes a force reply option that requires a reply from the user. + * + * @return void + */ + public function testForceReply(): void + { + $result = self::$bot->sendMessage( + chat_id: TEST_CHAT_ID, + text: 'Test Unit: testForceReply', + reply_markup: (new ForceReply())->setForceReply(true) + ); + + $this->assertInstanceOf(Message::class, $result); + $this->assertEquals(TEST_CHAT_ID, $result->getChat()->getId()); + + // Clear + $result = self::$bot->sendMessage( + chat_id: TEST_CHAT_ID, + text: 'Test Unit: testForceReply', + reply_markup: (new ForceReply())->setForceReply(false) + ); + + $this->assertInstanceOf(Message::class, $result); + $this->assertEquals(TEST_CHAT_ID, $result->getChat()->getId()); + } }