Skip to content

Add conversation tests and a few matching helpers. #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/ConversationDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Longman\TelegramBot;

use Longman\TelegramBot\DB;
use Longman\TelegramBot\Exception\TelegramException;

/**
* Class ConversationDB
Expand Down Expand Up @@ -63,7 +64,7 @@ public static function selectConversation($user_id, $chat_id, $limit = null)

$results = $sth->fetchAll(\PDO::FETCH_ASSOC);

} catch (PDOException $e) {
} catch (\Exception $e) {
throw new TelegramException($e->getMessage());
}
return $results;
Expand Down Expand Up @@ -106,7 +107,7 @@ public static function insertConversation($user_id, $chat_id, $command)
$sth->bindParam(':date', $created_at);

$status = $sth->execute();
} catch (PDOException $e) {
} catch (\Exception $e) {
throw new TelegramException($e->getMessage());
}
return $status;
Expand Down Expand Up @@ -178,7 +179,7 @@ public static function update($table, array $fields_values, array $where_fields_
try {
$sth = self::$pdo->prepare($query);
$status = $sth->execute($tokens);
} catch (PDOException $e) {
} catch (\Exception $e) {
throw new TelegramException($e->getMessage());
}
return $status;
Expand Down
140 changes: 124 additions & 16 deletions tests/TestHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

namespace Tests;

use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\User;

/**
* @package TelegramTest
Expand All @@ -21,6 +25,31 @@
*/
class TestHelpers
{
/**
* Data template of a user.
*
* @var array
*/
protected static $user_template = [
'id' => 1,
'first_name' => 'first',
'last_name' => 'last',
'username' => 'user',
];

/**
* Data template of a chat.
*
* @var array
*/
protected static $chat_template = [
'id' => 1,
'first_name' => 'first',
'last_name' => 'last',
'username' => 'name',
'type' => 'private',
];

/**
* Set the value of a private/protected property of an object
*
Expand Down Expand Up @@ -50,7 +79,7 @@ public static function getFakeUpdateObject($data = null)
'message' => [
'message_id' => 1,
'chat' => [
'id' => 1,
'id' => 1,
],
'date' => 1,
]
Expand All @@ -71,23 +100,102 @@ public static function getFakeUpdateCommandObject($command_text)
'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,
'from' => self::$user_template,
'chat' => self::$chat_template,
'date' => 1,
'text' => $command_text,
],
];
return self::getFakeUpdateObject($data);
}

/**
* Return a fake user object.
*
* @return Entities\User
*/
public static function getFakeUserObject()
{
return new User(self::$user_template);
}

/**
* Return a fake chat object.
*
* @return Entities\Chat
*/
public static function getFakeChatObject()
{
return new Chat(self::$chat_template);
}

/**
* Return a fake message object using the passed ids.
*
* @param integer $message_id
* @param integer $user_id
* @param integer $chat_id
*
* @return Entities\Message
*/
public static function getFakeMessageObject($message_id = 1, $user_id = 1, $chat_id = 1)
{
return new Message([
'message_id' => $message_id,
'from' => ['id' => $user_id] + self::$user_template,
'chat' => ['id' => $chat_id] + self::$chat_template,
'date' => 1,
], 'botname');
}

/**
* Start a fake conversation for the passed command and return the randomly generated ids.
*
* @param string $command
* @return array
*/
public static function startFakeConversation($command)
{
if (!DB::isDbConnected()) {
return false;
}

//Just get some random values.
$message_id = rand();
$user_id = rand();
$chat_id = rand();

//Make sure we have a valid user and chat available.
$message = self::getFakeMessageObject($message_id, $user_id, $chat_id);
DB::insertMessageRequest($message);
DB::insertUser($message->getFrom(), null, $message->getChat());

return compact('message_id', 'user_id', 'chat_id');
}

/**
* Empty all tables for the passed database
*
* @param array $credentials
*/
public static function emptyDB(array $credentials)
{
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database'];
$options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'];
try {
$pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options);
$pdo->prepare('
DELETE FROM `conversation`;
DELETE FROM `telegram_update`;
DELETE FROM `chosen_inline_query`;
DELETE FROM `inline_query`;
DELETE FROM `message`;
DELETE FROM `user_chat`;
DELETE FROM `chat`;
DELETE FROM `user`;
')->execute();
} catch (\Exception $e) {
throw new TelegramException($e->getMessage());
}
}
}
158 changes: 158 additions & 0 deletions tests/Unit/ConversationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tests\Unit;

