Skip to content

Commit cf209dc

Browse files
authored
Merge pull request #5 from noplanman/misc_fix_tests
Fix tests and add a simple API key validation.
2 parents ca48e79 + 9dc7966 commit cf209dc

File tree

7 files changed

+55
-23
lines changed

7 files changed

+55
-23
lines changed

src/Entities/Entity.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Exception;
1414
use Longman\TelegramBot\Entities\InlineQuery\InlineEntity;
15+
use Longman\TelegramBot\TelegramLog;
1516
use ReflectionObject;
1617

1718
/**
@@ -21,8 +22,8 @@
2122
*
2223
* @link https://core.telegram.org/bots/api#available-types
2324
*
24-
* @method array getRawData() Get the raw data passed to this entity
25-
* @method string getBotName() Return the bot name passed to this entity
25+
* @method array getRawData() Get the raw data passed to this entity
26+
* @method string getBotUsername() Return the bot name passed to this entity
2627
*/
2728
abstract class Entity
2829
{
@@ -241,4 +242,17 @@ public function tryMention($escape_markdown = false)
241242

242243
return ($is_username ? '@' : '') . $name;
243244
}
245+
246+
/**
247+
* Get Bot name
248+
*
249+
* @todo: Left for backwards compatibility, remove in the future
250+
*
251+
* @return string
252+
*/
253+
public function getBotName()
254+
{
255+
TelegramLog::debug('Usage of deprecated method getBotName() detected, please use getBotUsername() instead!');
256+
return $this->getBotUsername();
257+
}
244258
}

src/Telegram.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,18 @@ public function __construct($api_key, $bot_username)
143143
if (empty($api_key)) {
144144
throw new TelegramException('API KEY not defined!');
145145
}
146+
preg_match('/(\d+)\:.+/', $api_key, $matches);
147+
if (!isset($matches[1])) {
148+
throw new TelegramException('Invalid API KEY defined!');
149+
}
150+
$this->bot_id = $matches[1];
151+
$this->api_key = $api_key;
146152

147153
if (empty($bot_username)) {
148154
throw new TelegramException('Bot Username not defined!');
149155
}
150-
151-
$this->api_key = $api_key;
152156
$this->bot_username = $bot_username;
153157

154-
preg_match("/([0-9]*)\:.*/", $this->api_key, $matches);
155-
$this->bot_id = $matches[1];
156-
157158
//Set default download and upload path
158159
$this->setDownloadPath(BASE_PATH . '/../Download');
159160
$this->setUploadPath(BASE_PATH . '/../Upload');
@@ -740,7 +741,8 @@ public function getBotUsername()
740741

741742
/**
742743
* Get Bot name
743-
* @todo: Left for backwards compatibility, remove in the future
744+
*
745+
* @todo: Left for backwards compatibility, remove in the future
744746
*
745747
* @return string
746748
*/
@@ -860,7 +862,7 @@ protected function ucfirstUnicode($str, $encoding = 'UTF-8')
860862
* Enable Botan.io integration
861863
*
862864
* @param string $token
863-
* @param array $options
865+
* @param array $options
864866
*
865867
* @return \Longman\TelegramBot\Telegram
866868
* @throws \Longman\TelegramBot\Exception\TelegramException
@@ -882,7 +884,7 @@ public function enableLimiter()
882884

