Fix tests and add a simple API key validation.

parent ca48e792
...@@ -12,6 +12,7 @@ namespace Longman\TelegramBot\Entities; ...@@ -12,6 +12,7 @@ namespace Longman\TelegramBot\Entities;
use Exception; use Exception;
use Longman\TelegramBot\Entities\InlineQuery\InlineEntity; use Longman\TelegramBot\Entities\InlineQuery\InlineEntity;
use Longman\TelegramBot\TelegramLog;
use ReflectionObject; use ReflectionObject;
/** /**
...@@ -22,7 +23,7 @@ use ReflectionObject; ...@@ -22,7 +23,7 @@ use ReflectionObject;
* @link https://core.telegram.org/bots/api#available-types * @link https://core.telegram.org/bots/api#available-types
* *
* @method array getRawData() Get the raw data passed to this entity * @method array getRawData() Get the raw data passed to this entity
* @method string getBotName() Return the bot name passed to this entity * @method string getBotUsername() Return the bot name passed to this entity
*/ */
abstract class Entity abstract class Entity
{ {
...@@ -241,4 +242,17 @@ abstract class Entity ...@@ -241,4 +242,17 @@ abstract class Entity
return ($is_username ? '@' : '') . $name; 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();
}
} }
...@@ -143,17 +143,18 @@ class Telegram ...@@ -143,17 +143,18 @@ class Telegram
if (empty($api_key)) { if (empty($api_key)) {
throw new TelegramException('API KEY not defined!'); 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)) { if (empty($bot_username)) {
throw new TelegramException('Bot Username not defined!'); throw new TelegramException('Bot Username not defined!');
} }
$this->api_key = $api_key;
$this->bot_username = $bot_username; $this->bot_username = $bot_username;
preg_match("/([0-9]*)\:.*/", $this->api_key, $matches);
$this->bot_id = $matches[1];
//Set default download and upload path //Set default download and upload path
$this->setDownloadPath(BASE_PATH . '/../Download'); $this->setDownloadPath(BASE_PATH . '/../Download');
$this->setUploadPath(BASE_PATH . '/../Upload'); $this->setUploadPath(BASE_PATH . '/../Upload');
...@@ -740,6 +741,7 @@ class Telegram ...@@ -740,6 +741,7 @@ class Telegram
/** /**
* Get Bot name * Get Bot name
*
* @todo: Left for backwards compatibility, remove in the future * @todo: Left for backwards compatibility, remove in the future
* *
* @return string * @return string
...@@ -920,15 +922,15 @@ class Telegram ...@@ -920,15 +922,15 @@ class Telegram
'from' => [ 'from' => [
'id' => $bot_id, 'id' => $bot_id,
'first_name' => $bot_name, 'first_name' => $bot_name,
'username' => $bot_username 'username' => $bot_username,
], ],
'date' => time(), 'date' => time(),
'chat' => [ 'chat' => [
'id' => $bot_id, 'id' => $bot_id,
'type' => 'private', 'type' => 'private',
], ],
'text' => $command 'text' => $command,
] ],
] ]
); );
......
...@@ -51,11 +51,11 @@ class CommandTest extends TestCase ...@@ -51,11 +51,11 @@ class CommandTest extends TestCase
public function setUp() public function setUp()
{ {
//Default command object //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]); $this->command_stub = $this->getMockForAbstractClass($this->command_namespace, [$this->telegram]);
//Create separate command object that contain a command config //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->telegram_with_config->setCommandConfig('command_name', ['config_key' => 'config_value']);
$this->command_stub_with_config = $this->getMockBuilder($this->command_namespace) $this->command_stub_with_config = $this->getMockBuilder($this->command_namespace)
->disableOriginalConstructor() ->disableOriginalConstructor()
......
...@@ -37,7 +37,7 @@ class CommandTestCase extends TestCase ...@@ -37,7 +37,7 @@ class CommandTestCase extends TestCase
*/ */
public function setUp() 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'); $this->telegram->addCommandsPath(BASE_COMMANDS_PATH . '/UserCommands');
// Add custom commands dedicated to do some tests. // Add custom commands dedicated to do some tests.
......
...@@ -39,7 +39,7 @@ class ConversationTest extends TestCase ...@@ -39,7 +39,7 @@ class ConversationTest extends TestCase
'password' => PHPUNIT_DB_PASS, 'password' => PHPUNIT_DB_PASS,
]; ];
$this->telegram = new Telegram('apikey', 'testbot'); $this->telegram = new Telegram(self::$dummy_api_key, 'testbot');
$this->telegram->enableMySql($credentials); $this->telegram->enableMySql($credentials);
//Make sure we start with an empty DB for each test. //Make sure we start with an empty DB for each test.
......
...@@ -40,7 +40,7 @@ class TelegramTest extends TestCase ...@@ -40,7 +40,7 @@ class TelegramTest extends TestCase
*/ */
protected function setUp() 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. // Create a few dummy custom commands paths.
foreach ($this->custom_commands_paths as $custom_path) { foreach ($this->custom_commands_paths as $custom_path) {
...@@ -61,6 +61,7 @@ class TelegramTest extends TestCase ...@@ -61,6 +61,7 @@ class TelegramTest extends TestCase
/** /**
* @expectedException \Longman\TelegramBot\Exception\TelegramException * @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage API KEY not defined!
*/ */
public function testNewInstanceWithoutApiKeyParam() public function testNewInstanceWithoutApiKeyParam()
{ {
...@@ -69,20 +70,30 @@ class TelegramTest extends TestCase ...@@ -69,20 +70,30 @@ class TelegramTest extends TestCase
/** /**
* @expectedException \Longman\TelegramBot\Exception\TelegramException * @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() 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() public function testEnableAdmins()
......
...@@ -12,6 +12,11 @@ namespace Longman\TelegramBot\Tests\Unit; ...@@ -12,6 +12,11 @@ namespace Longman\TelegramBot\Tests\Unit;
class TestCase extends \PHPUnit_Framework_TestCase class TestCase extends \PHPUnit_Framework_TestCase
{ {
/**
* @var string
*/
public static $dummy_api_key = '123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11';
protected function skip64BitTest() protected function skip64BitTest()
{ {
if (PHP_INT_SIZE === 4) { if (PHP_INT_SIZE === 4) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment