Commit cf209dcf authored by Jack'lul's avatar Jack'lul Committed by GitHub

Merge pull request #5 from noplanman/misc_fix_tests

Fix tests and add a simple API key validation.
parents ca48e792 9dc79661
......@@ -12,6 +12,7 @@ namespace Longman\TelegramBot\Entities;
use Exception;
use Longman\TelegramBot\Entities\InlineQuery\InlineEntity;
use Longman\TelegramBot\TelegramLog;
use ReflectionObject;
/**
......@@ -21,8 +22,8 @@ use ReflectionObject;
*
* @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 @@ abstract class Entity
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
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 @@ class Telegram
/**
* 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 @@ class Telegram
* 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 @@ class Telegram
return $this;
}
/**
* Run provided commands
*
......@@ -920,15 +922,15 @@ class Telegram
'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,
],
]
);
......
......@@ -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()
......
......@@ -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.
......
......@@ -39,7 +39,7 @@ class ConversationTest extends TestCase
'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.
......
......@@ -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 @@ class TelegramTest extends TestCase
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage API KEY not defined!
*/
public function testNewInstanceWithoutApiKeyParam()
{
......@@ -69,20 +70,30 @@ class TelegramTest extends TestCase
/**
* @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()
......
......@@ -12,6 +12,11 @@ namespace Longman\TelegramBot\Tests\Unit;
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) {
......
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