883885
return $this;
884886
}
885-
887+
886888
/**
887889
* Run provided commands
888890
*
@@ -920,15 +922,15 @@ public function runCommands($commands)
920922
'from' => [
921923
'id' => $bot_id,
922924
'first_name' => $bot_name,
923-
'username' => $bot_username
925+
'username' => $bot_username,
924926
],
925927
'date' => time(),
926928
'chat' => [
927929
'id' => $bot_id,
928930
'type' => 'private',
929931
],
930-
'text' => $command
931-
]
932+
'text' => $command,
933+
],
932934
]
933935
);
934936

tests/unit/Commands/CommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ class CommandTest extends TestCase
5151
public function setUp()
5252
{
5353
//Default command object
54-
$this->telegram = new Telegram('apikey', 'testbot');
54+
$this->telegram = new Telegram(self::$dummy_api_key, 'testbot');
5555
$this->command_stub = $this->getMockForAbstractClass($this->command_namespace, [$this->telegram]);
5656

5757
//Create separate command object that contain a command config
58-
$this->telegram_with_config = new Telegram('apikey', 'testbot');
58+
$this->telegram_with_config = new Telegram(self::$dummy_api_key, 'testbot');
5959
$this->telegram_with_config->setCommandConfig('command_name', ['config_key' => 'config_value']);
6060
$this->command_stub_with_config = $this->getMockBuilder($this->command_namespace)
6161
->disableOriginalConstructor()

tests/unit/Commands/CommandTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class CommandTestCase extends TestCase
3737
*/
3838
public function setUp()
3939
{
40-
$this->telegram = new Telegram('apikey', 'testbot');
40+
$this->telegram = new Telegram(self::$dummy_api_key, 'testbot');
4141
$this->telegram->addCommandsPath(BASE_COMMANDS_PATH . '/UserCommands');
4242

4343
// Add custom commands dedicated to do some tests.

tests/unit/ConversationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function setUp()
3939
'password' => PHPUNIT_DB_PASS,
4040
];
4141

42-
$this->telegram = new Telegram('apikey', 'testbot');
42+
$this->telegram = new Telegram(self::$dummy_api_key, 'testbot');
4343
$this->telegram->enableMySql($credentials);
4444

4545
//Make sure we start with an empty DB for each test.

tests/unit/TelegramTest.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class TelegramTest extends TestCase
4040
*/
4141
protected function setUp()
4242
{
43-
$this->telegram = new Telegram('apikey', 'testbot');
43+
$this->telegram = new Telegram(self::$dummy_api_key, 'testbot');
4444

4545
// Create a few dummy custom commands paths.
4646
foreach ($this->custom_commands_paths as $custom_path) {
@@ -61,6 +61,7 @@ protected function tearDown()
6161

6262
/**
6363
* @expectedException \Longman\TelegramBot\Exception\TelegramException
64+
* @expectedExceptionMessage API KEY not defined!
6465
*/
6566
public function testNewInstanceWithoutApiKeyParam()
6667
{
@@ -69,20 +70,30 @@ public function testNewInstanceWithoutApiKeyParam()
6970

7071
/**
7172
* @expectedException \Longman\TelegramBot\Exception\TelegramException
73+
* @expectedExceptionMessage Invalid API KEY defined!
7274
*/
73-
public function testNewInstanceWithoutBotNameParam()
75+
public function testNewInstanceWithInvalidApiKeyParam()
7476
{
75-
new Telegram('apikey', null);
77+
new Telegram('invalid-api-key-format', null);
78+
}
79+
80+
/**
81+
* @expectedException \Longman\TelegramBot\Exception\TelegramException
82+
* @expectedExceptionMessage Bot Username not defined!
83+
*/
84+
public function testNewInstanceWithoutBotUsernameParam()
85+
{
86+
new Telegram(self::$dummy_api_key, null);
7687
}
7788

7889
public function testGetApiKey()
7990
{
80-
$this->assertEquals('apikey', $this->telegram->getApiKey());
91+
$this->assertEquals(self::$dummy_api_key, $this->telegram->getApiKey());
8192
}
8293

83-
public function testGetBotName()
94+
public function testGetBotUsername()
8495
{
85-
$this->assertEquals('testbot', $this->telegram->getBotName());
96+
$this->assertEquals('testbot', $this->telegram->getBotUsername());
8697
}
8798

8899
public function testEnableAdmins()

tests/unit/TestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
class TestCase extends \PHPUnit_Framework_TestCase
1414
{
15+
/**
16+
* @var string
17+
*/
18+
public static $dummy_api_key = '123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11';
19+
1520
protected function skip64BitTest()
1621
{
1722
if (PHP_INT_SIZE === 4) {

0 commit comments

Comments
 (0)