Skip to content

Commit

Permalink
Merge pull request #5 from noplanman/misc_fix_tests
Browse files Browse the repository at this point in the history
Fix tests and add a simple API key validation.
  • Loading branch information
jacklul authored Apr 8, 2017
2 parents ca48e79 + 9dc7966 commit cf209dc
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 23 deletions.
18 changes: 16 additions & 2 deletions src/Entities/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Exception;
use Longman\TelegramBot\Entities\InlineQuery\InlineEntity;
use Longman\TelegramBot\TelegramLog;
use ReflectionObject;

/**
Expand All @@ -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
{
Expand Down Expand Up @@ -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();
}
}
24 changes: 13 additions & 11 deletions src/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand All @@ -882,7 +884,7 @@ public function enableLimiter()

return $this;
}

/**
* Run provided commands
*
Expand Down Expand Up @@ -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,
],
]
);

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Commands/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Commands/CommandTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/ConversationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 17 additions & 6 deletions tests/unit/TelegramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -61,6 +61,7 @@ protected function tearDown()

/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage API KEY not defined!
*/
public function testNewInstanceWithoutApiKeyParam()
{
Expand All @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit cf209dc

Please sign in to comment.