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
61cc1a44
Unverified
Commit
61cc1a44
authored
Jul 06, 2016
by
Armando Lüscher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add possibility to customise fake entity objects.
Make fake entity ids random and set current date/time.
parent
c0b83b42
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
48 deletions
+57
-48
TestHelpers.php
tests/TestHelpers.php
+57
-48
No files found.
tests/TestHelpers.php
View file @
61cc1a44
...
@@ -84,21 +84,21 @@ class TestHelpers
...
@@ -84,21 +84,21 @@ class TestHelpers
*
*
* @param array $data Pass custom data array if needed
* @param array $data Pass custom data array if needed
*
*
* @return Entities\Update
* @return
\Longman\TelegramBot\
Entities\Update
*/
*/
public
static
function
getFakeUpdateObject
(
$data
=
null
)
public
static
function
getFakeUpdateObject
(
$data
=
null
)
{
{
$data
=
$data
?:
[
$data
=
$data
?:
[
'update_id'
=>
1
,
'update_id'
=>
mt_rand
()
,
'message'
=>
[
'message'
=>
[
'message_id'
=>
1
,
'message_id'
=>
mt_rand
()
,
'chat'
=>
[
'chat'
=>
[
'id'
=>
1
,
'id'
=>
mt_rand
()
,
],
],
'date'
=>
1
,
'date'
=>
time
()
,
]
]
];
];
return
new
Update
(
$data
,
'
botname
'
);
return
new
Update
(
$data
,
'
testbot
'
);
}
}
/**
/**
...
@@ -106,17 +106,17 @@ class TestHelpers
...
@@ -106,17 +106,17 @@ class TestHelpers
*
*
* @param string $command_text
* @param string $command_text
*
*
* @return Entities\Update
* @return
\Longman\TelegramBot\
Entities\Update
*/
*/
public
static
function
getFakeUpdateCommandObject
(
$command_text
)
public
static
function
getFakeUpdateCommandObject
(
$command_text
)
{
{
$data
=
[
$data
=
[
'update_id'
=>
1
,
'update_id'
=>
mt_rand
()
,
'message'
=>
[
'message'
=>
[
'message_id'
=>
1
,
'message_id'
=>
mt_rand
()
,
'from'
=>
self
::
$user_template
,
'from'
=>
self
::
$user_template
,
'chat'
=>
self
::
$chat_template
,
'chat'
=>
self
::
$chat_template
,
'date'
=>
1
,
'date'
=>
time
()
,
'text'
=>
$command_text
,
'text'
=>
$command_text
,
],
],
];
];
...
@@ -126,61 +126,73 @@ class TestHelpers
...
@@ -126,61 +126,73 @@ class TestHelpers
/**
/**
* Return a fake user object.
* Return a fake user object.
*
*
* @return Entities\User
* @param array $data Pass custom data array if needed
*
* @return \Longman\TelegramBot\Entities\User
*/
*/
public
static
function
getFakeUserObject
()
public
static
function
getFakeUserObject
(
array
$data
=
[]
)
{
{
return
new
User
(
self
::
$user_template
);
(
$data
===
null
)
&&
$data
=
[];
return
new
User
(
$data
+
self
::
$user_template
);
}
}
/**
/**
* Return a fake chat object.
* Return a fake chat object.
*
*
* @return Entities\Chat
* @param array $data Pass custom data array if needed
*
* @return \Longman\TelegramBot\Entities\Chat
*/
*/
public
static
function
getFakeChatObject
()
public
static
function
getFakeChatObject
(
array
$data
=
[]
)
{
{
return
new
Chat
(
self
::
$chat_template
);
(
$data
===
null
)
&&
$data
=
[];
return
new
Chat
(
$data
+
self
::
$chat_template
);
}
}
/**
/**
* Return a fake message object using the passed ids.
* Return a fake message object using the passed ids.
*
*
* @param
integer $message_i
d
* @param
array $message_data Pass custom message data array if neede
d
* @param
integer $user_i
d
* @param
array $user_data Pass custom user data array if neede
d
* @param
integer $chat_i
d
* @param
array $chat_data Pass custom chat data array if neede
d
*
*
* @return Entities\Message
* @return
\Longman\TelegramBot\
Entities\Message
*/
*/
public
static
function
getFakeMessageObject
(
$message_id
=
1
,
$user_id
=
1
,
$chat_id
=
1
)
public
static
function
getFakeMessageObject
(
array
$message_data
=
[],
array
$user_data
=
[],
array
$chat_data
=
[]
)
{
{
return
new
Message
([
(
$message_data
===
null
)
&&
$message_data
=
[];
'message_id'
=>
$message_id
,
(
$user_data
===
null
)
&&
$user_data
=
[];
'from'
=>
[
'id'
=>
$user_id
]
+
self
::
$user_template
,
(
$chat_data
===
null
)
&&
$chat_data
=
[];
'chat'
=>
[
'id'
=>
$chat_id
]
+
self
::
$chat_template
,
'date'
=>
1
,
return
new
Message
(
$message_data
+
[
],
'botname'
);
'message_id'
=>
mt_rand
(),
'from'
=>
$user_data
+
self
::
$user_template
,
'chat'
=>
$chat_data
+
self
::
$chat_template
,
'date'
=>
time
(),
'text'
=>
'dummy'
,
],
'testbot'
);
}
}
/**
/**
* Start a fake conversation for the passed command and return the randomly generated ids.
* Start a fake conversation for the passed command and return the randomly generated ids.
*
*
* @param string $command
* @return array
* @return array
*/
*/
public
static
function
startFakeConversation
(
$command
)
public
static
function
startFakeConversation
()
{
{
if
(
!
DB
::
isDbConnected
())
{
if
(
!
DB
::
isDbConnected
())
{
return
false
;
return
false
;
}
}
//Just get some random values.
//Just get some random values.
$message_id
=
rand
();
$message_id
=
mt_
rand
();
$user_id
=
rand
();
$user_id
=
mt_
rand
();
$chat_id
=
rand
();
$chat_id
=
mt_
rand
();
//Make sure we have a valid user and chat available.
//Make sure we have a valid user and chat available.
$message
=
self
::
getFakeMessageObject
(
$message_id
,
$user_id
,
$chat_id
);
$message
=
self
::
getFakeMessageObject
(
[
'message_id'
=>
$message_id
],
[
'id'
=>
$user_id
],
[
'id'
=>
$chat_id
]
);
DB
::
insertMessageRequest
(
$message
);
DB
::
insertMessageRequest
(
$message
);
DB
::
insertUser
(
$message
->
getFrom
(),
null
,
$message
->
getChat
());
DB
::
insertUser
(
$message
->
getFrom
(),
null
,
$message
->
getChat
());
...
@@ -196,20 +208,17 @@ class TestHelpers
...
@@ -196,20 +208,17 @@ class TestHelpers
{
{
$dsn
=
'mysql:host='
.
$credentials
[
'host'
]
.
';dbname='
.
$credentials
[
'database'
];
$dsn
=
'mysql:host='
.
$credentials
[
'host'
]
.
';dbname='
.
$credentials
[
'database'
];
$options
=
[
\PDO
::
MYSQL_ATTR_INIT_COMMAND
=>
'SET NAMES utf8'
];
$options
=
[
\PDO
::
MYSQL_ATTR_INIT_COMMAND
=>
'SET NAMES utf8'
];
try
{
$pdo
=
new
\PDO
(
$dsn
,
$credentials
[
'user'
],
$credentials
[
'password'
],
$options
);
$pdo
=
new
\PDO
(
$dsn
,
$credentials
[
'user'
],
$credentials
[
'password'
],
$options
);
$pdo
->
prepare
(
'
$pdo
->
prepare
(
'
DELETE FROM `conversation`;
DELETE FROM `conversation`;
DELETE FROM `telegram_update`;
DELETE FROM `telegram_update`;
DELETE FROM `chosen_inline_query`;
DELETE FROM `chosen_inline_result`;
DELETE FROM `inline_query`;
DELETE FROM `inline_query`;
DELETE FROM `message`;
DELETE FROM `message`;
DELETE FROM `user_chat`;
DELETE FROM `user_chat`;
DELETE FROM `chat`;
DELETE FROM `chat`;
DELETE FROM `user`;
DELETE FROM `user`;
'
)
->
execute
();
'
)
->
execute
();
}
catch
(
\Exception
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
}
}
}
}
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