Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
TelegramBot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
TelegramBot
Commits
9dc79661
Unverified
Commit
9dc79661
authored
Apr 08, 2017
by
Armando Lüscher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix tests and add a simple API key validation.
parent
ca48e792
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
55 additions
and
23 deletions
+55
-23
Entity.php
src/Entities/Entity.php
+16
-2
Telegram.php
src/Telegram.php
+13
-11
CommandTest.php
tests/unit/Commands/CommandTest.php
+2
-2
CommandTestCase.php
tests/unit/Commands/CommandTestCase.php
+1
-1
ConversationTest.php
tests/unit/ConversationTest.php
+1
-1
TelegramTest.php
tests/unit/TelegramTest.php
+17
-6
TestCase.php
tests/unit/TestCase.php
+5
-0
No files found.
src/Entities/Entity.php
View file @
9dc79661
...
@@ -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
;
/**
/**
...
@@ -21,8 +22,8 @@ use ReflectionObject;
...
@@ -21,8 +22,8 @@ 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 getBot
N
ame() Return the bot name passed to this entity
* @method string getBot
Usern
ame() 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
();
}
}
}
src/Telegram.php
View file @
9dc79661
...
@@ -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,7 +741,8 @@ class Telegram
...
@@ -740,7 +741,8 @@ 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
*/
*/
...
@@ -860,7 +862,7 @@ class Telegram
...
@@ -860,7 +862,7 @@ class Telegram
* Enable Botan.io integration
* Enable Botan.io integration
*
*
* @param string $token
* @param string $token
* @param array $options
* @param array
$options
*
*
* @return \Longman\TelegramBot\Telegram
* @return \Longman\TelegramBot\Telegram
* @throws \Longman\TelegramBot\Exception\TelegramException
* @throws \Longman\TelegramBot\Exception\TelegramException
...
@@ -882,7 +884,7 @@ class Telegram
...
@@ -882,7 +884,7 @@ class Telegram
return
$this
;
return
$this
;
}
}
/**
/**
* Run provided commands
* Run provided commands
*
*
...
@@ -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
,
]
]
,
]
]
);
);
...
...
tests/unit/Commands/CommandTest.php
View file @
9dc79661
...
@@ -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
()
...
...
tests/unit/Commands/CommandTestCase.php
View file @
9dc79661
...
@@ -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.
...
...
tests/unit/ConversationTest.php
View file @
9dc79661
...
@@ -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.
...
...
tests/unit/TelegramTest.php
View file @
9dc79661
...
@@ -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
testNewInstanceWith
outBotName
Param
()
public
function
testNewInstanceWith
InvalidApiKey
Param
()
{
{
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
testGetBot
N
ame
()
public
function
testGetBot
Usern
ame
()
{
{
$this
->
assertEquals
(
'testbot'
,
$this
->
telegram
->
getBot
N
ame
());
$this
->
assertEquals
(
'testbot'
,
$this
->
telegram
->
getBot
Usern
ame
());
}
}
public
function
testEnableAdmins
()
public
function
testEnableAdmins
()
...
...
tests/unit/TestCase.php
View file @
9dc79661
...
@@ -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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment