Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.6/phpunit.xsd"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
bootstrap="./tests/Bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
Expand All @@ -23,6 +23,10 @@
<php>
<ini name="error_reporting" value="-1" />
<const name="PHPUNIT_TESTSUITE" value="true"/>
<const name="PHPUNIT_DB_HOST" value="127.0.0.1"/>
<const name="PHPUNIT_DB_NAME" value="telegrambot"/>
<const name="PHPUNIT_DB_USER" value="root"/>
<const name="PHPUNIT_DB_PASS" value=""/>
</php>
<testsuites>
<testsuite name="Package Test Suite">
Expand Down
2 changes: 1 addition & 1 deletion structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand Down
105 changes: 57 additions & 48 deletions tests/TestHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,39 +84,39 @@ 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');
}

/**
* Return a fake command object for the passed command text
*
* @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,
],
];
Expand All @@ -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());

Expand All @@ -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();
}
}
60 changes: 19 additions & 41 deletions tests/Unit/Commands/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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';
Expand All @@ -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;
Expand All @@ -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);
Expand Down
14 changes: 11 additions & 3 deletions tests/Unit/Commands/CommandTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Loading