Commit bec999bb authored by Marco Boretto's avatar Marco Boretto Committed by GitHub

Merge pull request #246 from noplanman/improve_test_code

Improve test code
parents dd0b61f4 f66fcdaa
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.6/phpunit.xsd"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
bootstrap="./tests/Bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
......@@ -23,6 +23,10 @@
<php>
<ini name="error_reporting" value="-1" />
<const name="PHPUNIT_TESTSUITE" value="true"/>
<const name="PHPUNIT_DB_HOST" value="127.0.0.1"/>
<const name="PHPUNIT_DB_NAME" value="telegrambot"/>
<const name="PHPUNIT_DB_USER" value="root"/>
<const name="PHPUNIT_DB_PASS" value=""/>
</php>
<testsuites>
<testsuite name="Package Test Suite">
......
......@@ -22,7 +22,7 @@ CREATE TABLE IF NOT EXISTS `chat` (
KEY `old_id` (`old_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `user_chat` (
CREATE TABLE IF NOT EXISTS `user_chat` (
`user_id` bigint COMMENT 'Unique user identifier',
`chat_id` bigint COMMENT 'Unique user or chat identifier',
......
......@@ -84,21 +84,21 @@ class TestHelpers
*
* @param array $data Pass custom data array if needed
*
* @return Entities\Update
* @return \Longman\TelegramBot\Entities\Update
*/
public static function getFakeUpdateObject($data = null)
{
$data = $data ?: [
'update_id' => 1,
'update_id' => mt_rand(),
'message' => [
'message_id' => 1,
'message_id' => mt_rand(),
'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
*
* @param string $command_text
*
* @return Entities\Update
* @return \Longman\TelegramBot\Entities\Update
*/
public static function getFakeUpdateCommandObject($command_text)
{
$data = [
'update_id' => 1,
'update_id' => mt_rand(),
'message' => [
'message_id' => 1,
'message_id' => mt_rand(),
'from' => self::$user_template,
'chat' => self::$chat_template,
'date' => 1,
'date' => time(),
'text' => $command_text,
],
];
......@@ -126,61 +126,73 @@ class TestHelpers
/**
* 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 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.
*
* @param integer $message_id
* @param integer $user_id
* @param integer $chat_id
* @param array $message_data Pass custom message data array if needed
* @param array $user_data Pass custom user data array if needed
* @param array $chat_data Pass custom chat data array if needed
*
* @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_id' => $message_id,
'from' => ['id' => $user_id] + self::$user_template,
'chat' => ['id' => $chat_id] + self::$chat_template,
'date' => 1,
], 'botname');
($message_data === null) && $message_data = [];
($user_data === null) && $user_data = [];
($chat_data === null) && $chat_data = [];
return new Message($message_data + [
'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.
*
* @param string $command
* @return array
*/
public static function startFakeConversation($command)
public static function startFakeConversation()
{
if (!DB::isDbConnected()) {
return false;
}
//Just get some random values.
$message_id = rand();
$user_id = rand();
$chat_id = rand();
$message_id = mt_rand();
$user_id = mt_rand();
$chat_id = mt_rand();
//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::insertUser($message->getFrom(), null, $message->getChat());
......@@ -196,20 +208,17 @@ class TestHelpers
{
$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());
}
$pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options);
$pdo->prepare('
DELETE FROM `conversation`;
DELETE FROM `telegram_update`;
DELETE FROM `chosen_inline_result`;
DELETE FROM `inline_query`;
DELETE FROM `message`;
DELETE FROM `user_chat`;
DELETE FROM `chat`;
DELETE FROM `user`;
')->execute();
}
}
......@@ -23,22 +23,39 @@ use Longman\TelegramBot\Telegram;
*/
class CommandTest extends TestCase
{
/**
* @var string
*/
private $command_namespace = 'Longman\TelegramBot\Commands\Command';
/**
* @var \Longman\TelegramBot\Telegram
*/
private $telegram;
/**
* @var \Longman\TelegramBot\Commands\Command
*/
private $command_stub;
/**
* @var \Longman\TelegramBot\Telegram
*/
private $telegram_with_config;
/**
* @var \Longman\TelegramBot\Commands\Command
*/
private $command_stub_with_config;
public function setUp()
{
//Default command object
$this->telegram = new Telegram('apikey', 'botname');
$this->telegram = new Telegram('apikey', 'testbot');
$this->command_stub = $this->getMockForAbstractClass($this->command_namespace, [$this->telegram]);
//Create separate command object that contain a command config
$this->telegram_with_config = new Telegram('apikey', 'botname');
$this->telegram_with_config = new Telegram('apikey', 'testbot');
$this->telegram_with_config->setCommandConfig('command_name', ['config_key' => 'config_value']);
$this->command_stub_with_config = $this->getMockBuilder($this->command_namespace)
->disableOriginalConstructor()
......@@ -48,9 +65,6 @@ class CommandTest extends TestCase
$this->command_stub_with_config->__construct($this->telegram_with_config);
}
/**
* @test
*/
public function testCommandConstructorNeedsTelegramObject()
{
$error_message = 'must be an instance of Longman\TelegramBot\Telegram';
......@@ -72,87 +86,57 @@ class CommandTest extends TestCase
}
}
/**
* @test
*/
public function testCommandHasCorrectTelegramObject()
{
$this->assertAttributeEquals($this->telegram, 'telegram', $this->command_stub);
$this->assertSame($this->telegram, $this->command_stub->getTelegram());
}
/**
* @test
*/
public function testDefaultCommandName()
{
$this->assertAttributeEquals('', 'name', $this->command_stub);
$this->assertEmpty($this->command_stub->getName());
}
/**
* @test
*/
public function testDefaultCommandDescription()
{
$this->assertAttributeEquals('Command description', 'description', $this->command_stub);
$this->assertEquals('Command description', $this->command_stub->getDescription());
}
/**
* @test
*/
public function testDefaultCommandUsage()
{
$this->assertAttributeEquals('Command usage', 'usage', $this->command_stub);
$this->assertEquals('Command usage', $this->command_stub->getUsage());
}
/**
* @test
*/
public function testDefaultCommandVersion()
{
$this->assertAttributeEquals('1.0.0', 'version', $this->command_stub);
$this->assertEquals('1.0.0', $this->command_stub->getVersion());
}
/**
* @test
*/
public function testDefaultCommandIsEnabled()
{
$this->assertAttributeEquals(true, 'enabled', $this->command_stub);
$this->assertTrue($this->command_stub->isEnabled());
}
/**
* @test
*/
public function testDefaultCommandNeedsMysql()
{
$this->assertAttributeEquals(false, 'need_mysql', $this->command_stub);
}
/**
* @test
*/
public function testDefaultCommandEmptyConfig()
{
$this->assertAttributeEquals([], 'config', $this->command_stub);
}
/**
* @test
*/
public function testDefaultCommandUpdateNull()
{
$this->assertAttributeEquals(null, 'update', $this->command_stub);
}
/**
* @test
*/
public function testCommandSetUpdateAndMessage()
{
$stub = $this->command_stub;
......@@ -174,17 +158,11 @@ class CommandTest extends TestCase
$this->assertEquals($message, $stub->getMessage());
}
/**
* @test
*/
public function testCommandWithConfigNotEmptyConfig()
{
$this->assertAttributeNotEmpty('config', $this->command_stub_with_config);
}
/**
* @test
*/
public function testCommandWithConfigCorrectConfig()
{
$this->assertAttributeEquals(['config_key' => 'config_value'], 'config', $this->command_stub_with_config);
......
......@@ -22,20 +22,28 @@ use Longman\TelegramBot\Telegram;
*/
class CommandTestCase extends TestCase
{
/**
* @var \Longman\TelegramBot\Telegram
*/
protected $telegram;
/**
* @var \Longman\TelegramBot\Commands\Command
*/
protected $command;
/**
* setUp
*/
public function setUp()
{
$this->telegram = new Telegram('apikey', 'botname');
$this->telegram = new Telegram('apikey', 'testbot');
$this->telegram->addCommandsPath(BASE_COMMANDS_PATH . '/UserCommands');
$this->telegram->getCommandsList();
}
/**
* Make sure the version number is in the format x.x.x, x.x or x
*
* @test
*/
public function testVersionNumberFormat()
{
......
......@@ -24,15 +24,15 @@ use Longman\TelegramBot\Commands\UserCommands\EchoCommand;
*/
class EchoCommandTest extends CommandTestCase
{
/**
* setUp
*/
public function setUp()
{
parent::setUp();
$this->command = new EchoCommand($this->telegram);
}
/**
* @test
*/
public function testEchoCommandProperties()
{
$this->assertAttributeEquals('echo', 'name', $this->command);
......@@ -40,9 +40,6 @@ class EchoCommandTest extends CommandTestCase
$this->assertAttributeEquals('/echo <text>', 'usage', $this->command);
}
/**
* @test
*/
public function testEchoCommandExecuteWithoutParameter()
{
$text = $this->command
......@@ -60,9 +57,6 @@ class EchoCommandTest extends CommandTestCase
$this->assertEquals('Command usage: /echo <text>', $text);
}
/**
* @test
*/
public function testEchoCommandExecuteWithParameter()
{
$text = $this->command
......
......@@ -23,15 +23,15 @@ use Longman\TelegramBot\Commands\UserCommands\HelpCommand;
*/
class HelpCommandTest extends CommandTestCase
{
/**
* setUp
*/
public function setUp()
{
parent::setUp();
$this->command = new HelpCommand($this->telegram);
}
/**
* @test
*/
public function testHelpCommandProperties()
{
$this->assertAttributeEquals('help', 'name', $this->command);
......@@ -39,9 +39,6 @@ class HelpCommandTest extends CommandTestCase
$this->assertAttributeEquals('/help or /help <command>', 'usage', $this->command);
}
/**
* @test
*/
public function testHelpCommandExecuteWithoutParameter()
{
$text = $this->command
......@@ -50,14 +47,11 @@ class HelpCommandTest extends CommandTestCase
->getResult()
->getText();
$this->assertContains(
"botname v. " . $this->telegram->getVersion() . "\n\nCommands List:",
'testbot v. ' . $this->telegram->getVersion() . "\n\nCommands List:",
$text
);
}
/**
* @test
*/
public function testHelpCommandExecuteWithParameterInvalidCommand()
{
$text = $this->command
......@@ -68,9 +62,6 @@ class HelpCommandTest extends CommandTestCase
$this->assertEquals('No help available: Command /invalidcommand not found', $text);
}
/**
* @test
*/
public function testHelpCommandExecuteWithParameterValidCommand()
{
$text = $this->command
......
......@@ -34,23 +34,20 @@ class ConversationTest extends TestCase
protected function setUp()
{
$credentials = [
'host' => '127.0.0.1',
'user' => 'root',
'password' => '',
'database' => 'telegrambot',
'host' => PHPUNIT_DB_HOST,
'database' => PHPUNIT_DB_NAME,
'user' => PHPUNIT_DB_USER,
'password' => PHPUNIT_DB_PASS,
];
$this->telegram = new Telegram('testapikey', 'testbotname');
$this->telegram->enableMySQL($credentials);
$this->telegram = new Telegram('apikey', 'testbot');
$this->telegram->enableMySql($credentials);
//Make sure we start with an empty DB for each test.
TestHelpers::emptyDB($credentials);
}
/**
* @test
*/
public function conversationThatDoesntExistPropertiesSetCorrectly()
public function testConversationThatDoesntExistPropertiesSetCorrectly()
{
$conversation = new Conversation(123, 456);
$this->assertAttributeEquals(123, 'user_id', $conversation);
......@@ -58,22 +55,16 @@ class ConversationTest extends TestCase
$this->assertAttributeEquals(null, 'command', $conversation);
}
/**
* @test
*/
public function conversationThatExistsPropertiesSetCorrectly()
public function testConversationThatExistsPropertiesSetCorrectly()
{
$info = TestHelpers::startFakeConversation('command');
$info = TestHelpers::startFakeConversation();
$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()
public function testConversationThatDoesntExistWithoutCommand()
{
$conversation = new Conversation(1, 1);
$this->assertFalse($conversation->exists());
......@@ -81,42 +72,32 @@ class ConversationTest extends TestCase
}
/**
* @test
* @expectedException \Longman\TelegramBot\Exception\TelegramException
*/
public function conversationThatDoesntExistWithCommand()
public function testConversationThatDoesntExistWithCommand()
{
new Conversation(1, 1, 'command');
}
/**
* @test
*/
public function newConversationThatWontExistWithoutCommand()
public function testNewConversationThatWontExistWithoutCommand()
{
TestHelpers::startFakeConversation(null);
TestHelpers::startFakeConversation();
$conversation = new Conversation(0, 0);
$this->assertFalse($conversation->exists());
$this->assertNull($conversation->getCommand());
}
/**
* @test
*/
public function newConversationThatWillExistWithCommand()
public function testNewConversationThatWillExistWithCommand()
{
$info = TestHelpers::startFakeConversation('command');
$info = TestHelpers::startFakeConversation();
$conversation = new Conversation($info['user_id'], $info['chat_id'], 'command');
$this->assertTrue($conversation->exists());
$this->assertEquals('command', $conversation->getCommand());
}
/**
* @test
*/
public function stopConversation()
public function testStopConversation()
{
$info = TestHelpers::startFakeConversation('command');
$info = TestHelpers::startFakeConversation();
$conversation = new Conversation($info['user_id'], $info['chat_id'], 'command');
$this->assertTrue($conversation->exists());
$conversation->stop();
......@@ -125,12 +106,9 @@ class ConversationTest extends TestCase
$this->assertFalse($conversation2->exists());
}
/**
* @test
*/
public function cancelConversation()
public function testCancelConversation()
{
$info = TestHelpers::startFakeConversation('command');
$info = TestHelpers::startFakeConversation();
$conversation = new Conversation($info['user_id'], $info['chat_id'], 'command');
$this->assertTrue($conversation->exists());
$conversation->cancel();
......@@ -139,12 +117,9 @@ class ConversationTest extends TestCase
$this->assertFalse($conversation2->exists());
}
/**
* @test
*/
public function updateConversationNotes()
public function testUpdateConversationNotes()
{
$info = TestHelpers::startFakeConversation('command');
$info = TestHelpers::startFakeConversation();
$conversation = new Conversation($info['user_id'], $info['chat_id'], 'command');
$conversation->notes = 'newnote';
$conversation->update();
......
......@@ -10,43 +10,32 @@
namespace Tests\Unit;
use \Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Entities\Chat;
use Tests\TestHelpers;
/**
* @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
* @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 ChatTest extends TestCase
{
/**
* @var \Longman\TelegramBot\Telegram
* @var \Longman\TelegramBot\Entities\Chat
*/
private $chat;
/**
* setUp
*/
protected function setUp()
{
}
/**
* @test
*/
public function testChatType()
public function testChatType()
{
$this->chat = new Chat(json_decode('{"id":123,"title":null,"first_name":"john","last_name":null,"username":"null"}',true));
$this->chat = TestHelpers::getFakeChatObject();
$this->assertEquals('private', $this->chat->getType());
$this->chat = new Chat(json_decode('{"id":-123,"title":"ChatTitle","first_name":null,"last_name":null,"username":"null"}',true));
$this->chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => null]);
$this->assertEquals('group', $this->chat->getType());
$this->chat = new Chat(json_decode('{"id":-123,"type":"channel","title":"ChatTitle","first_name":null,"last_name":null,"username":"null"}',true));
$this->chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => 'channel']);
$this->assertEquals('channel', $this->chat->getType());
}
}
......@@ -10,101 +10,74 @@
namespace Tests\Unit;
use \Longman\TelegramBot\Entities\Message;
use Tests\TestHelpers;
/**
* @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
* @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 MessageTest extends TestCase
{
/**
* @var \Longman\TelegramBot\Telegram
* @var \Longman\TelegramBot\Entities\Message
*/
private $message;
/**
* setUp
*/
protected function setUp()
{
}
protected function generateMessage($string) {
$string = str_replace("\n", "\\n", $string);
$json = '{"message_id":961,"from":{"id":123,"first_name":"john","username":"john"},"chat":{"id":123,"title":null,"first_name":"john","last_name":null,"username":"null"},"date":1435920612,"text":"'.$string.'"}';
//$json = utf8_encode($json);
return json_decode($json, true);
}
/**
* @test
*/
public function testTextAndCommandRecognise() {
// /command
$this->message = new Message($this->generateMessage('/help'), 'testbot');
$this->message = TestHelpers::getFakeMessageObject(['text' => '/help']);
$this->assertEquals('/help', $this->message->getFullCommand());
$this->assertEquals('help', $this->message->getCommand());
$this->assertEquals('/help', $this->message->getText());
$this->assertEquals('', $this->message->getText(true));
// text
$this->message = new Message($this->generateMessage('some text'), 'testbot');
$this->message = TestHelpers::getFakeMessageObject(['text' => 'some text']);
$this->assertEquals('', $this->message->getFullCommand());
$this->assertEquals('', $this->message->getCommand());
$this->assertEquals('some text', $this->message->getText());
$this->assertEquals('some text', $this->message->getText(true));
// /command@bot
$this->message = new Message($this->generateMessage('/help@testbot'), 'testbot');
$this->message = TestHelpers::getFakeMessageObject(['text' => '/help@testbot']);
$this->assertEquals('/help@testbot', $this->message->getFullCommand());
$this->assertEquals('help', $this->message->getCommand());
$this->assertEquals('/help@testbot', $this->message->getText());
$this->assertEquals('', $this->message->getText(true));
// /commmad text
$this->message = new Message($this->generateMessage('/help some text'), 'testbot');
$this->message = TestHelpers::getFakeMessageObject(['text' => '/help some text']);
$this->assertEquals('/help', $this->message->getFullCommand());
$this->assertEquals('help', $this->message->getCommand());
$this->assertEquals('/help some text', $this->message->getText());
$this->assertEquals('some text', $this->message->getText(true));
// /command@bot some text
$this->message = new Message($this->generateMessage('/help@testbot some text'), 'testbot');
$this->message = TestHelpers::getFakeMessageObject(['text' => '/help@testbot some text']);
$this->assertEquals('/help@testbot', $this->message->getFullCommand());
$this->assertEquals('help', $this->message->getCommand());
$this->assertEquals('/help@testbot some text', $this->message->getText());
$this->assertEquals('some text', $this->message->getText(true));
// /commmad\n text
//$array = $this->generateMessage("/help\n some text");
////print_r($this->generateMessage('/help@testbot'));
//echo 'value:';
//print_r($array);
$this->message = new Message($this->generateMessage("/help\n some text"), 'testbot');
$this->message = TestHelpers::getFakeMessageObject(['text' => "/help\n some text"]);
$this->assertEquals('/help', $this->message->getFullCommand());
$this->assertEquals('help', $this->message->getCommand());
$this->assertEquals("/help\n some text", $this->message->getText());
$this->assertEquals(' some text', $this->message->getText(true));
// /command@bot\nsome text
$this->message = new Message($this->generateMessage("/help@testbot\nsome text"), 'testbot');
$this->message = TestHelpers::getFakeMessageObject(['text' => "/help@testbot\nsome text"]);
$this->assertEquals('/help@testbot', $this->message->getFullCommand());
$this->assertEquals('help', $this->message->getCommand());
$this->assertEquals("/help@testbot\nsome text", $this->message->getText());
$this->assertEquals('some text', $this->message->getText(true));
// /command@bot \nsome text
$this->message = new Message($this->generateMessage("/help@testbot \nsome text"), 'testbot');
$this->message = TestHelpers::getFakeMessageObject(['text' => "/help@testbot \nsome text"]);
$this->assertEquals('/help@testbot', $this->message->getFullCommand());
$this->assertEquals('help', $this->message->getCommand());
$this->assertEquals("/help@testbot \nsome text", $this->message->getText());
......
......@@ -14,32 +14,32 @@ use \Longman\TelegramBot\Entities\Update;
use \Longman\TelegramBot\Entities\ReplyToMessage;
/**
* @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
* @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 ReplyToMessageTest extends TestCase
{
/**
* @var \Longman\TelegramBot\Telegram
*/
* @var \Longman\TelegramBot\Entities\Message
*/
private $reply_to_message;
private $message;
/**
* @test
* @var \Longman\TelegramBot\Entities\Message
*/
public function testChatType()
private $message;
public function testChatType()
{
$json = '
{"update_id":137809335,
"message":{"message_id":4479,"from":{"id":123,"first_name":"John","username":"MJohn"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092987,"reply_to_message":{"message_id":11,"from":{"id":121,"first_name":"Myname","username":"mybot"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092984,"text":"type some text"},"text":"some text"}}
';
$struct = json_decode($json, true);
$update = new Update($struct,'mybot');
$update = new Update($struct, 'mybot');
$this->message = $update->getMessage();
$this->reply_to_message = $this->message->getReplyToMessage();
......
This diff is collapsed.
......@@ -13,24 +13,20 @@ namespace Tests\Unit;
use \Longman\TelegramBot\Entities\Update;
/**
* @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
* @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 UpdateTest extends TestCase
{
/**
* @var \Longman\TelegramBot\Telegram
* @var \Longman\TelegramBot\Entities\Update
*/
private $update;
/**
* @test
*/
public function testUpdateCast()
public function testUpdateCast()
{
$json = '
{"update_id":137809336,
......@@ -39,8 +35,7 @@ class UpdateTest extends TestCase
$struct = json_decode($json, true);
$update = new Update($struct, 'mybot');
$array_string_after = json_decode($update->toJSON(), true);
$array_string_after = json_decode($update->toJson(), true);
$this->assertEquals($struct, $array_string_after);
}
}
......@@ -26,13 +26,13 @@ use Tests\TestHelpers;
class TelegramLogTest extends TestCase
{
/**
* Logfile paths
* @var array Dummy logfile paths
*/
private $logfiles = [
'error' => '/tmp/errorlog.log',
'debug' => '/tmp/debuglog.log',
'update' => '/tmp/updatelog.log',
'external' => '/tmp/externallog.log',
'error' => '/tmp/php-telegram-bot-errorlog.log',
'debug' => '/tmp/php-telegram-bot-debuglog.log',
'update' => '/tmp/php-telegram-bot-updatelog.log',
'external' => '/tmp/php-telegram-bot-externallog.log',
];
/**
......@@ -56,78 +56,63 @@ class TelegramLogTest extends TestCase
}
/**
* @test
* @expectedException \Longman\TelegramBot\Exception\TelegramLogException
*/
public function newInstanceWithoutErrorPath()
public function testNewInstanceWithoutErrorPath()
{
TelegramLog::initErrorLog('');
}
/**
* @test
* @expectedException \Longman\TelegramBot\Exception\TelegramLogException
*/
public function newInstanceWithoutDebugPath()
public function testNewInstanceWithoutDebugPath()
{
TelegramLog::initDebugLog('');
}
/**
* @test
* @expectedException \Longman\TelegramBot\Exception\TelegramLogException
*/
public function newInstanceWithoutUpdatePath()
public function testNewInstanceWithoutUpdatePath()
{
TelegramLog::initUpdateLog('');
}
/**
* @test
*/
public function testErrorStream()
{
$file = $this->logfiles['error'];
$this->assertFalse(file_exists($file));
$this->assertFileNotExists($file);
TelegramLog::initErrorLog($file);
TelegramLog::error('my error');
$this->assertTrue(file_exists($file));
$this->assertFileExists($file);
$this->assertContains('bot_log.ERROR: my error', file_get_contents($file));
}
/**
* @test
*/
public function testDebugStream()
{
$file = $this->logfiles['debug'];
$this->assertFalse(file_exists($file));
$this->assertFileNotExists($file);
TelegramLog::initDebugLog($file);
TelegramLog::debug('my debug');
$this->assertTrue(file_exists($file));
$this->assertFileExists($file);
$this->assertContains('bot_log.DEBUG: my debug', file_get_contents($file));
}
/**
* @test
*/
public function testUpdateStream()
{
$file = $this->logfiles['update'];
$this->assertFalse(file_exists($file));
$this->assertFileNotExists($file);
TelegramLog::initUpdateLog($file);
TelegramLog::update('my update');
$this->assertTrue(file_exists($file));
$this->assertFileExists($file);
$this->assertContains('my update', file_get_contents($file));
}
/**
* @test
*/
public function testExternalStream()
{
$file = $this->logfiles['external'];
$this->assertFalse(file_exists($file));
$this->assertFileNotExists($file);
$external_monolog = new Logger('bot_update_log');
$external_monolog->pushHandler(new StreamHandler($file, Logger::ERROR));
......@@ -137,7 +122,7 @@ class TelegramLogTest extends TestCase
TelegramLog::error('my error');
TelegramLog::debug('my debug');
$this->assertTrue(file_exists($file));
$this->assertFileExists($file);
$file_contents = file_get_contents($file);
$this->assertContains('bot_update_log.ERROR: my error', $file_contents);
$this->assertContains('bot_update_log.DEBUG: my debug', $file_contents);
......
......@@ -27,7 +27,7 @@ class TelegramTest extends TestCase
private $telegram;
/**
* @var array
* @var array A few dummy custom commands paths
*/
private $custom_commands_paths = [
'/tmp/php-telegram-bot-custom-commands-1',
......@@ -40,9 +40,9 @@ class TelegramTest extends TestCase
*/
protected function setUp()
{
$this->telegram = new Telegram('testapikey', 'testbotname');
$this->telegram = new Telegram('apikey', 'testbot');
// Create a few custom commands paths.
// Create a few dummy custom commands paths.
foreach ($this->custom_commands_paths as $custom_path) {
mkdir($custom_path);
}
......@@ -60,45 +60,34 @@ class TelegramTest extends TestCase
}
/**
* @test
* @expectedException \Longman\TelegramBot\Exception\TelegramException
*/
public function newInstanceWithoutApiKeyParam()
public function testNewInstanceWithoutApiKeyParam()
{
new Telegram(null, 'testbotname');
new Telegram(null, 'testbot');
}
/**
* @test
* @expectedException \Longman\TelegramBot\Exception\TelegramException
*/
public function newInstanceWithoutBotNameParam()
public function testNewInstanceWithoutBotNameParam()
{
new Telegram('testapikey', null);
new Telegram('apikey', null);
}
/**
* @test
*/
public function getApiKey()
public function testGetApiKey()
{
$this->assertEquals('testapikey', $this->telegram->getApiKey());
$this->assertEquals('apikey', $this->telegram->getApiKey());
}
/**
* @test
*/
public function getBotName()
public function testGetBotName()
{
$this->assertEquals('testbotname', $this->telegram->getBotName());
$this->assertEquals('testbot', $this->telegram->getBotName());
}
/**
* @test
*/
public function enableAdmins()
public function testEnableAdmins()
{
$tg = &$this->telegram;
$tg = $this->telegram;
$this->assertEmpty($tg->getAdminList());
......@@ -115,12 +104,9 @@ class TelegramTest extends TestCase
$this->assertCount(3, $tg->getAdminList());
}
/**
* @test
*/
public function addCustomCommandsPaths()
public function testAddCustomCommandsPaths()
{
$tg = &$this->telegram;
$tg = $this->telegram;
$this->assertAttributeCount(1, 'commands_paths', $tg);
......@@ -140,20 +126,14 @@ class TelegramTest extends TestCase
$this->assertAttributeCount(4, 'commands_paths', $tg);
}
/**
* @test
*/
public function getCommandsList()
public function testGetCommandsList()
{
$commands = $this->telegram->getCommandsList();
$this->assertInternalType('array', $commands);
$this->assertNotCount(0, $commands);
}
/**
* @test
*/
public function getHelpCommandObject()
public function testGetHelpCommandObject()
{
$command = $this->telegram->getCommandObject('help');
$this->assertInstanceOf('Longman\TelegramBot\Commands\UserCommands\HelpCommand', $command);
......
......@@ -14,7 +14,7 @@ class TestCase extends \PHPUnit_Framework_TestCase
{
protected function skip64BitTest()
{
if (PHP_INT_SIZE == 4) {
if (PHP_INT_SIZE === 4) {
$this->markTestSkipped(
'Skipping test that can run only on a 64-bit build of PHP.'
);
......
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