From c0b83b4280b2d7e70b32a9d1f3be70f283d1e2f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Tue, 5 Jul 2016 19:41:51 +0200 Subject: [PATCH 1/8] Add test database credentials to phpunit config to allow local overriding. --- phpunit.xml.dist | 6 +++++- structure.sql | 2 +- tests/Unit/ConversationTest.php | 10 +++++----- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index cc236e6f4..11fec3c10 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,7 @@ + + + + diff --git a/structure.sql b/structure.sql index b538ee415..82d22d0d2 100644 --- a/structure.sql +++ b/structure.sql @@ -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', diff --git a/tests/Unit/ConversationTest.php b/tests/Unit/ConversationTest.php index 1b79d5918..5a117b7fd 100644 --- a/tests/Unit/ConversationTest.php +++ b/tests/Unit/ConversationTest.php @@ -34,14 +34,14 @@ 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->enableMySql($credentials); //Make sure we start with an empty DB for each test. TestHelpers::emptyDB($credentials); From 61cc1a44ebae6e3477356ffcdc370c3fc8cc0227 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Wed, 6 Jul 2016 00:01:40 +0200 Subject: [PATCH 2/8] Add possibility to customise fake entity objects. Make fake entity ids random and set current date/time. --- tests/TestHelpers.php | 105 +++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 48 deletions(-) diff --git a/tests/TestHelpers.php b/tests/TestHelpers.php index d04adfd5f..3ec2c8759 100644 --- a/tests/TestHelpers.php +++ b/tests/TestHelpers.php @@ -84,21 +84,21 @@ public static function setStaticProperty($class, $property, $value) * * @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 @@ public static function getFakeUpdateObject($data = null) * * @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 @@ public static function getFakeUpdateCommandObject($command_text) /** * 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 @@ 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()); - } + + $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(); } } From a00e6914f81ae5fbeccbbd5c8211061472b24393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Wed, 6 Jul 2016 00:10:05 +0200 Subject: [PATCH 3/8] Remove all superfluous `@test` comments and rename functions to start with `test`. --- tests/Unit/Commands/CommandTest.php | 39 ----------------- tests/Unit/Commands/CommandTestCase.php | 2 - .../Commands/UserCommands/EchoCommandTest.php | 9 ---- .../Commands/UserCommands/HelpCommandTest.php | 12 ------ tests/Unit/ConversationTest.php | 43 ++++--------------- tests/Unit/Entities/ChatTest.php | 6 +-- tests/Unit/Entities/MessageTest.php | 3 -- tests/Unit/Entities/ServerResponseTest.php | 43 ------------------- tests/Unit/Entities/UpdateTest.php | 4 -- tests/Unit/TelegramLogTest.php | 21 ++------- tests/Unit/TelegramTest.php | 36 ++++------------ 11 files changed, 21 insertions(+), 197 deletions(-) diff --git a/tests/Unit/Commands/CommandTest.php b/tests/Unit/Commands/CommandTest.php index a03db43ae..3611a719f 100644 --- a/tests/Unit/Commands/CommandTest.php +++ b/tests/Unit/Commands/CommandTest.php @@ -48,9 +48,6 @@ public function setUp() $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 +69,57 @@ public function testCommandConstructorNeedsTelegramObject() } } - /** - * @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 +141,11 @@ public function testCommandSetUpdateAndMessage() $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); diff --git a/tests/Unit/Commands/CommandTestCase.php b/tests/Unit/Commands/CommandTestCase.php index 416dfae21..ea1d97f9b 100644 --- a/tests/Unit/Commands/CommandTestCase.php +++ b/tests/Unit/Commands/CommandTestCase.php @@ -34,8 +34,6 @@ public function setUp() /** * Make sure the version number is in the format x.x.x, x.x or x - * - * @test */ public function testVersionNumberFormat() { diff --git a/tests/Unit/Commands/UserCommands/EchoCommandTest.php b/tests/Unit/Commands/UserCommands/EchoCommandTest.php index 27eae07c0..d84ed6b03 100644 --- a/tests/Unit/Commands/UserCommands/EchoCommandTest.php +++ b/tests/Unit/Commands/UserCommands/EchoCommandTest.php @@ -30,9 +30,6 @@ public function setUp() $this->command = new EchoCommand($this->telegram); } - /** - * @test - */ public function testEchoCommandProperties() { $this->assertAttributeEquals('echo', 'name', $this->command); @@ -40,9 +37,6 @@ public function testEchoCommandProperties() $this->assertAttributeEquals('/echo ', 'usage', $this->command); } - /** - * @test - */ public function testEchoCommandExecuteWithoutParameter() { $text = $this->command @@ -60,9 +54,6 @@ public function testEchoCommandExecuteWithoutParameter() $this->assertEquals('Command usage: /echo ', $text); } - /** - * @test - */ public function testEchoCommandExecuteWithParameter() { $text = $this->command diff --git a/tests/Unit/Commands/UserCommands/HelpCommandTest.php b/tests/Unit/Commands/UserCommands/HelpCommandTest.php index dbfcccc45..6ddb7052f 100644 --- a/tests/Unit/Commands/UserCommands/HelpCommandTest.php +++ b/tests/Unit/Commands/UserCommands/HelpCommandTest.php @@ -29,9 +29,6 @@ public function setUp() $this->command = new HelpCommand($this->telegram); } - /** - * @test - */ public function testHelpCommandProperties() { $this->assertAttributeEquals('help', 'name', $this->command); @@ -39,9 +36,6 @@ public function testHelpCommandProperties() $this->assertAttributeEquals('/help or /help ', 'usage', $this->command); } - /** - * @test - */ public function testHelpCommandExecuteWithoutParameter() { $text = $this->command @@ -55,9 +49,6 @@ public function testHelpCommandExecuteWithoutParameter() ); } - /** - * @test - */ public function testHelpCommandExecuteWithParameterInvalidCommand() { $text = $this->command @@ -68,9 +59,6 @@ public function testHelpCommandExecuteWithParameterInvalidCommand() $this->assertEquals('No help available: Command /invalidcommand not found', $text); } - /** - * @test - */ public function testHelpCommandExecuteWithParameterValidCommand() { $text = $this->command diff --git a/tests/Unit/ConversationTest.php b/tests/Unit/ConversationTest.php index 5a117b7fd..49b889eca 100644 --- a/tests/Unit/ConversationTest.php +++ b/tests/Unit/ConversationTest.php @@ -47,10 +47,7 @@ protected function setUp() TestHelpers::emptyDB($credentials); } - /** - * @test - */ - public function conversationThatDoesntExistPropertiesSetCorrectly() + public function testConversationThatDoesntExistPropertiesSetCorrectly() { $conversation = new Conversation(123, 456); $this->assertAttributeEquals(123, 'user_id', $conversation); @@ -58,10 +55,7 @@ public function conversationThatDoesntExistPropertiesSetCorrectly() $this->assertAttributeEquals(null, 'command', $conversation); } - /** - * @test - */ - public function conversationThatExistsPropertiesSetCorrectly() + public function testConversationThatExistsPropertiesSetCorrectly() { $info = TestHelpers::startFakeConversation('command'); $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); @@ -70,10 +64,7 @@ public function conversationThatExistsPropertiesSetCorrectly() $this->assertAttributeEquals('command', 'command', $conversation); } - /** - * @test - */ - public function conversationThatDoesntExistWithoutCommand() + public function testConversationThatDoesntExistWithoutCommand() { $conversation = new Conversation(1, 1); $this->assertFalse($conversation->exists()); @@ -81,18 +72,14 @@ public function conversationThatDoesntExistWithoutCommand() } /** - * @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); $conversation = new Conversation(0, 0); @@ -100,10 +87,7 @@ public function newConversationThatWontExistWithoutCommand() $this->assertNull($conversation->getCommand()); } - /** - * @test - */ - public function newConversationThatWillExistWithCommand() + public function testNewConversationThatWillExistWithCommand() { $info = TestHelpers::startFakeConversation('command'); $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); @@ -111,10 +95,7 @@ public function newConversationThatWillExistWithCommand() $this->assertEquals('command', $conversation->getCommand()); } - /** - * @test - */ - public function stopConversation() + public function testStopConversation() { $info = TestHelpers::startFakeConversation('command'); $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); @@ -125,10 +106,7 @@ public function stopConversation() $this->assertFalse($conversation2->exists()); } - /** - * @test - */ - public function cancelConversation() + public function testCancelConversation() { $info = TestHelpers::startFakeConversation('command'); $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); @@ -139,10 +117,7 @@ public function cancelConversation() $this->assertFalse($conversation2->exists()); } - /** - * @test - */ - public function updateConversationNotes() + public function testUpdateConversationNotes() { $info = TestHelpers::startFakeConversation('command'); $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); diff --git a/tests/Unit/Entities/ChatTest.php b/tests/Unit/Entities/ChatTest.php index 99123ffe8..b51998653 100644 --- a/tests/Unit/Entities/ChatTest.php +++ b/tests/Unit/Entities/ChatTest.php @@ -33,11 +33,7 @@ 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)); diff --git a/tests/Unit/Entities/MessageTest.php b/tests/Unit/Entities/MessageTest.php index 3a9df595b..2a05b594f 100644 --- a/tests/Unit/Entities/MessageTest.php +++ b/tests/Unit/Entities/MessageTest.php @@ -40,9 +40,6 @@ protected function generateMessage($string) { //$json = utf8_encode($json); return json_decode($json, true); } - /** - * @test - */ public function testTextAndCommandRecognise() { // /command diff --git a/tests/Unit/Entities/ServerResponseTest.php b/tests/Unit/Entities/ServerResponseTest.php index 9933aa9ec..3eb3ed35b 100644 --- a/tests/Unit/Entities/ServerResponseTest.php +++ b/tests/Unit/Entities/ServerResponseTest.php @@ -37,10 +37,6 @@ protected function setUp() { } - /** - * @test - */ - public function sendMessageOk() { return '{ @@ -80,10 +76,6 @@ public function testSendMessageOk() { } - /** - * @test - */ - public function sendMessageFail() { return '{ @@ -105,10 +97,6 @@ public function testSendMessageFail() { $this->assertEquals('Error: Bad Request: wrong chat id', $this->server->getDescription()); } - /** - * @test - */ - public function setWebHookOk() { return '{"ok":true,"result":true,"description":"Webhook was set"}'; @@ -126,10 +114,6 @@ public function testSetWebhookOk() { $this->assertEquals('Webhook was set', $this->server->getDescription()); } - /** - * @test - */ - public function setWebHookFail() { return '{ @@ -152,12 +136,6 @@ public function testSetWebhookFail() { $this->assertEquals("Error: Bad request: htttps://domain.host.org/dir/hook.php", $this->server->getDescription()); } - - - /** - * @test - */ - public function getUpdatesArray() { return '{ @@ -210,10 +188,6 @@ public function testGetUpdatesArray() { $this->assertInstanceOf('\Longman\TelegramBot\Entities\Update', $this->server->getResult()[0]); } - /** - * @test - */ - public function getUpdatesEmpty() { return '{"ok":true,"result":[]}'; @@ -226,12 +200,6 @@ public function testGetUpdatesEmpty() { $this->assertNull($this->server->getResult()); } - - /** - * @test - */ - - public function getUserProfilePhotos() { return '{ @@ -273,12 +241,6 @@ public function testGetUserProfilePhotos() } - - /** - * @test - */ - - public function getFile() { return '{ @@ -303,11 +265,6 @@ public function testGetFile() } - - /** - * @test - */ - public function testSetGeneralTestFakeResponse() { //setWebhook ok $fake_response = Request::generateGeneralFakeServerResponse(); diff --git a/tests/Unit/Entities/UpdateTest.php b/tests/Unit/Entities/UpdateTest.php index f2fc1f029..0b76ce2a6 100644 --- a/tests/Unit/Entities/UpdateTest.php +++ b/tests/Unit/Entities/UpdateTest.php @@ -26,10 +26,6 @@ class UpdateTest extends TestCase */ private $update; - /** - * @test - */ - public function testUpdateCast() { $json = ' diff --git a/tests/Unit/TelegramLogTest.php b/tests/Unit/TelegramLogTest.php index f3f77adbb..a18b1ef6f 100644 --- a/tests/Unit/TelegramLogTest.php +++ b/tests/Unit/TelegramLogTest.php @@ -56,35 +56,29 @@ protected function tearDown() } /** - * @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']; @@ -95,9 +89,6 @@ public function testErrorStream() $this->assertContains('bot_log.ERROR: my error', file_get_contents($file)); } - /** - * @test - */ public function testDebugStream() { $file = $this->logfiles['debug']; @@ -108,9 +99,6 @@ public function testDebugStream() $this->assertContains('bot_log.DEBUG: my debug', file_get_contents($file)); } - /** - * @test - */ public function testUpdateStream() { $file = $this->logfiles['update']; @@ -121,9 +109,6 @@ public function testUpdateStream() $this->assertContains('my update', file_get_contents($file)); } - /** - * @test - */ public function testExternalStream() { $file = $this->logfiles['external']; diff --git a/tests/Unit/TelegramTest.php b/tests/Unit/TelegramTest.php index 738793433..aba043c03 100644 --- a/tests/Unit/TelegramTest.php +++ b/tests/Unit/TelegramTest.php @@ -60,43 +60,32 @@ protected function tearDown() } /** - * @test * @expectedException \Longman\TelegramBot\Exception\TelegramException */ - public function newInstanceWithoutApiKeyParam() + public function testNewInstanceWithoutApiKeyParam() { new Telegram(null, 'testbotname'); } /** - * @test * @expectedException \Longman\TelegramBot\Exception\TelegramException */ - public function newInstanceWithoutBotNameParam() + public function testNewInstanceWithoutBotNameParam() { new Telegram('testapikey', null); } - /** - * @test - */ - public function getApiKey() + public function testGetApiKey() { $this->assertEquals('testapikey', $this->telegram->getApiKey()); } - /** - * @test - */ - public function getBotName() + public function testGetBotName() { $this->assertEquals('testbotname', $this->telegram->getBotName()); } - /** - * @test - */ - public function enableAdmins() + public function testEnableAdmins() { $tg = &$this->telegram; @@ -115,10 +104,7 @@ public function enableAdmins() $this->assertCount(3, $tg->getAdminList()); } - /** - * @test - */ - public function addCustomCommandsPaths() + public function testAddCustomCommandsPaths() { $tg = &$this->telegram; @@ -140,20 +126,14 @@ public function addCustomCommandsPaths() $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); From 764633a9ee9375d65b4f41e905bfbe21750a778c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Wed, 6 Jul 2016 00:14:10 +0200 Subject: [PATCH 4/8] Fix DocBlock comments and set correct variable types. --- tests/Unit/Commands/CommandTest.php | 17 ++++++++++++++ tests/Unit/Commands/CommandTestCase.php | 10 +++++++++ .../Commands/UserCommands/EchoCommandTest.php | 3 +++ .../Commands/UserCommands/HelpCommandTest.php | 3 +++ tests/Unit/Entities/ChatTest.php | 12 +++++----- tests/Unit/Entities/MessageTest.php | 12 +++++----- tests/Unit/Entities/ReplyToMessageTest.php | 22 +++++++++---------- tests/Unit/Entities/ServerResponseTest.php | 8 +++---- tests/Unit/Entities/UpdateTest.php | 12 +++++----- tests/Unit/TelegramLogTest.php | 2 +- tests/Unit/TelegramTest.php | 2 +- 11 files changed, 68 insertions(+), 35 deletions(-) diff --git a/tests/Unit/Commands/CommandTest.php b/tests/Unit/Commands/CommandTest.php index 3611a719f..f476820ed 100644 --- a/tests/Unit/Commands/CommandTest.php +++ b/tests/Unit/Commands/CommandTest.php @@ -23,12 +23,29 @@ */ 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() diff --git a/tests/Unit/Commands/CommandTestCase.php b/tests/Unit/Commands/CommandTestCase.php index ea1d97f9b..204924cfc 100644 --- a/tests/Unit/Commands/CommandTestCase.php +++ b/tests/Unit/Commands/CommandTestCase.php @@ -22,9 +22,19 @@ */ 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'); diff --git a/tests/Unit/Commands/UserCommands/EchoCommandTest.php b/tests/Unit/Commands/UserCommands/EchoCommandTest.php index d84ed6b03..1cdc27d24 100644 --- a/tests/Unit/Commands/UserCommands/EchoCommandTest.php +++ b/tests/Unit/Commands/UserCommands/EchoCommandTest.php @@ -24,6 +24,9 @@ */ class EchoCommandTest extends CommandTestCase { + /** + * setUp + */ public function setUp() { parent::setUp(); diff --git a/tests/Unit/Commands/UserCommands/HelpCommandTest.php b/tests/Unit/Commands/UserCommands/HelpCommandTest.php index 6ddb7052f..57d985b2b 100644 --- a/tests/Unit/Commands/UserCommands/HelpCommandTest.php +++ b/tests/Unit/Commands/UserCommands/HelpCommandTest.php @@ -23,6 +23,9 @@ */ class HelpCommandTest extends CommandTestCase { + /** + * setUp + */ public function setUp() { parent::setUp(); diff --git a/tests/Unit/Entities/ChatTest.php b/tests/Unit/Entities/ChatTest.php index b51998653..b537b4ed5 100644 --- a/tests/Unit/Entities/ChatTest.php +++ b/tests/Unit/Entities/ChatTest.php @@ -13,16 +13,16 @@ use \Longman\TelegramBot\Entities\Chat; /** - * @package TelegramTest - * @author Avtandil Kikabidze - * @copyright Avtandil Kikabidze - * @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 + * @copyright Avtandil Kikabidze + * @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; diff --git a/tests/Unit/Entities/MessageTest.php b/tests/Unit/Entities/MessageTest.php index 2a05b594f..1fe559b56 100644 --- a/tests/Unit/Entities/MessageTest.php +++ b/tests/Unit/Entities/MessageTest.php @@ -13,16 +13,16 @@ use \Longman\TelegramBot\Entities\Message; /** - * @package TelegramTest - * @author Avtandil Kikabidze - * @copyright Avtandil Kikabidze - * @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 + * @copyright Avtandil Kikabidze + * @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; diff --git a/tests/Unit/Entities/ReplyToMessageTest.php b/tests/Unit/Entities/ReplyToMessageTest.php index f6918d350..3c1c96417 100644 --- a/tests/Unit/Entities/ReplyToMessageTest.php +++ b/tests/Unit/Entities/ReplyToMessageTest.php @@ -14,25 +14,25 @@ use \Longman\TelegramBot\Entities\ReplyToMessage; /** - * @package TelegramTest - * @author Avtandil Kikabidze - * @copyright Avtandil Kikabidze - * @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 + * @copyright Avtandil Kikabidze + * @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, diff --git a/tests/Unit/Entities/ServerResponseTest.php b/tests/Unit/Entities/ServerResponseTest.php index 3eb3ed35b..286bf6aaf 100644 --- a/tests/Unit/Entities/ServerResponseTest.php +++ b/tests/Unit/Entities/ServerResponseTest.php @@ -18,15 +18,15 @@ /** * @package TelegramTest - * @author Avtandil Kikabidze - * @copyright Avtandil Kikabidze + * @author Avtandil Kikabidze + * @copyright Avtandil Kikabidze * @license http://opensource.org/licenses/mit-license.php The MIT License (MIT) - * @link http://www.github.com/akalongman/php-telegram-bot + * @link http://www.github.com/akalongman/php-telegram-bot */ class ServerResponseTest extends TestCase { /** - * @var \Longman\TelegramBot\Telegram + * @var \Longman\TelegramBot\Entities\ServerResponse */ private $server; diff --git a/tests/Unit/Entities/UpdateTest.php b/tests/Unit/Entities/UpdateTest.php index 0b76ce2a6..521fb3e8f 100644 --- a/tests/Unit/Entities/UpdateTest.php +++ b/tests/Unit/Entities/UpdateTest.php @@ -13,16 +13,16 @@ use \Longman\TelegramBot\Entities\Update; /** - * @package TelegramTest - * @author Avtandil Kikabidze - * @copyright Avtandil Kikabidze - * @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 + * @copyright Avtandil Kikabidze + * @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; diff --git a/tests/Unit/TelegramLogTest.php b/tests/Unit/TelegramLogTest.php index a18b1ef6f..1b3814dfd 100644 --- a/tests/Unit/TelegramLogTest.php +++ b/tests/Unit/TelegramLogTest.php @@ -26,7 +26,7 @@ class TelegramLogTest extends TestCase { /** - * Logfile paths + * @var array Dummy logfile paths */ private $logfiles = [ 'error' => '/tmp/errorlog.log', diff --git a/tests/Unit/TelegramTest.php b/tests/Unit/TelegramTest.php index aba043c03..91165acad 100644 --- a/tests/Unit/TelegramTest.php +++ b/tests/Unit/TelegramTest.php @@ -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', From 9a66af0d2ac91cc976267c897d41db7b443fc44d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Wed, 6 Jul 2016 00:19:16 +0200 Subject: [PATCH 5/8] Set test bot name to "testbot" and it's API key to "apikey". --- tests/Unit/Commands/CommandTest.php | 4 ++-- tests/Unit/Commands/CommandTestCase.php | 2 +- tests/Unit/Commands/UserCommands/HelpCommandTest.php | 2 +- tests/Unit/ConversationTest.php | 2 +- tests/Unit/TelegramTest.php | 10 +++++----- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/Unit/Commands/CommandTest.php b/tests/Unit/Commands/CommandTest.php index f476820ed..9ccc82437 100644 --- a/tests/Unit/Commands/CommandTest.php +++ b/tests/Unit/Commands/CommandTest.php @@ -51,11 +51,11 @@ class CommandTest extends TestCase 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() diff --git a/tests/Unit/Commands/CommandTestCase.php b/tests/Unit/Commands/CommandTestCase.php index 204924cfc..136ce959e 100644 --- a/tests/Unit/Commands/CommandTestCase.php +++ b/tests/Unit/Commands/CommandTestCase.php @@ -37,7 +37,7 @@ class CommandTestCase extends TestCase */ 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(); } diff --git a/tests/Unit/Commands/UserCommands/HelpCommandTest.php b/tests/Unit/Commands/UserCommands/HelpCommandTest.php index 57d985b2b..924a3b5ab 100644 --- a/tests/Unit/Commands/UserCommands/HelpCommandTest.php +++ b/tests/Unit/Commands/UserCommands/HelpCommandTest.php @@ -47,7 +47,7 @@ public function testHelpCommandExecuteWithoutParameter() ->getResult() ->getText(); $this->assertContains( - "botname v. " . $this->telegram->getVersion() . "\n\nCommands List:", + 'testbot v. ' . $this->telegram->getVersion() . "\n\nCommands List:", $text ); } diff --git a/tests/Unit/ConversationTest.php b/tests/Unit/ConversationTest.php index 49b889eca..90df1699a 100644 --- a/tests/Unit/ConversationTest.php +++ b/tests/Unit/ConversationTest.php @@ -40,7 +40,7 @@ protected function setUp() 'password' => PHPUNIT_DB_PASS, ]; - $this->telegram = new Telegram('testapikey', 'testbotname'); + $this->telegram = new Telegram('apikey', 'testbot'); $this->telegram->enableMySql($credentials); //Make sure we start with an empty DB for each test. diff --git a/tests/Unit/TelegramTest.php b/tests/Unit/TelegramTest.php index 91165acad..0305b59c5 100644 --- a/tests/Unit/TelegramTest.php +++ b/tests/Unit/TelegramTest.php @@ -40,7 +40,7 @@ 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. foreach ($this->custom_commands_paths as $custom_path) { @@ -64,7 +64,7 @@ protected function tearDown() */ public function testNewInstanceWithoutApiKeyParam() { - new Telegram(null, 'testbotname'); + new Telegram(null, 'testbot'); } /** @@ -72,17 +72,17 @@ public function testNewInstanceWithoutApiKeyParam() */ public function testNewInstanceWithoutBotNameParam() { - new Telegram('testapikey', null); + new Telegram('apikey', null); } public function testGetApiKey() { - $this->assertEquals('testapikey', $this->telegram->getApiKey()); + $this->assertEquals('apikey', $this->telegram->getApiKey()); } public function testGetBotName() { - $this->assertEquals('testbotname', $this->telegram->getBotName()); + $this->assertEquals('testbot', $this->telegram->getBotName()); } public function testEnableAdmins() From 75f72d54661bcaf08d0f7bffbec24917fe7df140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Wed, 6 Jul 2016 00:21:32 +0200 Subject: [PATCH 6/8] Use better PHPUnit assertions for TelegramLog tests. --- tests/Unit/TelegramLogTest.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/Unit/TelegramLogTest.php b/tests/Unit/TelegramLogTest.php index 1b3814dfd..9fbc51218 100644 --- a/tests/Unit/TelegramLogTest.php +++ b/tests/Unit/TelegramLogTest.php @@ -29,10 +29,10 @@ class TelegramLogTest extends TestCase * @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', ]; /** @@ -82,37 +82,37 @@ public function testNewInstanceWithoutUpdatePath() 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)); } 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)); } 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)); } 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)); @@ -122,7 +122,7 @@ public function testExternalStream() 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); From 68660025fae9a6a862cc3806b0388ab4513ad568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Wed, 6 Jul 2016 00:24:29 +0200 Subject: [PATCH 7/8] Make more use of the TestHelpers functions. --- tests/Unit/ConversationTest.php | 12 +++++----- tests/Unit/Entities/ChatTest.php | 10 ++++----- tests/Unit/Entities/MessageTest.php | 35 ++++++++--------------------- 3 files changed, 20 insertions(+), 37 deletions(-) diff --git a/tests/Unit/ConversationTest.php b/tests/Unit/ConversationTest.php index 90df1699a..56931addc 100644 --- a/tests/Unit/ConversationTest.php +++ b/tests/Unit/ConversationTest.php @@ -57,7 +57,7 @@ public function testConversationThatDoesntExistPropertiesSetCorrectly() 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); @@ -81,7 +81,7 @@ public function testConversationThatDoesntExistWithCommand() public function testNewConversationThatWontExistWithoutCommand() { - TestHelpers::startFakeConversation(null); + TestHelpers::startFakeConversation(); $conversation = new Conversation(0, 0); $this->assertFalse($conversation->exists()); $this->assertNull($conversation->getCommand()); @@ -89,7 +89,7 @@ public function testNewConversationThatWontExistWithoutCommand() 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()); @@ -97,7 +97,7 @@ public function testNewConversationThatWillExistWithCommand() 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(); @@ -108,7 +108,7 @@ public function testStopConversation() 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(); @@ -119,7 +119,7 @@ public function testCancelConversation() 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(); diff --git a/tests/Unit/Entities/ChatTest.php b/tests/Unit/Entities/ChatTest.php index b537b4ed5..6fa11a30d 100644 --- a/tests/Unit/Entities/ChatTest.php +++ b/tests/Unit/Entities/ChatTest.php @@ -10,7 +10,8 @@ namespace Tests\Unit; -use \Longman\TelegramBot\Entities\Chat; +use Longman\TelegramBot\Entities\Chat; +use Tests\TestHelpers; /** * @package TelegramTest @@ -35,14 +36,13 @@ protected function setUp() 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()); } } diff --git a/tests/Unit/Entities/MessageTest.php b/tests/Unit/Entities/MessageTest.php index 1fe559b56..64dac63d7 100644 --- a/tests/Unit/Entities/MessageTest.php +++ b/tests/Unit/Entities/MessageTest.php @@ -10,7 +10,7 @@ namespace Tests\Unit; -use \Longman\TelegramBot\Entities\Message; +use Tests\TestHelpers; /** * @package TelegramTest @@ -33,75 +33,58 @@ 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); - } - 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()); From f66fcdaa8ab536706aa8a3d9852b5ab603ff0fb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Wed, 6 Jul 2016 00:27:01 +0200 Subject: [PATCH 8/8] Smaller fixes like typos and code presentation inconsistencies. Add some helper variables to tests, to cut down the amount of code a bit and make it more readable. --- tests/Unit/Entities/ChatTest.php | 7 -- tests/Unit/Entities/MessageTest.php | 7 -- tests/Unit/Entities/ReplyToMessageTest.php | 2 +- tests/Unit/Entities/ServerResponseTest.php | 138 ++++++++++----------- tests/Unit/Entities/UpdateTest.php | 5 +- tests/Unit/TelegramTest.php | 6 +- tests/Unit/TestCase.php | 2 +- 7 files changed, 70 insertions(+), 97 deletions(-) diff --git a/tests/Unit/Entities/ChatTest.php b/tests/Unit/Entities/ChatTest.php index 6fa11a30d..326fc34a1 100644 --- a/tests/Unit/Entities/ChatTest.php +++ b/tests/Unit/Entities/ChatTest.php @@ -26,13 +26,6 @@ class ChatTest extends TestCase * @var \Longman\TelegramBot\Entities\Chat */ private $chat; - - /** - * setUp - */ - protected function setUp() - { - } public function testChatType() { diff --git a/tests/Unit/Entities/MessageTest.php b/tests/Unit/Entities/MessageTest.php index 64dac63d7..8e0463b82 100644 --- a/tests/Unit/Entities/MessageTest.php +++ b/tests/Unit/Entities/MessageTest.php @@ -26,13 +26,6 @@ class MessageTest extends TestCase */ private $message; - /** - * setUp - */ - protected function setUp() - { - } - public function testTextAndCommandRecognise() { // /command $this->message = TestHelpers::getFakeMessageObject(['text' => '/help']); diff --git a/tests/Unit/Entities/ReplyToMessageTest.php b/tests/Unit/Entities/ReplyToMessageTest.php index 3c1c96417..4b97421e4 100644 --- a/tests/Unit/Entities/ReplyToMessageTest.php +++ b/tests/Unit/Entities/ReplyToMessageTest.php @@ -39,7 +39,7 @@ public function testChatType() "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(); diff --git a/tests/Unit/Entities/ServerResponseTest.php b/tests/Unit/Entities/ServerResponseTest.php index 286bf6aaf..0d7a4a5c4 100644 --- a/tests/Unit/Entities/ServerResponseTest.php +++ b/tests/Unit/Entities/ServerResponseTest.php @@ -30,13 +30,6 @@ class ServerResponseTest extends TestCase */ private $server; - /** - * setUp - */ - protected function setUp() - { - } - public function sendMessageOk() { return '{ @@ -47,31 +40,30 @@ public function sendMessageOk() "chat":{"id":123456789,"first_name":"john","username":"Mjohn"}, "date":1441378360, "text":"hello" - } - }'; + } + }'; } public function testSendMessageOk() { - $result = $this->sendMessageOk(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $server_result = $this->server->getResult(); $this->assertTrue($this->server->isOk()); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\Message', $this->server->getResult()); $this->assertNull($this->server->getErrorCode()); $this->assertNull($this->server->getDescription()); + $this->assertInstanceOf('\Longman\TelegramBot\Entities\Message', $server_result); //Message - $this->assertEquals('1234', $this->server->getResult()->getMessageId()); - $this->assertEquals('123456789', $this->server->getResult()->getFrom()->getId()); - $this->assertEquals('botname', $this->server->getResult()->getFrom()->getFirstName()); - $this->assertEquals('namebot', $this->server->getResult()->getFrom()->getUserName()); - $this->assertEquals('123456789', $this->server->getResult()->getChat()->getId()); - $this->assertEquals('john', $this->server->getResult()->getChat()->getFirstName()); - $this->assertEquals('Mjohn', $this->server->getResult()->getChat()->getUserName()); - $this->assertEquals('1441378360', $this->server->getResult()->getDate()); - $this->assertEquals('hello', $this->server->getResult()->getText()); + $this->assertEquals('1234', $server_result->getMessageId()); + $this->assertEquals('123456789', $server_result->getFrom()->getId()); + $this->assertEquals('botname', $server_result->getFrom()->getFirstName()); + $this->assertEquals('namebot', $server_result->getFrom()->getUserName()); + $this->assertEquals('123456789', $server_result->getChat()->getId()); + $this->assertEquals('john', $server_result->getChat()->getFirstName()); + $this->assertEquals('Mjohn', $server_result->getChat()->getUserName()); + $this->assertEquals('1441378360', $server_result->getDate()); + $this->assertEquals('hello', $server_result->getText()); //... they are not finished... } @@ -79,16 +71,14 @@ public function testSendMessageOk() { public function sendMessageFail() { return '{ - "ok":false, - "error_code":400, - "description":"Error: Bad Request: wrong chat id" - }'; + "ok":false, + "error_code":400, + "description":"Error: Bad Request: wrong chat id" + }'; } public function testSendMessageFail() { - $result = $this->sendMessageFail(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); $this->assertFalse($this->server->isOk()); @@ -97,15 +87,13 @@ public function testSendMessageFail() { $this->assertEquals('Error: Bad Request: wrong chat id', $this->server->getDescription()); } - public function setWebHookOk() + public function setWebhookOk() { return '{"ok":true,"result":true,"description":"Webhook was set"}'; } public function testSetWebhookOk() { - $result = $this->setWebhookOk(); - $this->server = new ServerResponse(json_decode($result, true), 'testbot'); $this->assertTrue($this->server->isOk()); @@ -114,20 +102,17 @@ public function testSetWebhookOk() { $this->assertEquals('Webhook was set', $this->server->getDescription()); } - public function setWebHookFail() + public function setWebhookFail() { return '{ "ok":false, "error_code":400, "description":"Error: Bad request: htttps:\/\/domain.host.org\/dir\/hook.php" - }'; + }'; } - public function testSetWebhookFail() { - - $result = $this->setWebHookFail(); - + $result = $this->setWebhookFail(); $this->server = new ServerResponse(json_decode($result, true), 'testbot'); $this->assertFalse($this->server->isOk()); @@ -141,50 +126,55 @@ public function getUpdatesArray() return '{ "ok":true, "result":[ - {"update_id":123, + { + "update_id":123, "message":{ "message_id":90, "from":{"id":123456789,"first_name":"John","username":"Mjohn"}, "chat":{"id":123456789,"first_name":"John","username":"Mjohn"}, "date":1441569067, - "text":"\/start"} + "text":"\/start" + } }, - {"update_id":124, + { + "update_id":124, "message":{ "message_id":91, "from":{"id":123456788,"first_name":"Patrizia","username":"Patry"}, "chat":{"id":123456788,"first_name":"Patrizia","username":"Patry"}, "date":1441569073, - "text":"Hello!"} - }, - {"update_id":125, + "text":"Hello!" + } + }, + { + "update_id":125, "message":{ "message_id":92, "from":{"id":123456789,"first_name":"John","username":"MJohn"}, "chat":{"id":123456789,"first_name":"John","username":"MJohn"}, "date":1441569094, - "text":"\/echo hello!"} - }, - {"update_id":126, - "message":{ - "message_id":93, - "from":{"id":123456788,"first_name":"Patrizia","username":"Patry"}, - "chat":{"id":123456788,"first_name":"Patrizia","username":"Patry"}, - "date":1441569112, - "text":"\/echo the best" + "text":"\/echo hello!" + } + }, + { + "update_id":126, + "message":{ + "message_id":93, + "from":{"id":123456788,"first_name":"Patrizia","username":"Patry"}, + "chat":{"id":123456788,"first_name":"Patrizia","username":"Patry"}, + "date":1441569112, + "text":"\/echo the best" } } ] }'; } - public function testGetUpdatesArray() { $result = $this->getUpdatesArray(); $this->server = new ServerResponse(json_decode($result, true), 'testbot'); $this->assertCount(4, $this->server->getResult()); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\Update', $this->server->getResult()[0]); } @@ -193,10 +183,10 @@ public function getUpdatesEmpty() return '{"ok":true,"result":[]}'; } - public function testGetUpdatesEmpty() { $result = $this->getUpdatesEmpty(); $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $this->assertNull($this->server->getResult()); } @@ -227,18 +217,20 @@ public function getUserProfilePhotos() }'; } - public function testGetUserProfilePhotos() { $result = $this->getUserProfilePhotos(); $this->server = new ServerResponse(json_decode($result, true), 'testbot'); + $server_result = $this->server->getResult(); + $photos = $server_result->getPhotos(); - $this->assertCount(3, $this->server->getResult()->getPhotos()); - $this->assertCount(3, $this->server->getResult()->getPhotos()[0]); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\UserProfilePhotos', $this->server->getResult()); - - $this->assertInstanceOf('\Longman\TelegramBot\Entities\PhotoSize', $this->server->getResult()->getPhotos()[0][0]); + //Photo count + $this->assertCount(3, $photos); + //Photo size count + $this->assertCount(3, $photos[0]); + $this->assertInstanceOf('\Longman\TelegramBot\Entities\UserProfilePhotos', $server_result); + $this->assertInstanceOf('\Longman\TelegramBot\Entities\PhotoSize', $photos[0][0]); } public function getFile() @@ -253,16 +245,12 @@ public function getFile() }'; } - public function testGetFile() { $result = $this->getFile(); - //print_r(json_decode($result, true)); $this->server = new ServerResponse(json_decode($result, true), 'testbot'); - //var_dump($this->server->getResult()->getPhotos()); $this->assertInstanceOf('\Longman\TelegramBot\Entities\File', $this->server->getResult()); - } public function testSetGeneralTestFakeResponse() { @@ -276,29 +264,29 @@ public function testSetGeneralTestFakeResponse() { $this->assertNull($this->server->getErrorCode()); $this->assertEquals('', $this->server->getDescription()); - //sendMessage ok $fake_response = Request::generateGeneralFakeServerResponse(['chat_id' => 123456789, 'text' => 'hello']); $this->server = new ServerResponse($fake_response, 'testbot'); + $server_result = $this->server->getResult(); $this->assertTrue($this->server->isOk()); - $this->assertInstanceOf('\Longman\TelegramBot\Entities\Message', $this->server->getResult()); $this->assertNull($this->server->getErrorCode()); $this->assertNull($this->server->getDescription()); + $this->assertInstanceOf('\Longman\TelegramBot\Entities\Message', $server_result); //Message - $this->assertEquals('1234', $this->server->getResult()->getMessageId()); - $this->assertEquals('1441378360', $this->server->getResult()->getDate()); - $this->assertEquals('hello', $this->server->getResult()->getText()); + $this->assertEquals('1234', $server_result->getMessageId()); + $this->assertEquals('1441378360', $server_result->getDate()); + $this->assertEquals('hello', $server_result->getText()); //Message //User - $this->assertEquals('123456789', $this->server->getResult()->getFrom()->getId()); - $this->assertEquals('botname', $this->server->getResult()->getFrom()->getFirstName()); - $this->assertEquals('namebot', $this->server->getResult()->getFrom()->getUserName()); + $this->assertEquals('123456789', $server_result->getFrom()->getId()); + $this->assertEquals('botname', $server_result->getFrom()->getFirstName()); + $this->assertEquals('namebot', $server_result->getFrom()->getUserName()); //Message //Chat - $this->assertEquals('123456789', $this->server->getResult()->getChat()->getId()); - $this->assertEquals('', $this->server->getResult()->getChat()->getFirstName()); - $this->assertEquals('', $this->server->getResult()->getChat()->getUserName()); + $this->assertEquals('123456789', $server_result->getChat()->getId()); + $this->assertEquals('', $server_result->getChat()->getFirstName()); + $this->assertEquals('', $server_result->getChat()->getUserName()); //... they are not finished... } diff --git a/tests/Unit/Entities/UpdateTest.php b/tests/Unit/Entities/UpdateTest.php index 521fb3e8f..6b4eb4d33 100644 --- a/tests/Unit/Entities/UpdateTest.php +++ b/tests/Unit/Entities/UpdateTest.php @@ -26,7 +26,7 @@ class UpdateTest extends TestCase */ private $update; - public function testUpdateCast() + public function testUpdateCast() { $json = ' {"update_id":137809336, @@ -35,8 +35,7 @@ public function testUpdateCast() $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); - } } diff --git a/tests/Unit/TelegramTest.php b/tests/Unit/TelegramTest.php index 0305b59c5..762ed041b 100644 --- a/tests/Unit/TelegramTest.php +++ b/tests/Unit/TelegramTest.php @@ -42,7 +42,7 @@ protected function setUp() { $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); } @@ -87,7 +87,7 @@ public function testGetBotName() public function testEnableAdmins() { - $tg = &$this->telegram; + $tg = $this->telegram; $this->assertEmpty($tg->getAdminList()); @@ -106,7 +106,7 @@ public function testEnableAdmins() public function testAddCustomCommandsPaths() { - $tg = &$this->telegram; + $tg = $this->telegram; $this->assertAttributeCount(1, 'commands_paths', $tg); diff --git a/tests/Unit/TestCase.php b/tests/Unit/TestCase.php index 9dea2f5d5..f8f583bc6 100644 --- a/tests/Unit/TestCase.php +++ b/tests/Unit/TestCase.php @@ -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.' );