From f0afb267d5390a128c39a1048ea9f638dd0a1df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Mon, 10 Apr 2017 23:27:41 +0200 Subject: [PATCH] Rename botname to bot_username, like in core 0.42.0. Fix coding style. Add changelog. --- CHANGELOG.md | 50 +++++++++++++++++++ README.md | 10 ++-- src/Action.php | 4 +- src/BotManager.php | 17 ++++--- src/Params.php | 6 +-- tests/TelegramBotManager/Tests/ActionTest.php | 2 +- .../Tests/BotManagerTest.php | 48 +++++++++--------- tests/TelegramBotManager/Tests/ParamsTest.php | 20 ++++---- .../TelegramBotManager/Tests/TestHelpers.php | 2 +- tests/bootstrap.php | 2 +- 10 files changed, 106 insertions(+), 55 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..73b4232 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,50 @@ +# Changelog +The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). + +## [Unreleased] +### Added +- Changelog. +### Changed +- (!) Rename vital parameter `botname` to `bot_username` everywhere. +### Fixed +- Some code style issues. + +## [0.42.0] - 2017-04-10 +### Changed +- Move to PHP Telegram Bot organisation. +- Mirror version with core library. +- Update repository links. +### Fixed +- Readme formatting. + +## [0.4.0] - 2017-02-26 +### Added +- Latest Telegram Bot limiter functionality. +### Fixed +- Travis tests, using MariaDB instead of MySQL. + +## [0.3.1] - 2017-01-04 +### Fixed +- Make CLI usable again after setting up Telegram API IP address limitations. + +## [0.3.0] - 2016-12-25 +### Added +- Latest changes from PHP Telegram API bot. +### Security +- Request validation to secure the script to allow only Telegram API IPs of executing the webhook handle. + +## [0.2.1] - 2016-10-16 +### Added +- Interval between updates can be set via parameter. + +## [0.2] - 2016-09-16 +### Changed +- Force PHP7. + +## [0.1.1] - 2016-08-20 +### Fixed +- Tiny conditional fix to correct the output. + +## [0.1] - 2016-08-20 +### Added +- First minor version that contains the basic functionality. diff --git a/README.md b/README.md index 8b6a01d..94e961f 100644 --- a/README.md +++ b/README.md @@ -114,12 +114,12 @@ require __DIR__ . '/vendor/autoload.php'; try { $bot = new BotManager([ // Vitals! - 'api_key' => '12345:my_api_key', - 'botname' => 'my_own_bot', - 'secret' => 'super_secret', + 'api_key' => '12345:my_api_key', + 'bot_username' => 'my_own_bot', + 'secret' => 'super_secret', // Extras. - 'webhook' => 'https://example.com/manager.php', + 'webhook' => 'https://example.com/manager.php', ]); $bot->run(); } catch (\Exception $e) { @@ -131,7 +131,7 @@ try { The vital parameters are: - Bot API key -- Bot name +- Bot username - A secret What secret you ask? Well, this is a user-defined key that is required to execute any of the library features. diff --git a/src/Action.php b/src/Action.php index d01ffc5..2d80961 100644 --- a/src/Action.php +++ b/src/Action.php @@ -1,4 +1,4 @@ -action, (array)$actions, true); + return in_array($this->action, (array) $actions, true); } /** diff --git a/src/BotManager.php b/src/BotManager.php index 3f4e58d..aaa59f8 100644 --- a/src/BotManager.php +++ b/src/BotManager.php @@ -1,4 +1,4 @@ -telegram = new Telegram( $this->params->getBotParam('api_key'), - $this->params->getBotParam('botname') + $this->params->getBotParam('bot_username') ); } @@ -324,7 +325,7 @@ public function getLoopTime(): int return 604800; // Default to 7 days. } - return max(0, (int)$loop_time); + return max(0, (int) $loop_time); } /** @@ -341,7 +342,7 @@ public function getLoopInterval(): int } // Minimum interval is 1 second. - return max(1, (int)$interval_time); + return max(1, (int) $interval_time); } /** @@ -382,7 +383,7 @@ public function handleGetUpdates(): self $response = $this->telegram->handleGetUpdates(); if ($response->isOk()) { - $results = array_filter((array)$response->getResult()); + $results = array_filter((array) $response->getResult()); $output .= sprintf('Updates processed: %d' . PHP_EOL, count($results)); @@ -468,9 +469,9 @@ public function isValidRequest(): bool } } - $lower_dec = (float)sprintf('%u', ip2long(self::TELEGRAM_IP_LOWER)); - $upper_dec = (float)sprintf('%u', ip2long(self::TELEGRAM_IP_UPPER)); - $ip_dec = (float)sprintf('%u', ip2long($ip)); + $lower_dec = (float) sprintf('%u', ip2long(self::TELEGRAM_IP_LOWER)); + $upper_dec = (float) sprintf('%u', ip2long(self::TELEGRAM_IP_UPPER)); + $ip_dec = (float) sprintf('%u', ip2long($ip)); return $ip_dec >= $lower_dec && $ip_dec <= $upper_dec; } diff --git a/src/Params.php b/src/Params.php index 178582e..fbd1109 100644 --- a/src/Params.php +++ b/src/Params.php @@ -1,4 +1,4 @@ - getenv('API_KEY'), - 'botname' => getenv('BOTNAME'), - 'secret' => 'super-secret', - 'mysql' => [ + 'api_key' => getenv('API_KEY'), + 'bot_username' => getenv('BOT_USERNAME'), + 'secret' => 'super-secret', + 'mysql' => [ 'host' => PHPUNIT_DB_HOST, 'database' => PHPUNIT_DB_NAME, 'user' => PHPUNIT_DB_USER, @@ -77,7 +77,7 @@ public function testNoVitalsFail() */ public function testSomeVitalsFail() { - new BotManager(['api_key' => '12345:api_key', 'botname' => 'testbot']); + new BotManager(['api_key' => '12345:api_key', 'bot_username' => 'testbot']); } public function testVitalsSuccess() @@ -92,7 +92,7 @@ public function testValidTelegramObject() $telegram = $bot->getTelegram(); self::assertInstanceOf(Telegram::class, $telegram); - self::assertSame(ParamsTest::$demo_vital_params['botname'], $telegram->getBotName()); + self::assertSame(ParamsTest::$demo_vital_params['bot_username'], $telegram->getBotUsername()); self::assertSame(ParamsTest::$demo_vital_params['api_key'], $telegram->getApiKey()); } @@ -158,31 +158,31 @@ public function testValidateAndSetWebhookSuccess() $botManager, 'telegram', $this->getMockBuilder(Telegram::class) - ->disableOriginalConstructor() - ->setMethods(['setWebhook', 'deleteWebhook', 'getDescription']) - ->getMock() + ->disableOriginalConstructor() + ->setMethods(['setWebhook', 'deleteWebhook', 'getDescription']) + ->getMock() ); $telegram = $botManager->getTelegram(); /** @var \PHPUnit_Framework_MockObject_MockObject $telegram */ $telegram->expects(static::any()) - ->method('setWebhook') - ->with('https://web/hook.php?a=handle&s=secret_12345') - ->will(static::returnSelf()); + ->method('setWebhook') + ->with('https://web/hook.php?a=handle&s=secret_12345') + ->will(static::returnSelf()); $telegram->expects(static::any()) - ->method('deleteWebhook') - ->will(static::returnSelf()); + ->method('deleteWebhook') + ->will(static::returnSelf()); $telegram->expects(static::any()) - ->method('getDescription') - ->will(static::onConsecutiveCalls( - 'Webhook was set', // set - 'Webhook is already set', - 'Webhook was deleted', // reset - 'Webhook was set', - 'Webhook was deleted', //unset - 'Webhook is already deleted' - )); + ->method('getDescription') + ->will(static::onConsecutiveCalls( + 'Webhook was set', // set + 'Webhook is already set', + 'Webhook was deleted', // reset + 'Webhook was set', + 'Webhook was deleted', //unset + 'Webhook is already deleted' + )); TestHelpers::setObjectProperty($botManager->getAction(), 'action', 'set'); $output = $botManager->validateAndSetWebhook()->getOutput(); diff --git a/tests/TelegramBotManager/Tests/ParamsTest.php b/tests/TelegramBotManager/Tests/ParamsTest.php index 4846a84..3b8ef16 100644 --- a/tests/TelegramBotManager/Tests/ParamsTest.php +++ b/tests/TelegramBotManager/Tests/ParamsTest.php @@ -1,4 +1,4 @@ - '12345:api_key', - 'botname' => 'test_bot', - 'secret' => 'secret_12345', + 'api_key' => '12345:api_key', + 'bot_username' => 'test_bot', + 'secret' => 'secret_12345', ]; /** @@ -70,16 +70,16 @@ public function testConstruct() public function testConstructWithoutApiKey() { new Params([ - 'botname' => 'test_bot', - 'secret' => 'secret_12345', + 'bot_username' => 'test_bot', + 'secret' => 'secret_12345', ]); } /** * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Some vital info is missing: botname + * @expectedExceptionMessage Some vital info is missing: bot_username */ - public function testConstructWithoutBotname() + public function testConstructWithoutBotUsername() { new Params([ 'api_key' => '12345:api_key', @@ -94,8 +94,8 @@ public function testConstructWithoutBotname() public function testConstructWithoutSecret() { new Params([ - 'api_key' => '12345:api_key', - 'botname' => 'test_bot', + 'api_key' => '12345:api_key', + 'bot_username' => 'test_bot', ]); } diff --git a/tests/TelegramBotManager/Tests/TestHelpers.php b/tests/TelegramBotManager/Tests/TestHelpers.php index 1d0ad39..c9fda5b 100644 --- a/tests/TelegramBotManager/Tests/TestHelpers.php +++ b/tests/TelegramBotManager/Tests/TestHelpers.php @@ -1,4 +1,4 @@ -