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
84267c30
Commit
84267c30
authored
Feb 23, 2016
by
MBoretto
Browse files
Options
Browse Files
Download
Plain Diff
resolve conflict
parents
a8ea577f
980dcd4c
Changes
32
Show whitespace changes
Inline
Side-by-side
Showing
32 changed files
with
802 additions
and
370 deletions
+802
-370
ChatsCommand.php
src/Commands/AdminCommands/ChatsCommand.php
+2
-4
SendtoallCommand.php
src/Commands/AdminCommands/SendtoallCommand.php
+1
-1
SendtochannelCommand.php
src/Commands/AdminCommands/SendtochannelCommand.php
+3
-4
Command.php
src/Commands/Command.php
+52
-35
SystemCommand.php
src/Commands/SystemCommand.php
+6
-4
ChannelchatcreatedCommand.php
src/Commands/SystemCommands/ChannelchatcreatedCommand.php
+3
-8
ChoseninlineresultCommand.php
src/Commands/SystemCommands/ChoseninlineresultCommand.php
+3
-8
DeletechatphotoCommand.php
src/Commands/SystemCommands/DeletechatphotoCommand.php
+3
-8
GenericCommand.php
src/Commands/SystemCommands/GenericCommand.php
+3
-7
GroupchatcreatedCommand.php
src/Commands/SystemCommands/GroupchatcreatedCommand.php
+3
-8
InlinequeryCommand.php
src/Commands/SystemCommands/InlinequeryCommand.php
+2
-4
LeftchatparticipantCommand.php
src/Commands/SystemCommands/LeftchatparticipantCommand.php
+3
-8
NewchatparticipantCommand.php
src/Commands/SystemCommands/NewchatparticipantCommand.php
+2
-4
NewchattitleCommand.php
src/Commands/SystemCommands/NewchattitleCommand.php
+3
-8
StartCommand.php
src/Commands/SystemCommands/StartCommand.php
+2
-4
SupergroupchatcreatedCommand.php
src/Commands/SystemCommands/SupergroupchatcreatedCommand.php
+2
-4
DateCommand.php
src/Commands/UserCommands/DateCommand.php
+2
-4
EchoCommand.php
src/Commands/UserCommands/EchoCommand.php
+7
-5
HelpCommand.php
src/Commands/UserCommands/HelpCommand.php
+21
-29
SlapCommand.php
src/Commands/UserCommands/SlapCommand.php
+2
-4
WeatherCommand.php
src/Commands/UserCommands/WeatherCommand.php
+2
-4
WhoamiCommand.php
src/Commands/UserCommands/WhoamiCommand.php
+1
-3
Request.php
src/Request.php
+3
-3
Telegram.php
src/Telegram.php
+126
-149
Bootstrap.php
tests/Bootstrap.php
+9
-9
TestHelpers.php
tests/TestHelpers.php
+93
-0
CommandTest.php
tests/Unit/Commands/CommandTest.php
+196
-0
CommandTestCase.php
tests/Unit/Commands/CommandTestCase.php
+44
-0
EchoCommandTest.php
tests/Unit/Commands/UserCommands/EchoCommandTest.php
+75
-0
HelpCommandTest.php
tests/Unit/Commands/UserCommands/HelpCommandTest.php
+83
-0
TelegramTest.php
tests/Unit/TelegramTest.php
+45
-40
TestCase.php
tests/Unit/TestCase.php
+0
-1
No files found.
src/Commands/AdminCommands/ChatsCommand.php
View file @
84267c30
...
...
@@ -31,9 +31,7 @@ class ChatsCommand extends AdminCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
{
...
...
@@ -85,6 +83,6 @@ class ChatsCommand extends AdminCommand
'text'
=>
$text
,
];
return
Request
::
sendMessage
(
$data
)
->
isOk
()
;
return
Request
::
sendMessage
(
$data
);
}
}
src/Commands/AdminCommands/SendtoallCommand.php
View file @
84267c30
...
...
@@ -95,6 +95,6 @@ class SendtoallCommand extends AdminCommand
'text'
=>
$text
,
];
return
Request
::
sendMessage
(
$data
)
->
isOk
()
;
return
Request
::
sendMessage
(
$data
);
}
}
src/Commands/AdminCommands/SendtochannelCommand.php
View file @
84267c30
...
...
@@ -23,7 +23,7 @@ class SendtochannelCommand extends AdminCommand
*/
protected
$name
=
'sendtochannel'
;
protected
$description
=
'Send message to a channel'
;
protected
$usage
=
'/sendchannel <message to send>'
;
protected
$usage
=
'/send
to
channel <message to send>'
;
protected
$version
=
'0.1.1'
;
protected
$need_mysql
=
false
;
/**#@-*/
...
...
@@ -52,8 +52,7 @@ class SendtochannelCommand extends AdminCommand
'text'
=>
$text
,
];
$result
=
Request
::
sendMessage
(
$data
);
if
(
$result
->
isOk
())
{
if
(
Request
::
sendMessage
(
$data
)
->
isOk
())
{
$text_back
=
'Message sent succesfully to: '
.
$your_channel
;
}
else
{
$text_back
=
'Sorry message not sent to: '
.
$your_channel
;
...
...
@@ -65,6 +64,6 @@ class SendtochannelCommand extends AdminCommand
'text'
=>
$text_back
,
];
return
Request
::
sendMessage
(
$data
)
->
isOk
()
;
return
Request
::
sendMessage
(
$data
);
}
}
src/Commands/Command.php
View file @
84267c30
...
...
@@ -11,6 +11,7 @@
namespace
Longman\TelegramBot\Commands
;
use
Longman\TelegramBot\DB
;
use
Longman\TelegramBot\Request
;
use
Longman\TelegramBot\Telegram
;
use
Longman\TelegramBot\Entities\Chat
;
use
Longman\TelegramBot\Entities\Update
;
...
...
@@ -42,13 +43,6 @@ abstract class Command
*/
protected
$message
;
/**
* Command
*
* @var string
*/
protected
$command
;
/**
* Name
*
...
...
@@ -61,7 +55,7 @@ abstract class Command
*
* @var string
*/
protected
$description
=
'Command
help
'
;
protected
$description
=
'Command
description
'
;
/**
* Usage
...
...
@@ -96,16 +90,18 @@ abstract class Command
*
* @var array
*/
protected
$config
;
protected
$config
=
[]
;
/**
* Constructor
*
* @param Telegram $telegram
* @param Entities\Update $update
*/
public
function
__construct
(
Telegram
$telegram
)
public
function
__construct
(
Telegram
$telegram
,
Update
$update
=
null
)
{
$this
->
telegram
=
$telegram
;
$this
->
setUpdate
(
$update
);
$this
->
config
=
$telegram
->
getCommandConfig
(
$this
->
name
);
}
...
...
@@ -127,25 +123,27 @@ abstract class Command
/**
* Pre-execute command
*
* @return
mixed
* @return
Entities\ServerResponse
*/
public
function
preExecute
()
{
if
(
!
$this
->
need_mysql
||
(
$this
->
telegram
->
isDbEnabled
()
&&
DB
::
isDbConnected
()))
{
return
$this
->
execute
();
}
if
(
$this
->
need_mysql
&&
!
(
$this
->
telegram
->
isDbEnabled
()
&&
DB
::
isDbConnected
()))
{
return
$this
->
executeNoDB
();
}
return
$this
->
execute
();
}
/**
* Execute command
*
* @return Entities\ServerResponse
*/
abstract
public
function
execute
();
/**
* Execution if MySQL is required but not available
*
* @return
boolean
* @return
Entities\ServerResponse
*/
public
function
executeNoDB
()
{
...
...
@@ -158,7 +156,7 @@ abstract class Command
'text'
=>
'Sorry no database connection, unable to execute "'
.
$this
->
name
.
'" command.'
,
];
return
Request
::
sendMessage
(
$data
)
->
isOk
()
;
return
Request
::
sendMessage
(
$data
);
}
/**
...
...
@@ -184,21 +182,22 @@ abstract class Command
/**
* Get command config
*
* Look for
parameter
$name if found return it, if not return null.
* If $name is not set return
the all set params
* Look for
config
$name if found return it, if not return null.
* If $name is not set return
all set config.
*
* @param string|null $name
*
* @return mixed
*/
public
function
getConfig
(
$name
=
null
)
{
if
(
$name
===
null
)
{
return
$this
->
config
;
}
if
(
isset
(
$this
->
config
[
$name
]))
{
return
$this
->
config
[
$name
];
}
else
{
return
null
;
}
return
$this
->
config
;
return
null
;
}
/**
...
...
@@ -211,18 +210,6 @@ abstract class Command
return
$this
->
telegram
;
}
/**
* Set command
*
* @param string $command
* @return Command
*/
public
function
setCommand
(
$command
)
{
$this
->
command
=
$command
;
return
$this
;
}
/**
* Get usage
*
...
...
@@ -272,4 +259,34 @@ abstract class Command
{
return
$this
->
enabled
;
}
/**
* If this is a SystemCommand
*
* @return bool
*/
public
function
isSystemCommand
()
{
return
(
$this
instanceof
SystemCommand
);
}
/**
* If this is an AdminCommand
*
* @return bool
*/
public
function
isAdminCommand
()
{
return
(
$this
instanceof
AdminCommand
);
}
/**
* If this is a UserCommand
*
* @return bool
*/
public
function
isUserCommand
()
{
return
(
$this
instanceof
UserCommand
);
}
}
src/Commands/SystemCommand.php
View file @
84267c30
...
...
@@ -10,6 +10,8 @@
namespace
Longman\TelegramBot\Commands
;
use
Longman\TelegramBot\Entities\ServerResponse
;
/**
* Abstract System Command Class
*/
...
...
@@ -18,14 +20,14 @@ abstract class SystemCommand extends Command
/**
* A system command just executes
*
* Although system commands should just work and return
'true'
,
* Although system commands should just work and return
a successful ServerResponse
,
* each system command can override this method to add custom functionality.
*
* @return
bool
* @return
Entities\ServerResponse
*/
public
function
execute
()
{
//System command,
do nothing
return
true
;
//System command,
return successful ServerResponse
return
new
ServerResponse
([
'ok'
=>
true
,
'result'
=>
true
],
null
)
;
}
}
src/Commands/SystemCommands/ChannelchatcreatedCommand.php
View file @
84267c30
...
...
@@ -26,16 +26,11 @@ class ChannelchatcreatedCommand extends SystemCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
/*
public function execute()
{
//$message = $this->getMessage();
//$channel_chat_created = $message->getChannelChatCreated();
//System command, do nothing
return
true
;
}
}*/
}
src/Commands/SystemCommands/ChoseninlineresultCommand.php
View file @
84267c30
...
...
@@ -26,18 +26,13 @@ class ChoseninlineresultCommand extends SystemCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
/*
public function execute()
{
//Information about chosen result is returned
//$update = $this->getUpdate();
//$inline_query = $update->getChosenInlineResult();
//$query = $inline_query->getQuery();
//System command, do nothing
return
true
;
}
}*/
}
src/Commands/SystemCommands/DeletechatphotoCommand.php
View file @
84267c30
...
...
@@ -26,16 +26,11 @@ class DeletechatphotoCommand extends SystemCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
/*
public function execute()
{
//$message = $this->getMessage();
//$delete_chat_photo = $message->getDeleteChatPhoto();
//System command, do nothing
return
true
;
}
}*/
}
src/Commands/SystemCommands/GenericCommand.php
View file @
84267c30
...
...
@@ -27,11 +27,7 @@ class GenericCommand extends SystemCommand
/**#@-*/
/**
* Execute command
*
* @todo This can't be right, as it always returns "Command: xyz not found.. :("
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
{
...
...
@@ -43,9 +39,9 @@ class GenericCommand extends SystemCommand
$data
=
[
'chat_id'
=>
$chat_id
,
'text'
=>
'Command
:
'
.
$command
.
' not found.. :('
,
'text'
=>
'Command
/
'
.
$command
.
' not found.. :('
,
];
return
Request
::
sendMessage
(
$data
)
->
isOk
()
;
return
Request
::
sendMessage
(
$data
);
}
}
src/Commands/SystemCommands/GroupchatcreatedCommand.php
View file @
84267c30
...
...
@@ -26,16 +26,11 @@ class GroupchatcreatedCommand extends SystemCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
/*
public function execute()
{
//$message = $this->getMessage();
//$group_chat_created = $message->getGroupChatCreated();
//System command, do nothing
return
true
;
}
}*/
}
src/Commands/SystemCommands/InlinequeryCommand.php
View file @
84267c30
...
...
@@ -28,9 +28,7 @@ class InlinequeryCommand extends SystemCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
{
...
...
@@ -52,6 +50,6 @@ class InlinequeryCommand extends SystemCommand
}
$data
[
'results'
]
=
'['
.
implode
(
','
,
$array_article
)
.
']'
;
return
Request
::
answerInlineQuery
(
$data
)
->
isOk
()
;
return
Request
::
answerInlineQuery
(
$data
);
}
}
src/Commands/SystemCommands/LeftchatparticipantCommand.php
View file @
84267c30
...
...
@@ -26,16 +26,11 @@ class LeftchatparticipantCommand extends SystemCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
/*
public function execute()
{
//$message = $this->getMessage();
//$participant = $message->getLeftChatParticipant();
//System command, do nothing
return
true
;
}
}*/
}
src/Commands/SystemCommands/NewchatparticipantCommand.php
View file @
84267c30
...
...
@@ -27,9 +27,7 @@ class NewchatparticipantCommand extends SystemCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
{
...
...
@@ -49,6 +47,6 @@ class NewchatparticipantCommand extends SystemCommand
'text'
=>
$text
,
];
return
Request
::
sendMessage
(
$data
)
->
isOk
()
;
return
Request
::
sendMessage
(
$data
);
}
}
src/Commands/SystemCommands/NewchattitleCommand.php
View file @
84267c30
...
...
@@ -26,16 +26,11 @@ class NewchattitleCommand extends SystemCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
/*
public function execute()
{
//$message = $this->getMessage();
//$new_chat_title = $message->getNewChatTitle();
//System command, do nothing
return
true
;
}
}*/
}
src/Commands/SystemCommands/StartCommand.php
View file @
84267c30
...
...
@@ -28,9 +28,7 @@ class StartCommand extends SystemCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
{
...
...
@@ -44,6 +42,6 @@ class StartCommand extends SystemCommand
'text'
=>
$text
,
];
return
Request
::
sendMessage
(
$data
)
->
isOk
()
;
return
Request
::
sendMessage
(
$data
);
}
}
src/Commands/SystemCommands/SupergroupchatcreatedCommand.php
View file @
84267c30
...
...
@@ -27,9 +27,7 @@ class SupergroupchatcreatedCommand extends SystemCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
{
...
...
@@ -48,6 +46,6 @@ class SupergroupchatcreatedCommand extends SystemCommand
'text'
=>
$text
,
];
return
Request
::
sendMessage
(
$data
)
->
isOk
()
;
return
Request
::
sendMessage
(
$data
);
}
}
src/Commands/UserCommands/DateCommand.php
View file @
84267c30
...
...
@@ -175,9 +175,7 @@ class DateCommand extends UserCommand
}
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
{
...
...
@@ -204,6 +202,6 @@ class DateCommand extends UserCommand
'text'
=>
$text
,
];
return
Request
::
sendMessage
(
$data
)
->
isOk
()
;
return
Request
::
sendMessage
(
$data
);
}
}
src/Commands/UserCommands/EchoCommand.php
View file @
84267c30
...
...
@@ -28,21 +28,23 @@ class EchoCommand extends UserCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
{
$message
=
$this
->
getMessage
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$text
=
$message
->
getText
(
true
);
$text
=
trim
(
$message
->
getText
(
true
));
if
(
$text
===
''
)
{
$text
=
'Command usage: '
.
$this
->
getUsage
();
}
$data
=
[
'chat_id'
=>
$chat_id
,
'text'
=>
$text
,
];
return
Request
::
sendMessage
(
$data
)
->
isOk
()
;
return
Request
::
sendMessage
(
$data
);
}
}
src/Commands/UserCommands/HelpCommand.php
View file @
84267c30
...
...
@@ -28,9 +28,7 @@ class HelpCommand extends UserCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
{
...
...
@@ -38,46 +36,40 @@ class HelpCommand extends UserCommand
$chat_id
=
$message
->
getChat
()
->
getId
();
$message_id
=
$message
->
getMessageId
();
$
text
=
$message
->
getText
(
true
);
$
command
=
trim
(
$message
->
getText
(
true
)
);
$commands
=
$this
->
telegram
->
getCommandsList
();
//Only get enabled Admin and User commands
$commands
=
array_filter
(
$this
->
telegram
->
getCommandsList
(),
function
(
$command
)
{
return
(
!
$command
->
isSystemCommand
()
&&
$command
->
isEnabled
());
});
if
(
empty
(
$text
))
{
$msg
=
$this
->
telegram
->
getBotName
()
.
' v. '
.
$this
->
telegram
->
getVersion
()
.
"
\n\n
"
;
$msg
.=
'Commands List:'
.
"
\n
"
;
//If no command parameter is passed, show the list
if
(
$command
===
''
)
{
$text
=
$this
->
telegram
->
getBotName
()
.
' v. '
.
$this
->
telegram
->
getVersion
()
.
"
\n\n
"
;
$text
.=
'Commands List:'
.
"
\n
"
;
foreach
(
$commands
as
$command
)
{
if
(
is_object
(
$command
))
{
if
(
!
$command
->
isEnabled
())
{
continue
;
$text
.=
'/'
.
$command
->
getName
()
.
' - '
.
$command
->
getDescription
()
.
"
\n
"
;
}
$msg
.=
'/'
.
$command
->
getName
()
.
' - '
.
$command
->
getDescription
()
.
"
\n
"
;
}
}
$msg
.=
"
\n
"
.
'For exact command help type: /help <command>'
;
}
else
{
$text
=
str_replace
(
'/'
,
''
,
$text
);
if
(
isset
(
$commands
[
$text
]))
{
$command
=
$commands
[
$text
];
if
(
!
$command
->
isEnabled
())
{
$msg
=
'Command '
.
$text
.
' not found'
;
$text
.=
"
\n
"
.
'For exact command help type: /help <command>'
;
}
else
{
$msg
=
'Command: '
.
$command
->
getName
()
.
' v'
.
$command
->
getVersion
()
.
"
\n
"
;
$msg
.=
'Description: '
.
$command
->
getDescription
()
.
"
\n
"
;
$msg
.=
'Usage: '
.
$command
->
getUsage
();
}
$command
=
str_replace
(
'/'
,
''
,
$command
);
if
(
isset
(
$commands
[
$command
]))
{
$command
=
$commands
[
$command
];
$text
=
'Command: '
.
$command
->
getName
()
.
' v'
.
$command
->
getVersion
()
.
"
\n
"
;
$text
.=
'Description: '
.
$command
->
getDescription
()
.
"
\n
"
;
$text
.=
'Usage: '
.
$command
->
getUsage
();
}
else
{
$
msg
=
'Command '
.
$text
.
' not found'
;
$
text
=
'No help available: Command /'
.
$command
.
' not found'
;
}
}
$data
=
[
'chat_id'
=>
$chat_id
,
'reply_to_message_id'
=>
$message_id
,
'text'
=>
$
msg
,
'text'
=>
$
text
,
];
return
Request
::
sendMessage
(
$data
)
->
isOk
()
;
return
Request
::
sendMessage
(
$data
);
}
}
src/Commands/UserCommands/SlapCommand.php
View file @
84267c30
...
...
@@ -28,9 +28,7 @@ class SlapCommand extends UserCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
{
...
...
@@ -55,6 +53,6 @@ class SlapCommand extends UserCommand
'text'
=>
$text
,
];
return
Request
::
sendMessage
(
$data
)
->
isOk
()
;
return
Request
::
sendMessage
(
$data
);
}
}
src/Commands/UserCommands/WeatherCommand.php
View file @
84267c30
...
...
@@ -110,9 +110,7 @@ class WeatherCommand extends UserCommand
}
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
{
...
...
@@ -139,6 +137,6 @@ class WeatherCommand extends UserCommand
'text'
=>
$text
,
];
return
Request
::
sendMessage
(
$data
)
->
isOk
()
;
return
Request
::
sendMessage
(
$data
);
}
}
src/Commands/UserCommands/WhoamiCommand.php
View file @
84267c30
...
...
@@ -32,9 +32,7 @@ class WhoamiCommand extends UserCommand
/**#@-*/
/**
* Execute command
*
* @return boolean
* {@inheritdoc}
*/
public
function
execute
()
{
...
...
src/Request.php
View file @
84267c30
...
...
@@ -88,14 +88,14 @@ class Request
}
/**
* Set input from
update
or stdin and return it
* Set input from
custom input
or stdin and return it
*
* @return string
*/
public
static
function
getInput
()
{
if
(
$
update
=
self
::
$telegram
->
getCustomUpdate
())
{
self
::
setInputRaw
(
$
update
);
if
(
$
input
=
self
::
$telegram
->
getCustomInput
())
{
self
::
setInputRaw
(
$
input
);
}
else
{
self
::
setInputRaw
(
file_get_contents
(
'php://input'
));
}
...
...
src/Telegram.php
View file @
84267c30
...
...
@@ -54,18 +54,16 @@ class Telegram
protected
$input
;
/**
* Custom commands
folder
* Custom commands
paths
*
* @var array
*/
protected
$commands_
dir
=
[];
protected
$commands_
paths
=
[];
/**
*
Row custom update (json)
*
Current Update object
*
* Used to inject a custom update for testing purposes in the Request class
*
* @var string
* @var Entities\Update
*/
protected
$update
;
...
...
@@ -102,7 +100,7 @@ class Telegram
*
* @var int
*/
protected
$log_verbosity
;
protected
$log_verbosity
=
1
;
/**
* MySQL integration
...
...
@@ -123,26 +121,21 @@ class Telegram
*
* @var array
*/
protected
$commands_config
;
protected
$commands_config
=
[]
;
/**
*
Message types
*
Admins list
*
* @var array
*/
protected
$message_types
=
[
'Message'
,
'Photo'
,
'Audio'
,
'Document'
,
'Sticker'
,
'Video'
,
'Voice'
,
'Location'
,
'command'
,
'new_chat_participant'
,
'left_chat_participant'
,
'new_chat_title'
,
'delete_chat_photo'
,
'group_chat_created'
,
'supergroup_chat_created'
,
'channel_chat_created'
,
];
protected
$admins_list
=
[];
/**
*
Admins list
*
ServerResponse of the last Command execution
*
* @var
array
* @var
Entities\ServerResponse
*/
protected
$
admins_list
=
[]
;
protected
$
last_command_response
;
/**
* Constructor
...
...
@@ -163,7 +156,7 @@ class Telegram
$this
->
api_key
=
$api_key
;
$this
->
bot_name
=
$bot_name
;
//Set default download and upload
dir
//Set default download and upload
path
$this
->
setDownloadPath
(
BASE_PATH
.
'/../Download'
);
$this
->
setUploadPath
(
BASE_PATH
.
'/../Upload'
);
...
...
@@ -178,12 +171,15 @@ class Telegram
*
* @param array $credential
* @param string $table_prefix
*
* @return Telegram
*/
public
function
enableMySQL
(
array
$credential
,
$table_prefix
=
null
)
{
$this
->
pdo
=
DB
::
initialize
(
$credential
,
$this
,
$table_prefix
);
TrackingDB
::
initializeTracking
();
$this
->
mysql_enabled
=
true
;
return
$this
;
}
/**
...
...
@@ -195,14 +191,13 @@ class Telegram
{
$commands
=
[];
foreach
(
$this
->
commands_
dir
as
$dir
)
{
foreach
(
$this
->
commands_
paths
as
$path
)
{
try
{
//Get all "*Command.php" files
(except the "Abstract*" one)
//Get all "*Command.php" files
$files
=
new
\RegexIterator
(
new
\RecursiveIteratorIterator
(
new
\RecursiveDirectoryIterator
(
$
dir
)
new
\RecursiveDirectoryIterator
(
$
path
)
),
//'/^(?!Abstract).+Command.php$/'
'/^.+Command.php$/'
);
...
...
@@ -218,42 +213,12 @@ class Telegram
require_once
$file
->
getPathname
();
$command_obj
=
$this
->
getCommandObject
(
$command
);
if
(
$command_obj
&&
(
$command_obj
instanceof
Commands\AdminCommand
||
$command_obj
instanceof
Commands\UserCommand
)
)
{
if
(
$command_obj
instanceof
Commands\Command
)
{
$commands
[
$command_name
]
=
$command_obj
;
}
}
/*foreach (new \DirectoryIterator($dir) as $file) {
if ($file->isDot()) {
continue;
}
$filename = $file->getFilename();
if (substr($filename, -11) === 'Command.php') {
$command = $this->sanitizeCommand(substr($filename, 0, -11));
require_once $file->getPathname();
$command_obj = $this->getCommandObject($command);
if (!$command_obj) {
continue;
}
if (!empty($update)) {
$command_obj->setUpdate($update);
}
$commands[strtolower($command)] = $command_obj;
}
}
*/
}
catch
(
\Exception
$e
)
{
throw
new
TelegramException
(
'Error getting commands from path: '
.
$
dir
);
throw
new
TelegramException
(
'Error getting commands from path: '
.
$
path
);
}
}
...
...
@@ -264,20 +229,23 @@ class Telegram
* Get an object instance of the passed command
*
* @param string $command
* @param Entities\Update $update
*
* @return
bool|Entities\Command
* @return
Entities\Command|null
*/
public
function
getCommandObject
(
$command
,
Update
$update
=
null
)
public
function
getCommandObject
(
$command
)
{
foreach
([
'System'
,
'Admin'
,
'User'
]
as
$auth
)
{
$which
=
[
'System'
];
(
$this
->
isAdmin
())
&&
$which
[]
=
'Admin'
;
$which
[]
=
'User'
;
foreach
(
$which
as
$auth
)
{
$command_namespace
=
__NAMESPACE__
.
'\\Commands\\'
.
$auth
.
'Commands\\'
.
ucfirst
(
$command
)
.
'Command'
;
if
(
class_exists
(
$command_namespace
))
{
return
(
new
$command_namespace
(
$this
))
->
setUpdate
(
$
update
);
return
new
$command_namespace
(
$this
,
$this
->
update
);
}
}
return
false
;
return
null
;
}
/**
...
...
@@ -293,8 +261,6 @@ class Telegram
public
function
setLogRequests
(
$log_requests
)
{
$this
->
log_requests
=
$log_requests
;
//Set default log verbosity
$this
->
log_verbosity
=
1
;
return
$this
;
}
...
...
@@ -358,26 +324,36 @@ class Telegram
}
/**
* Set custom
update
string for debug purposes
* Set custom
input
string for debug purposes
*
* @param string $
update
(json format)
* @param string $
input
(json format)
*
* @return \Longman\TelegramBot\Telegram
*/
public
function
setCustom
Update
(
$update
)
public
function
setCustom
Input
(
$input
)
{
$this
->
update
=
$update
;
$this
->
input
=
$input
;
return
$this
;
}
/**
* Get custom update string for debug purposes
* Get custom input string for debug purposes
*
* @return string
*/
public
function
getCustomInput
()
{
return
$this
->
input
;
}
/**
* Get the ServerResponse of the last Command execution
*
* @return
string $update in json
* @return
Entities\ServerResponse
*/
public
function
get
CustomUpdat
e
()
public
function
get
LastCommandRespons
e
()
{
return
$this
->
updat
e
;
return
$this
->
last_command_respons
e
;
}
/**
...
...
@@ -393,43 +369,34 @@ class Telegram
//DB Query
$last_update
=
DB
::
selectTelegramUpdate
(
1
);
if
(
isset
(
$last_update
[
0
][
'id'
]))
{
//As explained in the telegram bot api documentation
$offset
=
$last_update
[
0
][
'id'
]
+
1
;
}
else
{
$offset
=
null
;
}
$offset
=
(
isset
(
$last_update
[
0
][
'id'
]))
?
$last_update
[
0
][
'id'
]
+
1
:
null
;
$
ServerR
esponse
=
Request
::
getUpdates
([
'offset'
=>
$offset
,
$
r
esponse
=
Request
::
getUpdates
([
'offset'
=>
$offset
,
'limit'
=>
$limit
,
'timeout'
=>
$timeout
'timeout'
=>
$timeout
,
]);
if
(
$ServerResponse
->
isOk
())
{
$results
=
''
;
$n_update
=
count
(
$ServerResponse
->
getResult
());
for
(
$a
=
0
;
$a
<
$n_update
;
$a
++
)
{
$result
=
$this
->
processUpdate
(
$ServerResponse
->
getResult
()[
$a
]);
if
(
$response
->
isOk
())
{
//Process all updates
foreach
((
array
)
$response
->
getResult
()
as
$result
)
{
$this
->
processUpdate
(
$result
);
}
}
return
$
ServerR
esponse
;
return
$
r
esponse
;
}
/**
* Handle bot request from wekhook
*
* @todo Should return the executed command result (true|false) but we shoud check if all commands return a value.
* Furthermore this function is the twin of handleGetUpdates for webhook, but the first returns the ServerResponse
* instead the latter return if the command has failed or not (true|false).
* We shoud use the same convention for both.
* Handle bot request from webhook
*
* @return bool
*/
public
function
handle
()
{
$this
->
input
=
Request
::
getInput
();
if
(
empty
(
$this
->
input
))
{
throw
new
TelegramException
(
'Input is empty!'
);
}
...
...
@@ -437,35 +404,44 @@ class Telegram
if
(
empty
(
$post
))
{
throw
new
TelegramException
(
'Invalid JSON!'
);
}
$update
=
new
Update
(
$post
,
$this
->
bot_name
);
return
$this
->
processUpdate
(
$update
);
return
$this
->
processUpdate
(
new
Update
(
$post
,
$this
->
bot_name
))
->
isOk
(
);
}
/**
* Get the command name from the command type
*
* @param string $type
*
* @return string
*/
private
function
getCommandFromType
(
$type
)
{
return
ucfirst
(
str_replace
(
'_'
,
''
,
$type
));
}
/**
* Process Handle bot request
* @param \Longman\TelegramBot\Entities\ServerResponse $update
* Process bot Update request
*
* @return bool
* @param Entities\Update $update
*
* @return Entities\ServerResponse
*/
public
function
processUpdate
(
Update
$update
)
{
$
update_type
=
$update
->
getUpdateType
()
;
$
this
->
update
=
$update
;
//If all else fails, it's a generic message.
$command
=
'genericmessage'
;
$update_type
=
$this
->
update
->
getUpdateType
();
if
(
in_array
(
$update_type
,
[
'inline_query'
,
'chosen_inline_result'
]))
{
$command
=
$this
->
getCommandFromType
(
$update_type
);
}
elseif
(
$update_type
===
'message'
)
{
$message
=
$update
->
getMessage
();
$message
=
$
this
->
update
->
getMessage
();
//Load admin commands
if
(
$this
->
isAdmin
(
$message
->
getFrom
()
->
getId
()
))
{
if
(
$this
->
isAdmin
())
{
$this
->
addCommandsPath
(
BASE_COMMANDS_PATH
.
'/AdminCommands'
,
false
);
}
...
...
@@ -488,37 +464,40 @@ class Telegram
}
//Make sure we have an up-to-date command list
//This is necessary to "require" all the necessary command files!
$this
->
getCommandsList
();
DB
::
insertRequest
(
$update
);
DB
::
insertRequest
(
$
this
->
update
);
return
$this
->
executeCommand
(
$command
,
$update
);
return
$this
->
executeCommand
(
$command
);
}
/**
* Execute /command
*
* @param string $command
* @param \Longman\TelegramBot\Entities\ServerResponse $update
*
* @return mixed
*/
public
function
executeCommand
(
$command
,
Update
$update
)
public
function
executeCommand
(
$command
)
{
$command_obj
=
$this
->
getCommandObject
(
$command
,
$update
);
$command_obj
=
$this
->
getCommandObject
(
$command
);
if
(
!
$command_obj
)
{
//handle a generic command or non existing one
return
$this
->
executeCommand
(
'Generic'
,
$update
);
if
(
!
$command_obj
||
!
$command_obj
->
isEnabled
())
{
//Failsafe in case the Generic command can't be found
if
(
$command
===
'Generic'
)
{
throw
new
TelegramException
(
'Generic command missing!'
);
}
if
(
!
$command_obj
->
isEnabled
())
{
return
false
;
//Handle a generic command or non existing one
$this
->
last_command_response
=
$this
->
executeCommand
(
'Generic'
);
}
else
{
//execute() method is executed after preExecute()
//This is to prevent executing a DB query without a valid connection
$this
->
last_command_response
=
$command_obj
->
preExecute
();
}
//execute() methods will be execute after preexecute() methods
//this for prevent to execute db query without connection
return
$command_obj
->
preExecute
();
return
$this
->
last_command_response
;
}
/**
...
...
@@ -562,13 +541,21 @@ class Telegram
/**
* Check if the passed user is an admin
*
* @param int $user_id
* If no user id is passed, the current update is checked for a valid message sender.
*
* @param int|null $user_id
*
* @return bool
*/
public
function
isAdmin
(
$user_id
)
public
function
isAdmin
(
$user_id
=
null
)
{
return
in_array
(
$user_id
,
$this
->
admins_list
);
if
(
$user_id
===
null
&&
$this
->
update
!==
null
)
{
if
((
$message
=
$this
->
update
->
getMessage
())
&&
(
$from
=
$message
->
getFrom
()))
{
$user_id
=
$from
->
getId
();
}
}
return
(
$user_id
===
null
)
?
false
:
in_array
(
$user_id
,
$this
->
admins_list
);
}
/**
...
...
@@ -588,20 +575,21 @@ class Telegram
/**
* Add custom commands path
*
* @param string $folder Custom commands path
* @param string $path Custom commands path
* @param bool $before If the path should be prepended or appended to the list
*
* @return \Longman\TelegramBot\Telegram
*/
public
function
addCommandsPath
(
$
folder
,
$before
=
true
)
public
function
addCommandsPath
(
$
path
,
$before
=
true
)
{
if
(
!
is_dir
(
$
folder
))
{
throw
new
TelegramException
(
'Commands
folder
does not exist!'
);
if
(
!
is_dir
(
$
path
))
{
throw
new
TelegramException
(
'Commands
path "'
.
$path
.
'"
does not exist!'
);
}
if
(
!
in_array
(
$
folder
,
$this
->
commands_dir
))
{
if
(
!
in_array
(
$
path
,
$this
->
commands_paths
))
{
if
(
$before
)
{
array_unshift
(
$this
->
commands_
dir
,
$folder
);
array_unshift
(
$this
->
commands_
paths
,
$path
);
}
else
{
array_push
(
$this
->
commands_
dir
,
$folder
);
array_push
(
$this
->
commands_
paths
,
$path
);
}
}
return
$this
;
...
...
@@ -610,13 +598,13 @@ class Telegram
/**
* Set custom upload path
*
* @param string $
folder
Custom upload path
* @param string $
path
Custom upload path
*
* @return \Longman\TelegramBot\Telegram
*/
public
function
setUploadPath
(
$
folder
)
public
function
setUploadPath
(
$
path
)
{
$this
->
upload_path
=
$
folder
;
$this
->
upload_path
=
$
path
;
return
$this
;
}
...
...
@@ -633,13 +621,13 @@ class Telegram
/**
* Set custom download path
*
* @param string $
folder
Custom download path
* @param string $
path
Custom download path
*
* @return \Longman\TelegramBot\Telegram
*/
public
function
setDownloadPath
(
$
folder
)
public
function
setDownloadPath
(
$
path
)
{
$this
->
download_path
=
$
folder
;
$this
->
download_path
=
$
path
;
return
$this
;
}
...
...
@@ -660,15 +648,14 @@ class Telegram
* For example you can add the channel name at the command /sendtochannel
* Or you can add the api key for external service.
*
*
* @param string $command
* @param array
$array
* @param array
$config
*
* @return \Longman\TelegramBot\Telegram
*/
public
function
setCommandConfig
(
$command
,
array
$
array
)
public
function
setCommandConfig
(
$command
,
array
$
config
)
{
$this
->
commands_config
[
$command
]
=
$
array
;
$this
->
commands_config
[
$command
]
=
$
config
;
return
$this
;
}
...
...
@@ -677,7 +664,7 @@ class Telegram
*
* @param string $command
*
* @return
object
* @return
array
*/
public
function
getCommandConfig
(
$command
)
{
...
...
@@ -756,14 +743,4 @@ class Telegram
return
$result
;
}
/**
* Get available message types
*
* @return array
*/
public
function
getMessageTypes
()
{
return
$this
->
message_types
;
}
}
tests/Bootstrap.php
View file @
84267c30
<?php
/*
/*
*
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
...
...
@@ -7,7 +7,9 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Tests
;
/*
* Set error reporting to the max level.
*/
...
...
@@ -18,24 +20,22 @@ error_reporting(-1);
*/
date_default_timezone_set
(
'UTC'
);
$autoloader
=
__DIR__
.
'/../vendor/autoload.php'
;
$root
=
realpath
(
dirname
(
dirname
(
__FILE__
)));
/**
/*
* Check that --dev composer installation was done
*/
if
(
!
file_exists
(
$
root
.
'/vendor/autoload.php'
))
{
if
(
!
file_exists
(
$
autoloader
))
{
throw
new
\Exception
(
'Please run "php composer.phar install --dev" in root directory '
.
'to setup unit test dependencies before running the tests'
);
}
// Include the Composer autoloader
$loader
=
require
__DIR__
.
'/../vendor/autoload.php'
;
//Include the Composer autoloader
require_once
$autoloader
;
/*
* Unset global variables that are no longer needed.
*/
unset
(
$
root
,
$
loader
);
unset
(
$
auto
loader
);
tests/TestHelpers.php
0 → 100644
View file @
84267c30
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Tests
;
use
Longman\TelegramBot\Entities\Update
;
/**
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
*/
class
TestHelpers
{
/**
* Set the value of a private/protected property of an object
*
* @param object $object Object that contains the private property
* @param string $property Name of the property who's value we want to set
* @param mixed $value The value to set to the property
*/
public
static
function
setObjectProperty
(
$object
,
$property
,
$value
)
{
$ref_object
=
new
\ReflectionObject
(
$object
);
$ref_property
=
$ref_object
->
getProperty
(
$property
);
$ref_property
->
setAccessible
(
true
);
$ref_property
->
setValue
(
$object
,
$value
);
}
/**
* Return a simple fake Update object
*
* @param array $data Pass custom data array if needed
*
* @return Entities\Update
*/
public
static
function
getFakeUpdateObject
(
$data
=
null
)
{
$data
=
$data
?:
[
'update_id'
=>
1
,
'message'
=>
[
'message_id'
=>
1
,
'chat'
=>
[
'id'
=>
1
,
],
'date'
=>
1
,
]
];
return
new
Update
(
$data
,
'botname'
);
}
/**
* Return a fake command object for the passed command text
*
* @param string $command_text
*
* @return Entities\Update
*/
public
static
function
getFakeUpdateCommandObject
(
$command_text
)
{
$data
=
[
'update_id'
=>
1
,
'message'
=>
[
'message_id'
=>
1
,
'from'
=>
[
'id'
=>
1
,
'first_name'
=>
'first'
,
'last_name'
=>
'last'
,
'username'
=>
'user'
,
],
'chat'
=>
[
'id'
=>
1
,
'first_name'
=>
'first'
,
'last_name'
=>
'last'
,
'username'
=>
'name'
,
'type'
=>
'private'
,
],
'date'
=>
1
,
'text'
=>
$command_text
,
],
];
return
self
::
getFakeUpdateObject
(
$data
);
}
}
tests/Unit/Commands/CommandTest.php
0 → 100644
View file @
84267c30
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Tests\Unit\Commands
;
use
Tests\Unit\TestCase
;
use
Tests\TestHelpers
;
use
Longman\TelegramBot\Telegram
;
/**
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
*/
class
CommandTest
extends
TestCase
{
private
$command_namespace
=
'Longman\TelegramBot\Commands\Command'
;
private
$telegram
;
private
$command_stub
;
private
$telegram_with_config
;
private
$command_stub_with_config
;
public
function
setUp
()
{
//Default command object
$this
->
telegram
=
new
Telegram
(
'apikey'
,
'botname'
);
$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'
,
'botname'
);
$this
->
telegram_with_config
->
setCommandConfig
(
'command_name'
,
[
'config_key'
=>
'config_value'
]);
$this
->
command_stub_with_config
=
$this
->
getMockBuilder
(
$this
->
command_namespace
)
->
disableOriginalConstructor
()
->
getMockForAbstractClass
();
//Set a name for the object property so that the constructor can set the config correctly
TestHelpers
::
setObjectProperty
(
$this
->
command_stub_with_config
,
'name'
,
'command_name'
);
$this
->
command_stub_with_config
->
__construct
(
$this
->
telegram_with_config
);
}
/**
* @test
*/
public
function
testCommandConstructorNeedsTelegramObject
()
{
$error_message
=
'must be an instance of Longman\TelegramBot\Telegram'
;
$params_to_test
=
[
[],
[
null
],
[
'something'
],
[
new
\stdClass
],
];
foreach
(
$params_to_test
as
$param
)
{
try
{
$this
->
getMockForAbstractClass
(
$this
->
command_namespace
,
$param
);
}
catch
(
\Exception
$e
)
{
$this
->
assertContains
(
$error_message
,
$e
->
getMessage
());
}
catch
(
\Throwable
$e
)
{
//For PHP7
$this
->
assertContains
(
$error_message
,
$e
->
getMessage
());
}
}
}
/**
* @test
*/
public
function
testCommandHasCorrectTelegramObject
()
{
$this
->
assertAttributeEquals
(
$this
->
telegram
,
'telegram'
,
$this
->
command_stub
);
$this
->
assertSame
(
$this
->
telegram
,
$this
->
command_stub
->
getTelegram
());
}
/**
* @test
*/
public
function
testDefaultCommandName
()
{
$this
->
assertAttributeEquals
(
''
,
'name'
,
$this
->
command_stub
);
$this
->
assertEmpty
(
$this
->
command_stub
->
getName
());
}
/**
* @test
*/
public
function
testDefaultCommandDescription
()
{
$this
->
assertAttributeEquals
(
'Command description'
,
'description'
,
$this
->
command_stub
);
$this
->
assertEquals
(
'Command description'
,
$this
->
command_stub
->
getDescription
());
}
/**
* @test
*/
public
function
testDefaultCommandUsage
()
{
$this
->
assertAttributeEquals
(
'Command usage'
,
'usage'
,
$this
->
command_stub
);
$this
->
assertEquals
(
'Command usage'
,
$this
->
command_stub
->
getUsage
());
}
/**
* @test
*/
public
function
testDefaultCommandVersion
()
{
$this
->
assertAttributeEquals
(
'1.0.0'
,
'version'
,
$this
->
command_stub
);
$this
->
assertEquals
(
'1.0.0'
,
$this
->
command_stub
->
getVersion
());
}
/**
* @test
*/
public
function
testDefaultCommandIsEnabled
()
{
$this
->
assertAttributeEquals
(
true
,
'enabled'
,
$this
->
command_stub
);
$this
->
assertTrue
(
$this
->
command_stub
->
isEnabled
());
}
/**
* @test
*/
public
function
testDefaultCommandNeedsMysql
()
{
$this
->
assertAttributeEquals
(
false
,
'need_mysql'
,
$this
->
command_stub
);
}
/**
* @test
*/
public
function
testDefaultCommandEmptyConfig
()
{
$this
->
assertAttributeEquals
([],
'config'
,
$this
->
command_stub
);
}
/**
* @test
*/
public
function
testDefaultCommandUpdateNull
()
{
$this
->
assertAttributeEquals
(
null
,
'update'
,
$this
->
command_stub
);
}
/**
* @test
*/
public
function
testCommandSetUpdateAndMessage
()
{
$stub
=
$this
->
command_stub
;
$this
->
assertSame
(
$stub
,
$stub
->
setUpdate
());
$this
->
assertEquals
(
null
,
$stub
->
getUpdate
());
$this
->
assertEquals
(
null
,
$stub
->
getMessage
());
$this
->
assertSame
(
$stub
,
$stub
->
setUpdate
(
null
));
$this
->
assertEquals
(
null
,
$stub
->
getUpdate
());
$this
->
assertEquals
(
null
,
$stub
->
getMessage
());
$update
=
TestHelpers
::
getFakeUpdateObject
();
$message
=
$update
->
getMessage
();
$stub
->
setUpdate
(
$update
);
$this
->
assertAttributeEquals
(
$update
,
'update'
,
$stub
);
$this
->
assertEquals
(
$update
,
$stub
->
getUpdate
());
$this
->
assertAttributeEquals
(
$message
,
'message'
,
$stub
);
$this
->
assertEquals
(
$message
,
$stub
->
getMessage
());
}
/**
* @test
*/
public
function
testCommandWithConfigNotEmptyConfig
()
{
$this
->
assertAttributeNotEmpty
(
'config'
,
$this
->
command_stub_with_config
);
}
/**
* @test
*/
public
function
testCommandWithConfigCorrectConfig
()
{
$this
->
assertAttributeEquals
([
'config_key'
=>
'config_value'
],
'config'
,
$this
->
command_stub_with_config
);
$this
->
assertEquals
([
'config_key'
=>
'config_value'
],
$this
->
command_stub_with_config
->
getConfig
(
null
));
$this
->
assertEquals
([
'config_key'
=>
'config_value'
],
$this
->
command_stub_with_config
->
getConfig
());
$this
->
assertEquals
(
'config_value'
,
$this
->
command_stub_with_config
->
getConfig
(
'config_key'
));
$this
->
assertEquals
(
null
,
$this
->
command_stub_with_config
->
getConfig
(
'not_config_key'
));
}
}
tests/Unit/Commands/CommandTestCase.php
0 → 100644
View file @
84267c30
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Tests\Unit\Commands
;
use
Tests\Unit\TestCase
;
use
Longman\TelegramBot\Telegram
;
/**
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
*/
class
CommandTestCase
extends
TestCase
{
protected
$telegram
;
protected
$command
;
public
function
setUp
()
{
$this
->
telegram
=
new
Telegram
(
'apikey'
,
'botname'
);
$this
->
telegram
->
addCommandsPath
(
BASE_COMMANDS_PATH
.
'/UserCommands'
);
$this
->
telegram
->
getCommandsList
();
}
/**
* Make sure the version number is in the format x.x.x, x.x or x
*
* @test
*/
public
function
testVersionNumberFormat
()
{
$this
->
assertRegExp
(
'/^(\d+\\.)?(\d+\\.)?(\d+)$/'
,
$this
->
command
->
getVersion
());
}
}
tests/Unit/Commands/UserCommands/EchoCommandTest.php
0 → 100644
View file @
84267c30
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Tests\Unit\Commands\UserCommands
;
use
Tests\Unit\Commands\CommandTestCase
;
use
Tests\TestHelpers
;
use
Longman\TelegramBot\Telegram
;
use
Longman\TelegramBot\Commands\UserCommands\EchoCommand
;
/**
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
*/
class
EchoCommandTest
extends
CommandTestCase
{
public
function
setUp
()
{
parent
::
setUp
();
$this
->
command
=
new
EchoCommand
(
$this
->
telegram
);
}
/**
* @test
*/
public
function
testEchoCommandProperties
()
{
$this
->
assertAttributeEquals
(
'echo'
,
'name'
,
$this
->
command
);
$this
->
assertAttributeEquals
(
'Show text'
,
'description'
,
$this
->
command
);
$this
->
assertAttributeEquals
(
'/echo <text>'
,
'usage'
,
$this
->
command
);
}
/**
* @test
*/
public
function
testEchoCommandExecuteWithoutParameter
()
{
$text
=
$this
->
command
->
setUpdate
(
TestHelpers
::
getFakeUpdateCommandObject
(
'/echo'
))
->
execute
()
->
getResult
()
->
getText
();
$this
->
assertEquals
(
'Command usage: /echo <text>'
,
$text
);
$text
=
$this
->
command
->
setUpdate
(
TestHelpers
::
getFakeUpdateCommandObject
(
'/echo '
))
->
execute
()
->
getResult
()
->
getText
();
$this
->
assertEquals
(
'Command usage: /echo <text>'
,
$text
);
}
/**
* @test
*/
public
function
testEchoCommandExecuteWithParameter
()
{
$text
=
$this
->
command
->
setUpdate
(
TestHelpers
::
getFakeUpdateCommandObject
(
'/echo Message!'
))
->
execute
()
->
getResult
()
->
getText
();
$this
->
assertEquals
(
'Message!'
,
$text
);
}
}
tests/Unit/Commands/UserCommands/HelpCommandTest.php
0 → 100644
View file @
84267c30
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Tests\Unit\Commands\UserCommands
;
use
Tests\Unit\Commands\CommandTestCase
;
use
Tests\TestHelpers
;
use
Longman\TelegramBot\Commands\UserCommands\HelpCommand
;
/**
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
*/
class
HelpCommandTest
extends
CommandTestCase
{
public
function
setUp
()
{
parent
::
setUp
();
$this
->
command
=
new
HelpCommand
(
$this
->
telegram
);
}
/**
* @test
*/
public
function
testHelpCommandProperties
()
{
$this
->
assertAttributeEquals
(
'help'
,
'name'
,
$this
->
command
);
$this
->
assertAttributeEquals
(
'Show bot commands help'
,
'description'
,
$this
->
command
);
$this
->
assertAttributeEquals
(
'/help or /help <command>'
,
'usage'
,
$this
->
command
);
}
/**
* @test
*/
public
function
testHelpCommandExecuteWithoutParameter
()
{
$text
=
$this
->
command
->
setUpdate
(
TestHelpers
::
getFakeUpdateCommandObject
(
'/help'
))
->
execute
()
->
getResult
()
->
getText
();
$this
->
assertContains
(
"botname v. "
.
$this
->
telegram
->
getVersion
()
.
"
\n\n
Commands List:"
,
$text
);
}
/**
* @test
*/
public
function
testHelpCommandExecuteWithParameterInvalidCommand
()
{
$text
=
$this
->
command
->
setUpdate
(
TestHelpers
::
getFakeUpdateCommandObject
(
'/help invalidcommand'
))
->
execute
()
->
getResult
()
->
getText
();
$this
->
assertEquals
(
'No help available: Command /invalidcommand not found'
,
$text
);
}
/**
* @test
*/
public
function
testHelpCommandExecuteWithParameterValidCommand
()
{
$text
=
$this
->
command
->
setUpdate
(
TestHelpers
::
getFakeUpdateCommandObject
(
'/help echo'
))
->
execute
()
->
getResult
()
->
getText
();
$this
->
assertContains
(
"Description: Show text
\n
Usage: /echo <text>"
,
$text
);
}
}
tests/Unit/TelegramTest.php
View file @
84267c30
<?php
/*
/*
*
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
...
...
@@ -7,9 +7,10 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Tests\Unit
;
use
\
Longman\TelegramBot\Telegram
;
use
Longman\TelegramBot\Telegram
;
/**
* @package TelegramTest
...
...
@@ -25,7 +26,6 @@ class TelegramTest extends TestCase
*/
private
$telegram
;
/**
* setUp
*/
...
...
@@ -34,51 +34,56 @@ class TelegramTest extends TestCase
$this
->
telegram
=
new
Telegram
(
'testapikey'
,
'testbotname'
);
}
/**
* @test
* @expectedException \Longman\TelegramBot\Exception\TelegramException
*/
public
function
newInstanceWithout
Params
()
{
$telegram
=
new
Telegram
(
'testapikey'
,
null
);
$telegram
=
new
Telegram
(
null
,
'test
'
);
public
function
newInstanceWithout
ApiKeyParam
()
{
new
Telegram
(
null
,
'testbotname
'
);
}
/**
* @test
* @expectedException \Longman\TelegramBot\Exception\TelegramException
*/
public
function
getCommandsList
()
{
$commands
=
$this
->
telegram
->
getCommandsList
();
$this
->
assertInternalType
(
'array'
,
$commands
);
$this
->
assertNotCount
(
0
,
$commands
);
public
function
newInstanceWithoutBotNameParam
()
{
new
Telegram
(
'testapikey'
,
null
);
}
/**
* @test
*/
public
function
get
CommandsClass
()
{
$command
=
$this
->
telegram
->
getCommandClass
(
'help'
);
$this
->
assertInstanceOf
(
'Longman\TelegramBot\Commands\HelpCommand'
,
$command
);
public
function
get
ApiKey
()
{
$this
->
assertEquals
(
'testapikey'
,
$this
->
telegram
->
getApiKey
()
);
}
/**
* @test
*/
public
function
getApiKey
()
{
$this
->
assertEquals
(
'testapikey'
,
$this
->
telegram
->
getApiKey
());
public
function
getBotName
()
{
$this
->
assertEquals
(
'testbotname'
,
$this
->
telegram
->
getBotName
());
}
/**
* @test
*/
public
function
getBotName
()
{
$this
->
assertEquals
(
'testbotname'
,
$this
->
telegram
->
getBotName
());
public
function
getCommandsList
()
{
$commands
=
$this
->
telegram
->
getCommandsList
();
$this
->
assertInternalType
(
'array'
,
$commands
);
$this
->
assertNotCount
(
0
,
$commands
);
}
/**
* @test
*/
public
function
getHelpCommandObject
()
{
$command
=
$this
->
telegram
->
getCommandObject
(
'help'
);
$this
->
assertInstanceOf
(
'Longman\TelegramBot\Commands\UserCommands\HelpCommand'
,
$command
);
}
}
tests/Unit/TestCase.php
View file @
84267c30
...
...
@@ -12,5 +12,4 @@ class TestCase extends \PHPUnit_Framework_TestCase
);
}
}
}
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