From e4787b90a1b2d515be2249d751590f9f674a3457 Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 10 Oct 2024 12:21:34 -0400 Subject: [PATCH] Add sendLocation method with validation and unit tests --- src/TgBotLib/Enums/Methods.php | 3 + src/TgBotLib/Methods/SendLocation.php | 124 ++++++++++++++++++++ tests/TgBotLib/Methods/SendLocationTest.php | 112 ++++++++++++++++++ 3 files changed, 239 insertions(+) create mode 100644 src/TgBotLib/Methods/SendLocation.php create mode 100644 tests/TgBotLib/Methods/SendLocationTest.php diff --git a/src/TgBotLib/Enums/Methods.php b/src/TgBotLib/Enums/Methods.php index 52552cf..968d454 100644 --- a/src/TgBotLib/Enums/Methods.php +++ b/src/TgBotLib/Enums/Methods.php @@ -15,6 +15,7 @@ use TgBotLib\Methods\SendAnimation; use TgBotLib\Methods\SendAudio; use TgBotLib\Methods\SendDocument; + use TgBotLib\Methods\SendLocation; use TgBotLib\Methods\SendMediaGroup; use TgBotLib\Methods\SendMessage; use TgBotLib\Methods\SendPaidMedia; @@ -42,6 +43,7 @@ case SEND_VIDEO_NOTE = 'sendVideoNote'; case SEND_PAID_MEDIA = 'sendPaidMedia'; case SEND_MEDIA_GROUP = 'sendMediaGroup'; + case SEND_LOCATION = 'sendLocation'; /** * Executes a command on the provided bot with the given parameters. @@ -72,6 +74,7 @@ self::SEND_VIDEO_NOTE => SendVideoNote::execute($bot, $parameters), self::SEND_PAID_MEDIA => SendPaidMedia::execute($bot, $parameters), self::SEND_MEDIA_GROUP => SendMediaGroup::execute($bot, $parameters), + self::SEND_LOCATION => SendLocation::execute($bot, $parameters), }; } } diff --git a/src/TgBotLib/Methods/SendLocation.php b/src/TgBotLib/Methods/SendLocation.php new file mode 100644 index 0000000..4041911 --- /dev/null +++ b/src/TgBotLib/Methods/SendLocation.php @@ -0,0 +1,124 @@ + 1500) { + throw new TelegramException("Parameter 'horizontal_accuracy' must be between 0 and 1500"); + } + } + + // Validate live_period + if (isset($parameters['live_period'])) { + if (!is_int($parameters['live_period'])) { + throw new TelegramException("Parameter 'live_period' must be an integer"); + } + if ($parameters['live_period'] != 0x7FFFFFFF && + ($parameters['live_period'] < 60 || $parameters['live_period'] > 86400)) { + throw new TelegramException("Parameter 'live_period' must be between 60 and 86400, or 0x7FFFFFFF"); + } + } + + // Validate heading + if (isset($parameters['heading'])) { + if (!is_int($parameters['heading'])) { + throw new TelegramException("Parameter 'heading' must be an integer"); + } + if ($parameters['heading'] < 1 || $parameters['heading'] > 360) { + throw new TelegramException("Parameter 'heading' must be between 1 and 360"); + } + } + + // Validate proximity_alert_radius + if (isset($parameters['proximity_alert_radius'])) { + if (!is_int($parameters['proximity_alert_radius'])) { + throw new TelegramException("Parameter 'proximity_alert_radius' must be an integer"); + } + if ($parameters['proximity_alert_radius'] < 1 || $parameters['proximity_alert_radius'] > 100000) { + throw new TelegramException("Parameter 'proximity_alert_radius' must be between 1 and 100000"); + } + } + + // Handle ReplyParameters + if (isset($parameters['reply_parameters']) && $parameters['reply_parameters'] instanceof ReplyParameters) { + $parameters['reply_parameters'] = $parameters['reply_parameters']->toArray(); + } + + // Handle reply_markup + if (isset($parameters['reply_markup'])) { + if ($parameters['reply_markup'] instanceof ObjectTypeInterface) { + $parameters['reply_markup'] = json_encode($parameters['reply_markup']->toArray()); + } + } + + return Message::fromArray( + self::executeCurl( + self::buildPost($bot, Methods::SEND_LOCATION->value, $parameters) + ) + ); + } + + /** + * @inheritDoc + */ + public static function getRequiredParameters(): ?array + { + return [ + 'chat_id', + 'latitude', + 'longitude' + ]; + } + + /** + * @inheritDoc + */ + public static function getOptionalParameters(): ?array + { + return [ + 'business_connection_id', + 'message_thread_id', + 'horizontal_accuracy', + 'live_period', + 'heading', + 'proximity_alert_radius', + 'disable_notification', + 'protect_content', + 'message_effect_id', + 'reply_parameters', + 'reply_markup' + ]; + } + } \ No newline at end of file diff --git a/tests/TgBotLib/Methods/SendLocationTest.php b/tests/TgBotLib/Methods/SendLocationTest.php new file mode 100644 index 0000000..c67346c --- /dev/null +++ b/tests/TgBotLib/Methods/SendLocationTest.php @@ -0,0 +1,112 @@ +setAutoRetry(true); + } + + /** + * Tests the basic `sendLocation` functionality + * + * @return void + */ + public function testSendLocation(): void + { + $result = self::$bot->sendLocation( + chat_id: TEST_CHAT_ID, + latitude: 51.5074, + longitude: -0.1278 + ); + + $this->assertInstanceOf(Message::class, $result); + $this->assertNotNull($result->getLocation()); + $this->assertEquals(51.507394, $result->getLocation()->getLatitude()); + $this->assertEquals(-0.127813, $result->getLocation()->getLongitude()); + } + + /** + * Tests sending a live location + * + * @return void + */ + public function testSendLiveLocation(): void + { + $result = self::$bot->sendLocation( + chat_id: TEST_CHAT_ID, + latitude: 51.5074, + longitude: -0.1278, + live_period: 60, + heading: 90, + proximity_alert_radius: 100 + ); + + $this->assertInstanceOf(Message::class, $result); + $this->assertNotNull($result->getLocation()); + $this->assertTrue($result->getLocation()->getLivePeriod() > 0); + } + + /** + * Tests sending location with invalid latitude + * + * @return void + */ + public function testSendLocationInvalidLatitude(): void + { + $this->expectException(TelegramException::class); + + self::$bot->sendLocation( + chat_id: TEST_CHAT_ID, + latitude: 'invalid', + longitude: -0.1278 + ); + } + + /** + * Tests sending location with invalid heading + * + * @return void + */ + public function testSendLocationInvalidHeading(): void + { + $this->expectException(TelegramException::class); + + self::$bot->sendLocation( + chat_id: TEST_CHAT_ID, + latitude: 51.5074, + longitude: -0.1278, + heading: 361 + ); + } + + /** + * Tests sending location with invalid proximity alert radius + * + * @return void + */ + public function testSendLocationInvalidProximityAlertRadius(): void + { + $this->expectException(TelegramException::class); + + self::$bot->sendLocation( + chat_id: TEST_CHAT_ID, + latitude: 51.5074, + longitude: -0.1278, + proximity_alert_radius: 100001 + ); + } +} \ No newline at end of file