use Tests\TestHelpers;
use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Telegram;

/**
* @package TelegramTest
* @author Avtandil Kikabidze <[email protected]>
* @copyright Avtandil Kikabidze <[email protected]>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
*/
class ConversationTest extends TestCase
{
/**
* @var \Longman\TelegramBot\Telegram
*/
private $telegram;

/**
* setUp
*/
protected function setUp()
{
$credentials = [
'host' => '127.0.0.1',
'user' => 'travis',
'password' => '',
'database' => 'telegrambot',
];

$this->telegram = new Telegram('testapikey', 'testbotname');
$this->telegram->enableMySQL($credentials);

//Make sure we start with an empty DB for each test.
TestHelpers::emptyDB($credentials);
}

/**
* @test
*/
public function conversationThatDoesntExistPropertiesSetCorrectly()
{
$conversation = new Conversation(123, 456);
$this->assertAttributeEquals(123, 'user_id', $conversation);
$this->assertAttributeEquals(456, 'chat_id', $conversation);
$this->assertAttributeEquals(null, 'command', $conversation);
}

/**
* @test
*/
public function conversationThatExistsPropertiesSetCorrectly()
{
$info = TestHelpers::startFakeConversation('command');
$conversation = new Conversation($info['user_id'], $info['chat_id'], 'command');
$this->assertAttributeEquals($info['user_id'], 'user_id', $conversation);
$this->assertAttributeEquals($info['chat_id'], 'chat_id', $conversation);
$this->assertAttributeEquals('command', 'command', $conversation);
}

/**
* @test
*/
public function conversationThatDoesntExistWithoutCommand()
{
$conversation = new Conversation(1, 1);
$this->assertFalse($conversation->exists());
$this->assertNull($conversation->getCommand());
}

/**
* @test
* @expectedException \Longman\TelegramBot\Exception\TelegramException
*/
public function conversationThatDoesntExistWithCommand()
{
new Conversation(1, 1, 'command');
}

/**
* @test
*/
public function newConversationThatWontExistWithoutCommand()
{
TestHelpers::startFakeConversation(null);
$conversation = new Conversation(0, 0);
$this->assertFalse($conversation->exists());
$this->assertNull($conversation->getCommand());
}

/**
* @test
*/
public function newConversationThatWillExistWithCommand()
{
$info = TestHelpers::startFakeConversation('command');
$conversation = new Conversation($info['user_id'], $info['chat_id'], 'command');
$this->assertTrue($conversation->exists());
$this->assertEquals('command', $conversation->getCommand());
}

/**
* @test
*/
public function stopConversation()
{
$info = TestHelpers::startFakeConversation('command');
$conversation = new Conversation($info['user_id'], $info['chat_id'], 'command');
$this->assertTrue($conversation->exists());
$conversation->stop();

$conversation2 = new Conversation($info['user_id'], $info['chat_id']);
$this->assertFalse($conversation2->exists());
}

/**
* @test
*/
public function cancelConversation()
{
$info = TestHelpers::startFakeConversation('command');
$conversation = new Conversation($info['user_id'], $info['chat_id'], 'command');
$this->assertTrue($conversation->exists());
$conversation->cancel();

$conversation2 = new Conversation($info['user_id'], $info['chat_id']);
$this->assertFalse($conversation2->exists());
}

/**
* @test
*/
public function updateConversationNotes()
{
$info = TestHelpers::startFakeConversation('command');
$conversation = new Conversation($info['user_id'], $info['chat_id'], 'command');
$conversation->notes = 'newnote';
$conversation->update();

$conversation2 = new Conversation($info['user_id'], $info['chat_id'], 'command');
$this->assertSame('newnote', $conversation2->notes);

$conversation3 = new Conversation($info['user_id'], $info['chat_id']);
$this->assertSame('newnote', $conversation3->notes);
}
}