Commit ca9bff22 authored by Armando Lüscher's avatar Armando Lüscher

Add conversation tests and a few matching helpers.

parent 291b90f7
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
namespace Longman\TelegramBot; namespace Longman\TelegramBot;
use Longman\TelegramBot\DB; use Longman\TelegramBot\DB;
use Longman\TelegramBot\Exception\TelegramException;
/** /**
* Class ConversationDB * Class ConversationDB
...@@ -63,7 +64,7 @@ class ConversationDB extends DB ...@@ -63,7 +64,7 @@ class ConversationDB extends DB
$results = $sth->fetchAll(\PDO::FETCH_ASSOC); $results = $sth->fetchAll(\PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (\Exception $e) {
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
return $results; return $results;
...@@ -106,7 +107,7 @@ class ConversationDB extends DB ...@@ -106,7 +107,7 @@ class ConversationDB extends DB
$sth->bindParam(':date', $created_at); $sth->bindParam(':date', $created_at);
$status = $sth->execute(); $status = $sth->execute();
} catch (PDOException $e) { } catch (\Exception $e) {
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
return $status; return $status;
...@@ -178,7 +179,7 @@ class ConversationDB extends DB ...@@ -178,7 +179,7 @@ class ConversationDB extends DB
try { try {
$sth = self::$pdo->prepare($query); $sth = self::$pdo->prepare($query);
$status = $sth->execute($tokens); $status = $sth->execute($tokens);
} catch (PDOException $e) { } catch (\Exception $e) {
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
return $status; return $status;
......
...@@ -10,7 +10,11 @@ ...@@ -10,7 +10,11 @@
namespace Tests; 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\Update;
use Longman\TelegramBot\Entities\User;
/** /**
* @package TelegramTest * @package TelegramTest
...@@ -21,6 +25,31 @@ use Longman\TelegramBot\Entities\Update; ...@@ -21,6 +25,31 @@ use Longman\TelegramBot\Entities\Update;
*/ */
class TestHelpers 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 * Set the value of a private/protected property of an object
* *
...@@ -50,7 +79,7 @@ class TestHelpers ...@@ -50,7 +79,7 @@ class TestHelpers
'message' => [ 'message' => [
'message_id' => 1, 'message_id' => 1,
'chat' => [ 'chat' => [
'id' => 1, 'id' => 1,
], ],
'date' => 1, 'date' => 1,
] ]
...@@ -71,23 +100,102 @@ class TestHelpers ...@@ -71,23 +100,102 @@ class TestHelpers
'update_id' => 1, 'update_id' => 1,
'message' => [ 'message' => [
'message_id' => 1, 'message_id' => 1,
'from' => [ 'from' => self::$user_template,
'id' => 1, 'chat' => self::$chat_template,
'first_name' => 'first', 'date' => 1,
'last_name' => 'last', 'text' => $command_text,
'username' => 'user',
],
'chat' => [
'id' => 1,
'first_name' => 'first',
'last_name' => 'last',
'username' => 'name',
'type' => 'private',
],
'date' => 1,
'text' => $command_text,
], ],
]; ];
return self::getFakeUpdateObject($data); 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 `message`;
DELETE FROM `user_chat`;
DELETE FROM `user`;
DELETE FROM `chat`;
DELETE FROM `chosen_inline_query`;
DELETE FROM `inline_query`;
DELETE FROM `telegram_update`;
')->execute();
} catch (\Exception $e) {
//Ignore...
}
}
} }
<?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;
use Tests\TestHelpers;
use Longman\TelegramBot\Conversation;
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 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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment