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/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(); } } diff --git a/tests/Unit/Commands/CommandTest.php b/tests/Unit/Commands/CommandTest.php index a03db43ae..9ccc82437 100644 --- a/tests/Unit/Commands/CommandTest.php +++ b/tests/Unit/Commands/CommandTest.php @@ -23,22 +23,39 @@ */ class CommandTest extends TestCase { + /** + * @var string + */ private $command_namespace = 'Longman\TelegramBot\Commands\Command'; + /** + * @var \Longman\TelegramBot\Telegram + */ private $telegram; + + /** + * @var \Longman\TelegramBot\Commands\Command + */ private $command_stub; + /** + * @var \Longman\TelegramBot\Telegram + */ private $telegram_with_config; + + /** + * @var \Longman\TelegramBot\Commands\Command + */ private $command_stub_with_config; public function setUp() { //Default command object - $this->telegram = new Telegram('apikey', 'botname'); + $this->telegram = new Telegram('apikey', 'testbot'); $this->command_stub = $this->getMockForAbstractClass($this->command_namespace, [$this->telegram]); //Create separate command object that contain a command config - $this->telegram_with_config = new Telegram('apikey', 'botname'); + $this->telegram_with_config = new Telegram('apikey', 'testbot'); $this->telegram_with_config->setCommandConfig('command_name', ['config_key' => 'config_value']); $this->command_stub_with_config = $this->getMockBuilder($this->command_namespace) ->disableOriginalConstructor() @@ -48,9 +65,6 @@ 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 +86,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 +158,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..136ce959e 100644 --- a/tests/Unit/Commands/CommandTestCase.php +++ b/tests/Unit/Commands/CommandTestCase.php @@ -22,20 +22,28 @@ */ class CommandTestCase extends TestCase { + /** + * @var \Longman\TelegramBot\Telegram + */ protected $telegram; + + /** + * @var \Longman\TelegramBot\Commands\Command + */ protected $command; + /** + * setUp + */ public function setUp() { - $this->telegram = new Telegram('apikey', 'botname'); + $this->telegram = new Telegram('apikey', 'testbot'); $this->telegram->addCommandsPath(BASE_COMMANDS_PATH . '/UserCommands'); $this->telegram->getCommandsList(); } /** * Make sure the version number is in the format x.x.x, x.x or x - * - * @test */ public function testVersionNumberFormat() { diff --git a/tests/Unit/Commands/UserCommands/EchoCommandTest.php b/tests/Unit/Commands/UserCommands/EchoCommandTest.php index 27eae07c0..1cdc27d24 100644 --- a/tests/Unit/Commands/UserCommands/EchoCommandTest.php +++ b/tests/Unit/Commands/UserCommands/EchoCommandTest.php @@ -24,15 +24,15 @@ */ class EchoCommandTest extends CommandTestCase { + /** + * setUp + */ public function setUp() { parent::setUp(); $this->command = new EchoCommand($this->telegram); } - /** - * @test - */ public function testEchoCommandProperties() { $this->assertAttributeEquals('echo', 'name', $this->command); @@ -40,9 +40,6 @@ public function testEchoCommandProperties() $this->assertAttributeEquals('/echo ', 'usage', $this->command); } - /** - * @test - */ public function testEchoCommandExecuteWithoutParameter() { $text = $this->command @@ -60,9 +57,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..924a3b5ab 100644 --- a/tests/Unit/Commands/UserCommands/HelpCommandTest.php +++ b/tests/Unit/Commands/UserCommands/HelpCommandTest.php @@ -23,15 +23,15 @@ */ class HelpCommandTest extends CommandTestCase { + /** + * setUp + */ public function setUp() { parent::setUp(); $this->command = new HelpCommand($this->telegram); } - /** - * @test - */ public function testHelpCommandProperties() { $this->assertAttributeEquals('help', 'name', $this->command); @@ -39,9 +39,6 @@ public function testHelpCommandProperties() $this->assertAttributeEquals('/help or /help ', 'usage', $this->command); } - /** - * @test - */ public function testHelpCommandExecuteWithoutParameter() { $text = $this->command @@ -50,14 +47,11 @@ public function testHelpCommandExecuteWithoutParameter() ->getResult() ->getText(); $this->assertContains( - "botname v. " . $this->telegram->getVersion() . "\n\nCommands List:", + 'testbot v. ' . $this->telegram->getVersion() . "\n\nCommands List:", $text ); } - /** - * @test - */ public function testHelpCommandExecuteWithParameterInvalidCommand() { $text = $this->command @@ -68,9 +62,6 @@ 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 1b79d5918..56931addc 100644 --- a/tests/Unit/ConversationTest.php +++ b/tests/Unit/ConversationTest.php @@ -34,23 +34,20 @@ class ConversationTest extends TestCase protected function setUp() { $credentials = [ - 'host' => '127.0.0.1', - 'user' => 'root', - 'password' => '', - 'database' => 'telegrambot', + 'host' => PHPUNIT_DB_HOST, + 'database' => PHPUNIT_DB_NAME, + 'user' => PHPUNIT_DB_USER, + 'password' => PHPUNIT_DB_PASS, ]; - $this->telegram = new Telegram('testapikey', 'testbotname'); - $this->telegram->enableMySQL($credentials); + $this->telegram = new Telegram('apikey', 'testbot'); + $this->telegram->enableMySql($credentials); //Make sure we start with an empty DB for each test. TestHelpers::emptyDB($credentials); } - /** - * @test - */ - public function conversationThatDoesntExistPropertiesSetCorrectly() + public function testConversationThatDoesntExistPropertiesSetCorrectly() { $conversation = new Conversation(123, 456); $this->assertAttributeEquals(123, 'user_id', $conversation); @@ -58,22 +55,16 @@ public function conversationThatDoesntExistPropertiesSetCorrectly() $this->assertAttributeEquals(null, 'command', $conversation); } - /** - * @test - */ - public function conversationThatExistsPropertiesSetCorrectly() + public function testConversationThatExistsPropertiesSetCorrectly() { - $info = TestHelpers::startFakeConversation('command'); + $info = TestHelpers::startFakeConversation(); $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); $this->assertAttributeEquals($info['user_id'], 'user_id', $conversation); $this->assertAttributeEquals($info['chat_id'], 'chat_id', $conversation); $this->assertAttributeEquals('command', 'command', $conversation); } - /** - * @test - */ - public function conversationThatDoesntExistWithoutCommand() + public function testConversationThatDoesntExistWithoutCommand() { $conversation = new Conversation(1, 1); $this->assertFalse($conversation->exists()); @@ -81,42 +72,32 @@ 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); + TestHelpers::startFakeConversation(); $conversation = new Conversation(0, 0); $this->assertFalse($conversation->exists()); $this->assertNull($conversation->getCommand()); } - /** - * @test - */ - public function newConversationThatWillExistWithCommand() + public function testNewConversationThatWillExistWithCommand() { - $info = TestHelpers::startFakeConversation('command'); + $info = TestHelpers::startFakeConversation(); $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); $this->assertTrue($conversation->exists()); $this->assertEquals('command', $conversation->getCommand()); } - /** - * @test - */ - public function stopConversation() + public function testStopConversation() { - $info = TestHelpers::startFakeConversation('command'); + $info = TestHelpers::startFakeConversation(); $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); $this->assertTrue($conversation->exists()); $conversation->stop(); @@ -125,12 +106,9 @@ public function stopConversation() $this->assertFalse($conversation2->exists()); } - /** - * @test - */ - public function cancelConversation() + public function testCancelConversation() { - $info = TestHelpers::startFakeConversation('command'); + $info = TestHelpers::startFakeConversation(); $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); $this->assertTrue($conversation->exists()); $conversation->cancel(); @@ -139,12 +117,9 @@ public function cancelConversation() $this->assertFalse($conversation2->exists()); } - /** - * @test - */ - public function updateConversationNotes() + public function testUpdateConversationNotes() { - $info = TestHelpers::startFakeConversation('command'); + $info = TestHelpers::startFakeConversation(); $conversation = new Conversation($info['user_id'], $info['chat_id'], 'command'); $conversation->notes = 'newnote'; $conversation->update(); diff --git a/tests/Unit/Entities/ChatTest.php b/tests/Unit/Entities/ChatTest.php index 99123ffe8..326fc34a1 100644 --- a/tests/Unit/Entities/ChatTest.php +++ b/tests/Unit/Entities/ChatTest.php @@ -10,43 +10,32 @@ namespace Tests\Unit; -use \Longman\TelegramBot\Entities\Chat; +use Longman\TelegramBot\Entities\Chat; +use Tests\TestHelpers; /** - * @package TelegramTest - * @author Avtandil Kikabidze - * @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; - - /** - * setUp - */ - protected function setUp() - { - } - /** - * @test - */ - - public function testChatType() + public function testChatType() { - - $this->chat = new Chat(json_decode('{"id":123,"title":null,"first_name":"john","last_name":null,"username":"null"}',true)); + $this->chat = TestHelpers::getFakeChatObject(); $this->assertEquals('private', $this->chat->getType()); - $this->chat = new Chat(json_decode('{"id":-123,"title":"ChatTitle","first_name":null,"last_name":null,"username":"null"}',true)); + $this->chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => null]); $this->assertEquals('group', $this->chat->getType()); - $this->chat = new Chat(json_decode('{"id":-123,"type":"channel","title":"ChatTitle","first_name":null,"last_name":null,"username":"null"}',true)); + $this->chat = TestHelpers::getFakeChatObject(['id' => -123, 'type' => 'channel']); $this->assertEquals('channel', $this->chat->getType()); } } diff --git a/tests/Unit/Entities/MessageTest.php b/tests/Unit/Entities/MessageTest.php index 3a9df595b..8e0463b82 100644 --- a/tests/Unit/Entities/MessageTest.php +++ b/tests/Unit/Entities/MessageTest.php @@ -10,101 +10,74 @@ namespace Tests\Unit; -use \Longman\TelegramBot\Entities\Message; +use Tests\TestHelpers; /** - * @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; - /** - * setUp - */ - protected function setUp() - { - } - - protected function generateMessage($string) { - - $string = str_replace("\n", "\\n", $string); - $json = '{"message_id":961,"from":{"id":123,"first_name":"john","username":"john"},"chat":{"id":123,"title":null,"first_name":"john","last_name":null,"username":"null"},"date":1435920612,"text":"'.$string.'"}'; - //$json = utf8_encode($json); - return json_decode($json, true); - } - /** - * @test - */ - public function testTextAndCommandRecognise() { // /command - $this->message = new Message($this->generateMessage('/help'), 'testbot'); - + $this->message = TestHelpers::getFakeMessageObject(['text' => '/help']); $this->assertEquals('/help', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals('/help', $this->message->getText()); $this->assertEquals('', $this->message->getText(true)); // text - $this->message = new Message($this->generateMessage('some text'), 'testbot'); - + $this->message = TestHelpers::getFakeMessageObject(['text' => 'some text']); $this->assertEquals('', $this->message->getFullCommand()); $this->assertEquals('', $this->message->getCommand()); $this->assertEquals('some text', $this->message->getText()); $this->assertEquals('some text', $this->message->getText(true)); - // /command@bot - - $this->message = new Message($this->generateMessage('/help@testbot'), 'testbot'); + $this->message = TestHelpers::getFakeMessageObject(['text' => '/help@testbot']); $this->assertEquals('/help@testbot', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals('/help@testbot', $this->message->getText()); $this->assertEquals('', $this->message->getText(true)); // /commmad text - $this->message = new Message($this->generateMessage('/help some text'), 'testbot'); + $this->message = TestHelpers::getFakeMessageObject(['text' => '/help some text']); $this->assertEquals('/help', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals('/help some text', $this->message->getText()); $this->assertEquals('some text', $this->message->getText(true)); // /command@bot some text - $this->message = new Message($this->generateMessage('/help@testbot some text'), 'testbot'); + $this->message = TestHelpers::getFakeMessageObject(['text' => '/help@testbot some text']); $this->assertEquals('/help@testbot', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals('/help@testbot some text', $this->message->getText()); $this->assertEquals('some text', $this->message->getText(true)); // /commmad\n text - -//$array = $this->generateMessage("/help\n some text"); -////print_r($this->generateMessage('/help@testbot')); -//echo 'value:'; -//print_r($array); - $this->message = new Message($this->generateMessage("/help\n some text"), 'testbot'); + $this->message = TestHelpers::getFakeMessageObject(['text' => "/help\n some text"]); $this->assertEquals('/help', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals("/help\n some text", $this->message->getText()); $this->assertEquals(' some text', $this->message->getText(true)); // /command@bot\nsome text - $this->message = new Message($this->generateMessage("/help@testbot\nsome text"), 'testbot'); + $this->message = TestHelpers::getFakeMessageObject(['text' => "/help@testbot\nsome text"]); $this->assertEquals('/help@testbot', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals("/help@testbot\nsome text", $this->message->getText()); $this->assertEquals('some text', $this->message->getText(true)); // /command@bot \nsome text - $this->message = new Message($this->generateMessage("/help@testbot \nsome text"), 'testbot'); + $this->message = TestHelpers::getFakeMessageObject(['text' => "/help@testbot \nsome text"]); $this->assertEquals('/help@testbot', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals("/help@testbot \nsome text", $this->message->getText()); diff --git a/tests/Unit/Entities/ReplyToMessageTest.php b/tests/Unit/Entities/ReplyToMessageTest.php index f6918d350..4b97421e4 100644 --- a/tests/Unit/Entities/ReplyToMessageTest.php +++ b/tests/Unit/Entities/ReplyToMessageTest.php @@ -14,32 +14,32 @@ 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, "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 9933aa9ec..0d7a4a5c4 100644 --- a/tests/Unit/Entities/ServerResponseTest.php +++ b/tests/Unit/Entities/ServerResponseTest.php @@ -18,29 +18,18 @@ /** * @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; - /** - * setUp - */ - protected function setUp() - { - } - - /** - * @test - */ - public function sendMessageOk() { return '{ @@ -51,52 +40,45 @@ 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... } - /** - * @test - */ - 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()); @@ -105,19 +87,13 @@ public function testSendMessageFail() { $this->assertEquals('Error: Bad Request: wrong chat id', $this->server->getDescription()); } - /** - * @test - */ - - 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()); @@ -126,24 +102,17 @@ public function testSetWebhookOk() { $this->assertEquals('Webhook was set', $this->server->getDescription()); } - /** - * @test - */ - - 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()); @@ -152,86 +121,75 @@ public function testSetWebhookFail() { $this->assertEquals("Error: Bad request: htttps://domain.host.org/dir/hook.php", $this->server->getDescription()); } - - - /** - * @test - */ - 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]); } - /** - * @test - */ - 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()); } - - /** - * @test - */ - - public function getUserProfilePhotos() { return '{ @@ -259,26 +217,22 @@ 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]); } - - /** - * @test - */ - - public function getFile() { return '{ @@ -291,23 +245,14 @@ 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()); - } - - /** - * @test - */ - public function testSetGeneralTestFakeResponse() { //setWebhook ok $fake_response = Request::generateGeneralFakeServerResponse(); @@ -319,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 f2fc1f029..6b4eb4d33 100644 --- a/tests/Unit/Entities/UpdateTest.php +++ b/tests/Unit/Entities/UpdateTest.php @@ -13,24 +13,20 @@ 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; - /** - * @test - */ - - public function testUpdateCast() + public function testUpdateCast() { $json = ' {"update_id":137809336, @@ -39,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/TelegramLogTest.php b/tests/Unit/TelegramLogTest.php index f3f77adbb..9fbc51218 100644 --- a/tests/Unit/TelegramLogTest.php +++ b/tests/Unit/TelegramLogTest.php @@ -26,13 +26,13 @@ class TelegramLogTest extends TestCase { /** - * Logfile paths + * @var array Dummy logfile paths */ private $logfiles = [ - 'error' => '/tmp/errorlog.log', - 'debug' => '/tmp/debuglog.log', - 'update' => '/tmp/updatelog.log', - 'external' => '/tmp/externallog.log', + 'error' => '/tmp/php-telegram-bot-errorlog.log', + 'debug' => '/tmp/php-telegram-bot-debuglog.log', + 'update' => '/tmp/php-telegram-bot-updatelog.log', + 'external' => '/tmp/php-telegram-bot-externallog.log', ]; /** @@ -56,78 +56,63 @@ 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']; - $this->assertFalse(file_exists($file)); + $this->assertFileNotExists($file); TelegramLog::initErrorLog($file); TelegramLog::error('my error'); - $this->assertTrue(file_exists($file)); + $this->assertFileExists($file); $this->assertContains('bot_log.ERROR: my error', file_get_contents($file)); } - /** - * @test - */ public function testDebugStream() { $file = $this->logfiles['debug']; - $this->assertFalse(file_exists($file)); + $this->assertFileNotExists($file); TelegramLog::initDebugLog($file); TelegramLog::debug('my debug'); - $this->assertTrue(file_exists($file)); + $this->assertFileExists($file); $this->assertContains('bot_log.DEBUG: my debug', file_get_contents($file)); } - /** - * @test - */ public function testUpdateStream() { $file = $this->logfiles['update']; - $this->assertFalse(file_exists($file)); + $this->assertFileNotExists($file); TelegramLog::initUpdateLog($file); TelegramLog::update('my update'); - $this->assertTrue(file_exists($file)); + $this->assertFileExists($file); $this->assertContains('my update', file_get_contents($file)); } - /** - * @test - */ public function testExternalStream() { $file = $this->logfiles['external']; - $this->assertFalse(file_exists($file)); + $this->assertFileNotExists($file); $external_monolog = new Logger('bot_update_log'); $external_monolog->pushHandler(new StreamHandler($file, Logger::ERROR)); @@ -137,7 +122,7 @@ 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); diff --git a/tests/Unit/TelegramTest.php b/tests/Unit/TelegramTest.php index 738793433..762ed041b 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', @@ -40,9 +40,9 @@ class TelegramTest extends TestCase */ protected function setUp() { - $this->telegram = new Telegram('testapikey', 'testbotname'); + $this->telegram = new Telegram('apikey', 'testbot'); - // Create a few custom commands paths. + // Create a few dummy custom commands paths. foreach ($this->custom_commands_paths as $custom_path) { mkdir($custom_path); } @@ -60,45 +60,34 @@ protected function tearDown() } /** - * @test * @expectedException \Longman\TelegramBot\Exception\TelegramException */ - public function newInstanceWithoutApiKeyParam() + public function testNewInstanceWithoutApiKeyParam() { - new Telegram(null, 'testbotname'); + new Telegram(null, 'testbot'); } /** - * @test * @expectedException \Longman\TelegramBot\Exception\TelegramException */ - public function newInstanceWithoutBotNameParam() + public function testNewInstanceWithoutBotNameParam() { - new Telegram('testapikey', null); + new Telegram('apikey', null); } - /** - * @test - */ - public function getApiKey() + public function testGetApiKey() { - $this->assertEquals('testapikey', $this->telegram->getApiKey()); + $this->assertEquals('apikey', $this->telegram->getApiKey()); } - /** - * @test - */ - public function getBotName() + public function testGetBotName() { - $this->assertEquals('testbotname', $this->telegram->getBotName()); + $this->assertEquals('testbot', $this->telegram->getBotName()); } - /** - * @test - */ - public function enableAdmins() + public function testEnableAdmins() { - $tg = &$this->telegram; + $tg = $this->telegram; $this->assertEmpty($tg->getAdminList()); @@ -115,12 +104,9 @@ public function enableAdmins() $this->assertCount(3, $tg->getAdminList()); } - /** - * @test - */ - public function addCustomCommandsPaths() + public function testAddCustomCommandsPaths() { - $tg = &$this->telegram; + $tg = $this->telegram; $this->assertAttributeCount(1, 'commands_paths', $tg); @@ -140,20 +126,14 @@ 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); 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.' );