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