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
Expand all
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
This diff is collapsed.
Click to expand it.
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