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
1ea289e2
Commit
1ea289e2
authored
Sep 05, 2015
by
MBoretto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Binded behaivor of ServerResponse.php
parent
3be45232
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
235 additions
and
59 deletions
+235
-59
ServerResponse.php
src/Entities/ServerResponse.php
+11
-46
Request.php
src/Request.php
+33
-13
ServerResponseTest.php
tests/Unit/Entities/ServerResponseTest.php
+191
-0
No files found.
src/Entities/ServerResponse.php
View file @
1ea289e2
...
...
@@ -26,18 +26,24 @@ class ServerResponse extends Entity
{
if
(
isset
(
$data
[
'ok'
])
&
isset
(
$data
[
'result'
]))
{
if
(
$data
[
'ok'
]
&
$data
[
'result'
]
!=
1
)
{
if
(
$data
[
'ok'
]
&
is_array
(
$data
[
'result'
])
)
{
//Response from sendMessage set
$this
->
ok
=
$data
[
'ok'
];
$this
->
result
=
new
Message
(
$data
[
'result'
],
$bot_name
);
$this
->
error_code
=
null
;
$this
->
description
=
null
;
}
elseif
(
$data
[
'ok'
]
&
$data
[
'result'
]
==
1
)
{
}
elseif
(
$data
[
'ok'
]
&
$data
[
'result'
]
==
true
)
{
//Response from setWebhook set
$this
->
ok
=
$data
[
'ok'
];
$this
->
result
=
$data
[
'result'
]
;
$this
->
result
=
true
;
$this
->
error_code
=
null
;
if
(
isset
(
$data
[
'description'
]))
{
$this
->
description
=
$data
[
'description'
];
}
else
{
$this
->
description
=
''
;
}
}
else
{
$this
->
ok
=
false
;
...
...
@@ -87,45 +93,4 @@ class ServerResponse extends Entity
{
return
$this
->
description
;
}
//Succes request
//Array
//(
// [ok] => 1
// [result] => Array
// (
// [message_id] => 3582
// [from] => Array
// (
// [id] => 12345678
// [first_name] => name
// [username] => botname
// )
//
// [chat] => Array
// (
// [id] => 123456789
// [first_name] => name
// [username] => Surname
// )
//
// [date] => 1441194780
// [text] => hello
// )
//
//)
// Error Request
//
//Array
//(
// [ok] =>
// [error_code] => 401
// [description] => Error: Unauthorized
//)
//Array
//(
// [chat_id] => 110751663
// [text] => ciao
//)
}
src/Request.php
View file @
1ea289e2
...
...
@@ -65,25 +65,43 @@ class Request
return
$status
;
}
public
static
function
send
(
$action
,
array
$data
=
null
)
public
static
function
generateGeneralFakeServerSesponse
(
$data
=
null
)
{
//PARAM BINDED IN PHPUNIT TEST FOR TestServerResponse.php
//Maybe this in not the best possible implementation
if
(
!
in_array
(
$action
,
self
::
$methods
))
{
throw
new
TelegramException
(
'This methods doesn\'t exixt!'
);
}
//No value set in $data ie testing setWekhook
//Provided $data['chat_id'] testing sendMessage
if
(
defined
(
'PHPUNIT_TESTSUITE'
))
{
$fake_response
[
'ok'
]
=
1
;
// :)
$fake_response
[
'ok'
]
=
true
;
// :)
//some fake data just to let iniatilize the class method SendMessage
if
(
!
isset
(
$data
))
{
$fake_response
[
'result'
]
=
true
;
}
//some data to let iniatilize the class method SendMessage
if
(
isset
(
$data
[
'chat_id'
]))
{
$data
[
'message_id'
]
=
'123'
;
$data
[
'date'
]
=
'123'
;
$data
[
'message_id'
]
=
'1234'
;
$data
[
'date'
]
=
'1441378360'
;
$data
[
'text'
]
=
'hello'
;
$data
[
'from'
]
=
array
(
'id'
=>
123456789
,
'first_name'
=>
'botname'
,
'username'
=>
'namebot'
);
$data
[
'chat'
]
=
array
(
'id'
=>
$data
[
'chat_id'
]
);
$data
[
'from'
]
=
array
(
'id'
=>
123
,
'first_name'
=>
'botname'
,
'username'
=>
'namebot'
);
$fake_response
[
'result'
]
=
$data
;
}
return
$fake_response
;
}
public
static
function
send
(
$action
,
array
$data
=
null
)
{
if
(
!
in_array
(
$action
,
self
::
$methods
))
{
throw
new
TelegramException
(
'This methods doesn\'t exixt!'
);
}
if
(
defined
(
'PHPUNIT_TESTSUITE'
))
{
$fake_response
=
self
::
generateGeneralFakeServerSesponse
(
$data
);
return
new
ServerResponse
(
$fake_response
,
self
::
$telegram
->
getBotName
());
}
...
...
@@ -111,8 +129,10 @@ class Request
$response
[
'description'
]
=
'Empty server response'
;
}
//return json_decode($result, true);
return
new
ServerResponse
(
json_decode
(
$result
,
true
),
self
::
$telegram
->
getBotName
());
// return json_decode($result, true);
return
$result
;
//return new ServerResponse(json_decode($result, true), self::$telegram->getBotName());
}
public
static
function
sendMessage
(
array
$data
)
...
...
tests/Unit/Entities/ServerResponseTest.php
0 → 100644
View file @
1ea289e2
<?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.
* Written by Marco Boretto
*/
namespace
Tests\Unit
;
use
\Longman\TelegramBot\Entities\ServerResponse
;
use
\Longman\TelegramBot\Entities\Message
;
use
\Longman\TelegramBot\Request
;
/**
* @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
ServerResponseTest
extends
TestCase
{
/**
* @var \Longman\TelegramBot\Telegram
*/
private
$server
;
/**
* setUp
*/
protected
function
setUp
()
{
}
/**
* @test
*/
public
function
sendMessageOk
()
{
return
'{
"ok":true,
"result":{
"message_id":1234,
"from":{"id":123456789,"first_name":"botname","username":"namebot"},
"chat":{"id":123456789,"first_name":"john","username":"Mjohn"},
"date":1441378360,
"text":"hello"
}
}'
;
}
public
function
testSendMessageOk
()
{
$result
=
$this
->
sendMessageOk
();
$this
->
server
=
new
ServerResponse
(
json_decode
(
$result
,
true
),
'testbot'
);
$this
->
assertTrue
(
$this
->
server
->
isOk
());
$this
->
assertInstanceOf
(
'\Longman\TelegramBot\Entities\Message'
,
$this
->
server
->
getResult
());
$this
->
assertNull
(
$this
->
server
->
getErrorCode
());
$this
->
assertNull
(
$this
->
server
->
getDescription
());
//Message
$this
->
assertEquals
(
'1234'
,
$this
->
server
->
getResult
()
->
getMessageId
());
$this
->
assertEquals
(
'123456789'
,
$this
->
server
->
getResult
()
->
getFrom
()
->
getId
());
$this
->
assertEquals
(
'botname'
,
$this
->
server
->
getResult
()
->
getFrom
()
->
getFirstName
());
$this
->
assertEquals
(
'namebot'
,
$this
->
server
->
getResult
()
->
getFrom
()
->
getUserName
());
$this
->
assertEquals
(
'123456789'
,
$this
->
server
->
getResult
()
->
getChat
()
->
getId
());
$this
->
assertEquals
(
'john'
,
$this
->
server
->
getResult
()
->
getChat
()
->
getFirstName
());
$this
->
assertEquals
(
'Mjohn'
,
$this
->
server
->
getResult
()
->
getChat
()
->
getUserName
());
$this
->
assertEquals
(
'1441378360'
,
$this
->
server
->
getResult
()
->
getDate
());
$this
->
assertEquals
(
'hello'
,
$this
->
server
->
getResult
()
->
getText
());
//... they are not finished...
}
/**
* @test
*/
public
function
sendMessageFail
()
{
return
'{
"ok":false,
"error_code":400,
"description":"Error: Bad Request: wrong chat id"
}'
;
}
public
function
testSendMessageFail
()
{
$result
=
$this
->
sendMessageFail
();
$this
->
server
=
new
ServerResponse
(
json_decode
(
$result
,
true
),
'testbot'
);
$this
->
assertFalse
(
$this
->
server
->
isOk
());
$this
->
assertNull
(
$this
->
server
->
getResult
());
$this
->
assertEquals
(
'400'
,
$this
->
server
->
getErrorCode
());
$this
->
assertEquals
(
'Error: Bad Request: wrong chat id'
,
$this
->
server
->
getDescription
());
}
/**
* @test
*/
public
function
setWebHookOk
()
{
return
'{"ok":true,"result":true,"description":"Webhook was set"}'
;
}
public
function
testSetWebhookOk
()
{
$result
=
$this
->
setWebhookOk
();
$this
->
server
=
new
ServerResponse
(
json_decode
(
$result
,
true
),
'testbot'
);
$this
->
assertTrue
(
$this
->
server
->
isOk
());
$this
->
assertTrue
(
$this
->
server
->
getResult
());
$this
->
assertNull
(
$this
->
server
->
getErrorCode
());
$this
->
assertEquals
(
'Webhook was set'
,
$this
->
server
->
getDescription
());
}
/**
* @test
*/
public
function
setWebHookFail
()
{
return
'{
"ok":false,
"error_code":400,
"description":"Error: Bad request: htttps:\/\/domain.host.org\/dir\/hook.php"
}'
;
}
public
function
testSetWebhookFail
()
{
$result
=
$this
->
setWebHookFail
();
$this
->
server
=
new
ServerResponse
(
json_decode
(
$result
,
true
),
'testbot'
);
$this
->
assertFalse
(
$this
->
server
->
isOk
());
$this
->
assertNull
(
$this
->
server
->
getResult
());
$this
->
assertEquals
(
400
,
$this
->
server
->
getErrorCode
());
$this
->
assertEquals
(
"Error: Bad request: htttps://domain.host.org/dir/hook.php"
,
$this
->
server
->
getDescription
());
}
public
function
testSetGeneralTestFakeResponse
()
{
//setWebhook ok
$fake_response
=
Request
::
generateGeneralFakeServerSesponse
();
$this
->
server
=
new
ServerResponse
(
$fake_response
,
'testbot'
);
$this
->
assertTrue
(
$this
->
server
->
isOk
());
$this
->
assertTrue
(
$this
->
server
->
getResult
());
$this
->
assertNull
(
$this
->
server
->
getErrorCode
());
$this
->
assertEquals
(
''
,
$this
->
server
->
getDescription
());
//sendMessage ok
$fake_response
=
Request
::
generateGeneralFakeServerSesponse
([
'chat_id'
=>
123456789
]);
$this
->
server
=
new
ServerResponse
(
$fake_response
,
'testbot'
);
$this
->
assertTrue
(
$this
->
server
->
isOk
());
$this
->
assertInstanceOf
(
'\Longman\TelegramBot\Entities\Message'
,
$this
->
server
->
getResult
());
$this
->
assertNull
(
$this
->
server
->
getErrorCode
());
$this
->
assertNull
(
$this
->
server
->
getDescription
());
//Message
$this
->
assertEquals
(
'1234'
,
$this
->
server
->
getResult
()
->
getMessageId
());
$this
->
assertEquals
(
'1441378360'
,
$this
->
server
->
getResult
()
->
getDate
());
$this
->
assertEquals
(
'hello'
,
$this
->
server
->
getResult
()
->
getText
());
//Message //User
$this
->
assertEquals
(
'123456789'
,
$this
->
server
->
getResult
()
->
getFrom
()
->
getId
());
$this
->
assertEquals
(
'botname'
,
$this
->
server
->
getResult
()
->
getFrom
()
->
getFirstName
());
$this
->
assertEquals
(
'namebot'
,
$this
->
server
->
getResult
()
->
getFrom
()
->
getUserName
());
//Message //Chat
$this
->
assertEquals
(
'123456789'
,
$this
->
server
->
getResult
()
->
getChat
()
->
getId
());
$this
->
assertEquals
(
''
,
$this
->
server
->
getResult
()
->
getChat
()
->
getFirstName
());
$this
->
assertEquals
(
''
,
$this
->
server
->
getResult
()
->
getChat
()
->
getUserName
());
//... they are not finished...
}
}
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