Skip to content

Commit

Permalink
Test for command handler
Browse files Browse the repository at this point in the history
  • Loading branch information
punyflash committed Sep 3, 2020
1 parent fe8fba0 commit c11ab84
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 39 deletions.
10 changes: 1 addition & 9 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use WeStacks\TeleBot\Methods\SendMessageMethod;
use WeStacks\TeleBot\Methods\SendPhotoMethod;
use GuzzleHttp\Promise\PromiseInterface;
use WeStacks\TeleBot\Handlers\CommandHandler;
use WeStacks\TeleBot\Interfaces\UpdateHandler;
use WeStacks\TeleBot\Methods\GetUpdatesMethod;
use WeStacks\TeleBot\Objects\Update;
Expand Down Expand Up @@ -108,15 +109,6 @@ public function exceptions($exceptions = true)
return $this;
}

/**
* Get bot's update handlers
* @return (UpdateHandler|Closure)[]
*/
public function getHandlers()
{
return $this->properties['handlers'];
}

/**
* Add new update handler(s) to the bot instance
* @param string|Closure|array $handler - string that representing `UpdateHandler` subclass resolution or closure function. You also may give an array to add multiple handlers.
Expand Down
45 changes: 19 additions & 26 deletions src/Handlers/CommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WeStacks\TeleBot\Handlers;

use WeStacks\TeleBot\Interfaces\UpdateHandler;
use WeStacks\TeleBot\Objects\BotCommand;
use WeStacks\TeleBot\Objects\Update;

/**
Expand All @@ -11,42 +12,35 @@
*/
abstract class CommandHandler extends UpdateHandler
{
/**
* Bot command to trigger handler
* @var string|null
*/
protected static $command = null;

/**
* Command aliases
* @var string[]
*/
protected static $aliases = [];

/**
* Command description
* @var string|null
*/
protected static $description = null;

/**
* Command help message
* @var string|null
* Command descriptioin
* @var string
*/
protected static $help = null;
protected static $description = null;

/**
* Get array with command's `name`, `aliases`, `description` and `help` message
* @return array
* Get BotCommand foreach command `aliases` and `description`
* @return BotCommand[]
*/
public static function getComandData()
public static function getBotCommand()
{
return [
'name' => static::$command,
'aliases' => static::$aliases,
'description' => static::$description,
'help' => static::$help
];
$data = [];

foreach (static::$aliases as $name)
{
$data[] = new BotCommand([
'command' => $name,
'description' => static::$description
]);
}

return $data;
}

public static function trigger(Update $update)
Expand All @@ -57,10 +51,9 @@ public static function trigger(Update $update)
foreach ($entities as $entity)
{
if ($entity->type != 'bot_command') continue;
$aliases = array_merge(static::$aliases, [static::$command]);

$command = substr($message->text, $entity->offset, $entity->length);
if (in_array($command, $aliases)) return true;
if (in_array($command, static::$aliases)) return true;
}

return false;
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/TypeCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private static function castDirect($object, string $type)

if(in_array($value_type, $simple) && in_array($type, $simple))
{
settype($value, $type);
return $value;
settype($object, $type);
return $object;
}

if(class_exists($type)) return $type::create($object);
Expand Down
2 changes: 1 addition & 1 deletion src/Methods/SendMessageMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function request()
private function send()
{
$parameters = [
'chat_id' => 'string',
'chat_id' => 'integer',
'text' => 'string',
'parse_mode' => 'string',
'disable_web_page_preview' => 'boolean',
Expand Down
17 changes: 16 additions & 1 deletion tests/Feature/HandleUpdatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use WeStacks\TeleBot\Bot;
use WeStacks\TeleBot\Objects\Update;
use WeStacks\TeleBot\Tests\Helpers\StartCommandHandler;

class HandleUpdatesTest extends TestCase
{
Expand All @@ -20,20 +21,34 @@ protected function setUp(): void

public function testHandleUpdates()
{
// Using array just to test is it works
$this->bot->addHandler([function (Update $update) {
// This should echo update's JSON object
echo $update;
}]);

$updates = $this->bot->getUpdates();

foreach ($updates as $update)
{
// We will store out handler JSON output into the buffer and then validate is it the same update
// We will store our handler JSON output into the output buffer and then validate is it the same update
ob_start();
$this->bot->handleUpdate($update);
$result = new Update(json_decode(ob_get_clean()));

$this->assertEquals($update->update_id, $result->update_id);
}
}

public function testHandleUpdatesUsingObject()
{
$this->expectNotToPerformAssertions(); // Check telegram output. You should get "Hello, World!" from StartCommandHandler::class
$this->bot->addHandler(StartCommandHandler::class);

$updates = $this->bot->getUpdates([]);
foreach ($updates as $update)
{
$this->bot->handleUpdate($update);
}
}
}
1 change: 1 addition & 0 deletions tests/Feature/SendMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testSendMessageAsync()
'text' => 'Unit test message'
]);

// Should be wrong cos Telegram is not accept empty messages.
$promises[] = $this->bot->async(true)->exceptions(false)->sendMessage([
'chat_id' => getenv('TELEGRAM_USER_ID'),
'text' => ''
Expand Down
19 changes: 19 additions & 0 deletions tests/Helpers/StartCommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace WeStacks\TeleBot\Tests\Helpers;

use WeStacks\TeleBot\Handlers\CommandHandler;

class StartCommandHandler extends CommandHandler
{
protected static $aliases = [ '/start', '/s' ];
protected static $description = 'Send "/start" or "/s" to get "Hello, World!"';

public function handle()
{
$this->bot->sendMessage([
'chat_id' => $this->update->message->chat->id,
'text' => 'Hello, World!'
]);
}
}

0 comments on commit c11ab84

Please sign in to comment.