diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index 6dbf9a41c..e690634e5 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -12,6 +12,7 @@ use Exception; use Longman\TelegramBot\Entities\InlineQuery\InlineEntity; +use Longman\TelegramBot\TelegramLog; use ReflectionObject; /** @@ -21,8 +22,8 @@ * * @link https://core.telegram.org/bots/api#available-types * - * @method array getRawData() Get the raw data passed to this entity - * @method string getBotName() Return the bot name passed to this entity + * @method array getRawData() Get the raw data passed to this entity + * @method string getBotUsername() Return the bot name passed to this entity */ abstract class Entity { @@ -241,4 +242,17 @@ public function tryMention($escape_markdown = false) return ($is_username ? '@' : '') . $name; } + + /** + * Get Bot name + * + * @todo: Left for backwards compatibility, remove in the future + * + * @return string + */ + public function getBotName() + { + TelegramLog::debug('Usage of deprecated method getBotName() detected, please use getBotUsername() instead!'); + return $this->getBotUsername(); + } } diff --git a/src/Telegram.php b/src/Telegram.php index a831a8207..8574e6216 100644 --- a/src/Telegram.php +++ b/src/Telegram.php @@ -143,17 +143,18 @@ public function __construct($api_key, $bot_username) if (empty($api_key)) { throw new TelegramException('API KEY not defined!'); } + preg_match('/(\d+)\:.+/', $api_key, $matches); + if (!isset($matches[1])) { + throw new TelegramException('Invalid API KEY defined!'); + } + $this->bot_id = $matches[1]; + $this->api_key = $api_key; if (empty($bot_username)) { throw new TelegramException('Bot Username not defined!'); } - - $this->api_key = $api_key; $this->bot_username = $bot_username; - preg_match("/([0-9]*)\:.*/", $this->api_key, $matches); - $this->bot_id = $matches[1]; - //Set default download and upload path $this->setDownloadPath(BASE_PATH . '/../Download'); $this->setUploadPath(BASE_PATH . '/../Upload'); @@ -740,7 +741,8 @@ public function getBotUsername() /** * Get Bot name - * @todo: Left for backwards compatibility, remove in the future + * + * @todo: Left for backwards compatibility, remove in the future * * @return string */ @@ -860,7 +862,7 @@ protected function ucfirstUnicode($str, $encoding = 'UTF-8') * Enable Botan.io integration * * @param string $token - * @param array $options + * @param array $options * * @return \Longman\TelegramBot\Telegram * @throws \Longman\TelegramBot\Exception\TelegramException @@ -882,7 +884,7 @@ public function enableLimiter() return $this; } - + /** * Run provided commands * @@ -920,15 +922,15 @@ public function runCommands($commands) 'from' => [ 'id' => $bot_id, 'first_name' => $bot_name, - 'username' => $bot_username + 'username' => $bot_username, ], 'date' => time(), 'chat' => [ 'id' => $bot_id, 'type' => 'private', ], - 'text' => $command - ] + 'text' => $command, + ], ] ); diff --git a/tests/unit/Commands/CommandTest.php b/tests/unit/Commands/CommandTest.php index 20a896342..e98eec642 100644 --- a/tests/unit/Commands/CommandTest.php +++ b/tests/unit/Commands/CommandTest.php @@ -51,11 +51,11 @@ class CommandTest extends TestCase public function setUp() { //Default command object - $this->telegram = new Telegram('apikey', 'testbot'); + $this->telegram = new Telegram(self::$dummy_api_key, 'testbot'); $this->command_stub = $this->getMockForAbstractClass($this->command_namespace, [$this->telegram]); //Create separate command object that contain a command config - $this->telegram_with_config = new Telegram('apikey', 'testbot'); + $this->telegram_with_config = new Telegram(self::$dummy_api_key, 'testbot'); $this->telegram_with_config->setCommandConfig('command_name', ['config_key' => 'config_value']); $this->command_stub_with_config = $this->getMockBuilder($this->command_namespace) ->disableOriginalConstructor() diff --git a/tests/unit/Commands/CommandTestCase.php b/tests/unit/Commands/CommandTestCase.php index b8bcd0a18..966e3d9b0 100644 --- a/tests/unit/Commands/CommandTestCase.php +++ b/tests/unit/Commands/CommandTestCase.php @@ -37,7 +37,7 @@ class CommandTestCase extends TestCase */ public function setUp() { - $this->telegram = new Telegram('apikey', 'testbot'); + $this->telegram = new Telegram(self::$dummy_api_key, 'testbot'); $this->telegram->addCommandsPath(BASE_COMMANDS_PATH . '/UserCommands'); // Add custom commands dedicated to do some tests. diff --git a/tests/unit/ConversationTest.php b/tests/unit/ConversationTest.php index 26f424f9b..bb739902d 100644 --- a/tests/unit/ConversationTest.php +++ b/tests/unit/ConversationTest.php @@ -39,7 +39,7 @@ protected function setUp() 'password' => PHPUNIT_DB_PASS, ]; - $this->telegram = new Telegram('apikey', 'testbot'); + $this->telegram = new Telegram(self::$dummy_api_key, 'testbot'); $this->telegram->enableMySql($credentials); //Make sure we start with an empty DB for each test. diff --git a/tests/unit/TelegramTest.php b/tests/unit/TelegramTest.php index 55d782cb8..54d5d7cac 100644 --- a/tests/unit/TelegramTest.php +++ b/tests/unit/TelegramTest.php @@ -40,7 +40,7 @@ class TelegramTest extends TestCase */ protected function setUp() { - $this->telegram = new Telegram('apikey', 'testbot'); + $this->telegram = new Telegram(self::$dummy_api_key, 'testbot'); // Create a few dummy custom commands paths. foreach ($this->custom_commands_paths as $custom_path) { @@ -61,6 +61,7 @@ protected function tearDown() /** * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage API KEY not defined! */ public function testNewInstanceWithoutApiKeyParam() { @@ -69,20 +70,30 @@ public function testNewInstanceWithoutApiKeyParam() /** * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage Invalid API KEY defined! */ - public function testNewInstanceWithoutBotNameParam() + public function testNewInstanceWithInvalidApiKeyParam() { - new Telegram('apikey', null); + new Telegram('invalid-api-key-format', null); + } + + /** + * @expectedException \Longman\TelegramBot\Exception\TelegramException + * @expectedExceptionMessage Bot Username not defined! + */ + public function testNewInstanceWithoutBotUsernameParam() + { + new Telegram(self::$dummy_api_key, null); } public function testGetApiKey() { - $this->assertEquals('apikey', $this->telegram->getApiKey()); + $this->assertEquals(self::$dummy_api_key, $this->telegram->getApiKey()); } - public function testGetBotName() + public function testGetBotUsername() { - $this->assertEquals('testbot', $this->telegram->getBotName()); + $this->assertEquals('testbot', $this->telegram->getBotUsername()); } public function testEnableAdmins() diff --git a/tests/unit/TestCase.php b/tests/unit/TestCase.php index 53a096191..3ac5cca88 100644 --- a/tests/unit/TestCase.php +++ b/tests/unit/TestCase.php @@ -12,6 +12,11 @@ class TestCase extends \PHPUnit_Framework_TestCase { + /** + * @var string + */ + public static $dummy_api_key = '123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11'; + protected function skip64BitTest() { if (PHP_INT_SIZE === 4) {