Skip to content

Commit 7d434dd

Browse files
committed
Merge pull request #10 from noplanman/add_conversation_tests
Add conversation tests and a few matching helpers.
2 parents 2858ff5 + 97755aa commit 7d434dd

File tree

2 files changed

+282
-16
lines changed

2 files changed

+282
-16
lines changed

tests/TestHelpers.php

Lines changed: 124 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
namespace Tests;
1212

13+
use Longman\TelegramBot\DB;
14+
use Longman\TelegramBot\Entities\Chat;
15+
use Longman\TelegramBot\Entities\Message;
1316
use Longman\TelegramBot\Entities\Update;
17+
use Longman\TelegramBot\Entities\User;
1418

1519
/**
1620
* @package TelegramTest
@@ -21,6 +25,31 @@
2125
*/
2226
class TestHelpers
2327
{
28+
/**
29+
* Data template of a user.
30+
*
31+
* @var array
32+
*/
33+
protected static $user_template = [
34+
'id' => 1,
35+
'first_name' => 'first',
36+
'last_name' => 'last',
37+
'username' => 'user',
38+
];
39+
40+
/**
41+
* Data template of a chat.
42+
*
43+
* @var array
44+
*/
45+
protected static $chat_template = [
46+
'id' => 1,
47+
'first_name' => 'first',
48+
'last_name' => 'last',
49+
'username' => 'name',
50+
'type' => 'private',
51+
];
52+
2453
/**
2554
* Set the value of a private/protected property of an object
2655
*
@@ -50,7 +79,7 @@ public static function getFakeUpdateObject($data = null)
5079
'message' => [
5180
'message_id' => 1,
5281
'chat' => [
53-
'id' => 1,
82+
'id' => 1,
5483
],
5584
'date' => 1,
5685
]
@@ -71,23 +100,102 @@ public static function getFakeUpdateCommandObject($command_text)
71100
'update_id' => 1,
72101
'message' => [
73102
'message_id' => 1,
74-
'from' => [
75-
'id' => 1,
76-
'first_name' => 'first',
77-
'last_name' => 'last',
78-
'username' => 'user',
79-
],
80-
'chat' => [
81-
'id' => 1,
82-
'first_name' => 'first',
83-
'last_name' => 'last',
84-
'username' => 'name',
85-
'type' => 'private',
86-
],
87-
'date' => 1,
88-
'text' => $command_text,
103+
'from' => self::$user_template,
104+
'chat' => self::$chat_template,
105+
'date' => 1,
106+
'text' => $command_text,
89107
],
90108
];
91109
return self::getFakeUpdateObject($data);
92110
}
111+
112+
/**
113+
* Return a fake user object.
114+
*
115+
* @return Entities\User
116+
*/
117+
public static function getFakeUserObject()
118+
{
119+
return new User(self::$user_template);
120+
}
121+
122+
/**
123+
* Return a fake chat object.
124+
*
125+
* @return Entities\Chat
126+
*/
127+
public static function getFakeChatObject()
128+
{
129+
return new Chat(self::$chat_template);
130+
}
131+
132+
/**
133+
* Return a fake message object using the passed ids.
134+
*
135+
* @param integer $message_id
136+
* @param integer $user_id
137+
* @param integer $chat_id
138+
*
139+
* @return Entities\Message
140+
*/
141+
public static function getFakeMessageObject($message_id = 1, $user_id = 1, $chat_id = 1)
142+
{
143+
return new Message([
144+
'message_id' => $message_id,
145+
'from' => ['id' => $user_id] + self::$user_template,
146+
'chat' => ['id' => $chat_id] + self::$chat_template,
147+
'date' => 1,
148+
], 'botname');
149+
}
150+
151+
/**
152+
* Start a fake conversation for the passed command and return the randomly generated ids.
153+
*
154+
* @param string $command
155+
* @return array
156+
*/
157+
public static function startFakeConversation($command)
158+
{
159+
if (!DB::isDbConnected()) {
160+
return false;
161+
}
162+
163+
//Just get some random values.
164+
$message_id = rand();
165+
$user_id = rand();
166+
$chat_id = rand();
167+
168+
//Make sure we have a valid user and chat available.
169+
$message = self::getFakeMessageObject($message_id, $user_id, $chat_id);
170+
DB::insertMessageRequest($message);
171+
DB::insertUser($message->getFrom(), null, $message->getChat());
172+
173+
return compact('message_id', 'user_id', 'chat_id');
174+
}
175+
176+
/**
177+
* Empty all tables for the passed database
178+
*
179+
* @param array $credentials
180+
*/
181+
public static function emptyDB(array $credentials)
182+
{
183+
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database'];
184+
$options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'];
185+
try {
186+
$pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options);
187+
$pdo->prepare('
188+
DELETE FROM `conversation`;
189+
DELETE FROM `telegram_update`;
190+
DELETE FROM `chosen_inline_query`;
191+
DELETE FROM `inline_query`;
192+
DELETE FROM `message`;
193+
DELETE FROM `user_chat`;
194+
DELETE FROM `chat`;
195+
DELETE FROM `user`;
196+
')->execute();
197+
} catch (\Exception $e) {
198+
throw new TelegramException($e->getMessage());
199+
}
200+
}
93201
}

