From 7aa6be4e9add29afb0abd090c22b657beef48a08 Mon Sep 17 00:00:00 2001 From: netkas Date: Mon, 30 Sep 2024 02:34:07 -0400 Subject: [PATCH] Add unit tests and abstract bot method execution framework --- src/TgBotLib/Abstracts/Method.php | 122 +++++++++++++++++++++++++++ src/TgBotLib/Enums/Methods.php | 19 +++++ tests/TgBotLib/Methods/GetMeTest.php | 29 +++++++ 3 files changed, 170 insertions(+) create mode 100644 src/TgBotLib/Abstracts/Method.php create mode 100644 src/TgBotLib/Enums/Methods.php create mode 100644 tests/TgBotLib/Methods/GetMeTest.php diff --git a/src/TgBotLib/Abstracts/Method.php b/src/TgBotLib/Abstracts/Method.php new file mode 100644 index 0000000..261abde --- /dev/null +++ b/src/TgBotLib/Abstracts/Method.php @@ -0,0 +1,122 @@ +getEndpoint(), $bot->getToken(), $method)); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + + if($parameters === null) + { + curl_setopt($curl, CURLOPT_POST, false); + } + else + { + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters)); + } + + return $curl; + } + + /** + * Builds a cURL handle for uploading a file to the Telegram API. + * + * @param Bot $bot The bot instance used to get the endpoint. + * @param string $file_param The parameter name for the file to be uploaded. + * @param string $file_path The file path of the file to be uploaded. + * @param array $parameters Additional parameters to be included in the request. + * @return CurlHandle The cURL handle configured for the file upload. + */ + protected static function buildUpload(Bot $bot, string $method, string $file_param, string $file_path, array $parameters): CurlHandle + { + $curl = curl_init(sprintf('%s/%s?%s', $bot->getEndpoint(), $method, http_build_query($parameters))); + + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']); + curl_setopt($curl, CURLOPT_POSTFIELDS, [ + $file_param => new CURLFile($file_path) + ]); + + return $curl; + } + + protected static function buildMultiUpload(Bot $bot, string $method, array $files, array $parameters): CurlHandle + { + $curl = curl_init(sprintf('%s/%s?%s', $bot->getEndpoint(), $method, http_build_query($parameters))); + + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']); + + $post_fields = []; + foreach($files as $file_param => $file_path) + { + $post_fields[$file_param] = new CURLFile($file_path); + } + + curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields); + return $curl; + } + + /** + * Executes a cURL request and processes the response. + * + * @param CurlHandle $curl The cURL handle to be executed. + * @return array The decoded response from the cURL request. + * @throws TelegramException if the response is not a valid array, + * or if the 'ok' field is not set or is false. + */ + protected static function executeCurl(CurlHandle $curl): array + { + $response = curl_exec($curl); + curl_close($curl); + $result = json_decode($response, true); + + if(!is_array($result)) + { + throw new TelegramException('Invalid response from Telegram API'); + } + + if(!isset($result['ok'])) + { + throw new TelegramException('Invalid response from Telegram API'); + } + + if($result['ok'] === false) + { + throw new TelegramException($result['description'], (int)$result['error_code']); + } + + return $result['result']; + } +} \ No newline at end of file diff --git a/src/TgBotLib/Enums/Methods.php b/src/TgBotLib/Enums/Methods.php new file mode 100644 index 0000000..469a1b4 --- /dev/null +++ b/src/TgBotLib/Enums/Methods.php @@ -0,0 +1,19 @@ + GetMe::execute($bot, $parameters), + }; + } + } diff --git a/tests/TgBotLib/Methods/GetMeTest.php b/tests/TgBotLib/Methods/GetMeTest.php new file mode 100644 index 0000000..1c8a3d3 --- /dev/null +++ b/tests/TgBotLib/Methods/GetMeTest.php @@ -0,0 +1,29 @@ +bot = new Bot(BOT_TOKEN); + } + + /** + * This test checks if the `execute` method of the `getMe` class correctly calls `fromArray` method of User class + * and `executeCurl` method of the `getMe` class (itself), with correctly built parameters. + * + * Method `execute` should return User object constructed from the array returned by `executeCurl`. + */ + public function testExecute() + { + $user = getMe::execute($this->bot); + $this->assertInstanceOf(User::class, $user); + } +} \ No newline at end of file