tests/Unit/ConversationTest.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Tests\Unit;
12+
13+
use Tests\TestHelpers;
14+
use Longman\TelegramBot\Conversation;
15+
use Longman\TelegramBot\Telegram;
16+
17+
/**
18+
* @package TelegramTest
19+
* @author Avtandil Kikabidze <[email protected]>
20+
* @copyright Avtandil Kikabidze <[email protected]>
21+
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
22+
* @link http://www.github.com/akalongman/php-telegram-bot
23+
*/
24+
class ConversationTest extends TestCase
25+
{
26+
/**
27+
* @var \Longman\TelegramBot\Telegram
28+
*/
29+
private $telegram;
30+
31+
/**
32+
* setUp
33+
*/
34+
protected function setUp()
35+
{
36+
$credentials = [
37+
'host' => '127.0.0.1',
38+
'user' => 'travis',
39+
'password' => '',
40+
'database' => 'telegrambot',
41+
];
42+
43+
$this->telegram = new Telegram('testapikey', 'testbotname');
44+
$this->telegram->enableMySQL($credentials);
45+
46+
//Make sure we start with an empty DB for each test.
47+
TestHelpers::emptyDB($credentials);
48+
}
49+
50+
/**
51+
* @test
52+
*/
53+
public function conversationThatDoesntExistPropertiesSetCorrectly()
54+
{
55+
$conversation = new Conversation(123, 456);
56+
$this->assertAttributeEquals(123, 'user_id', $conversation);
57+
$this->assertAttributeEquals(456, 'chat_id', $conversation);
58+
$this->assertAttributeEquals(null, 'command', $conversation);
59+
}
60+
61+
/**
62+
* @test
63+
*/
64+
public function conversationThatExistsPropertiesSetCorrectly()
65+
{
66+
$info = TestHelpers::startFakeConversation('command');
67+
$conversation = new Conversation($info['user_id'], $info['chat_id'], 'command');
68+
$this->assertAttributeEquals($info['user_id'], 'user_id', $conversation);
69+
$this->assertAttributeEquals($info['chat_id'], 'chat_id', $conversation);
70+
$this->assertAttributeEquals('command', 'command', $conversation);
71+
}
72+
73+
/**
74+
* @test
75+
*/
76+
public function conversationThatDoesntExistWithoutCommand()
77+
{
78+
$conversation = new Conversation(1, 1);
79+
$this->assertFalse($conversation->exists());
80+
$this->assertNull($conversation->getCommand());
81+
}
82+
83+
/**
84+
* @test
85+
* @expectedException \Longman\TelegramBot\Exception\TelegramException
86+
*/
87+
public function conversationThatDoesntExistWithCommand()
88+
{
89+
new Conversation(1, 1, 'command');
90+
}
91+
92+
/**
93+
* @test
94+
*/
95+
public function newConversationThatWontExistWithoutCommand()
96+
{
97+
TestHelpers::startFakeConversation(null);
98+
$conversation = new Conversation(0, 0);
99+
$this->assertFalse($conversation->exists());
100+
$this->assertNull($conversation->getCommand());
101+
}
102+
103+
/**
104+
* @test
105+
*/
106+
public function newConversationThatWillExistWithCommand()
107+
{
108+
$info = TestHelpers::startFakeConversation('command');
109+
$conversation = new Conversation($info['user_id'], $info['chat_id'], 'command');
110+
$this->assertTrue($conversation->exists());
111+
$this->assertEquals('command', $conversation->getCommand());
112+
}
113+
114+
/**
115+
* @test
116+
*/
117+
public function stopConversation()
118+
{
119+
$info = TestHelpers::startFakeConversation('command');
120+
$conversation = new Conversation($info['user_id'], $info['chat_id'], 'command');
121+
$this->assertTrue($conversation->exists());
122+
$conversation->stop();
123+
124+
$conversation2 = new Conversation($info['user_id'], $info['chat_id']);
125+
$this->assertFalse($conversation2->exists());
126+
}
127+
128+
/**
129+
* @test
130+
*/
131+
public function cancelConversation()
132+
{
133+
$info = TestHelpers::startFakeConversation('command');
134+
$conversation = new Conversation($info['user_id'], $info['chat_id'], 'command');
135+
$this->assertTrue($conversation->exists());
136+
$conversation->cancel();
137+
138+
$conversation2 = new Conversation($info['user_id'], $info['chat_id']);
139+
$this->assertFalse($conversation2->exists());
140+
}
141+
142+
/**
143+
* @test
144+
*/
145+
public function updateConversationNotes()
146+
{
147+
$info = TestHelpers::startFakeConversation('command');
148+
$conversation = new Conversation($info['user_id'], $info['chat_id'], 'command');
149+
$conversation->notes = 'newnote';
150+
$conversation->update();
151+
152+
$conversation2 = new Conversation($info['user_id'], $info['chat_id'], 'command');
153+
$this->assertSame('newnote', $conversation2->notes);
154+
155+
$conversation3 = new Conversation($info['user_id'], $info['chat_id']);
156+
$this->assertSame('newnote', $conversation3->notes);
157+
}
158+
}

0 commit comments

Comments
 (0)