Skip to content

Commit

Permalink
Fix handler validation
Browse files Browse the repository at this point in the history
  • Loading branch information
punyflash committed Sep 3, 2020
1 parent c11ab84 commit 761a397
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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 @@ -125,7 +124,7 @@ public function addHandler($handler)
return;
}

if (!@class_exists($handler) && !is_callable($handler))
if ((!@class_exists($handler) || !is_subclass_of($handler, UpdateHandler::class)) && !is_callable($handler))
throw TeleBotMehtodException::wrongHandlerType(is_string($handler) ? $handler : gettype($handler));

$this->properties['handlers'][] = $handler;
Expand Down
2 changes: 0 additions & 2 deletions src/Methods/GetUpdatesMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use WeStacks\TeleBot\Helpers\TypeCaster;
use WeStacks\TeleBot\Interfaces\TelegramMethod;
use WeStacks\TeleBot\Objects\Keyboard;
use WeStacks\TeleBot\Objects\Message;
use WeStacks\TeleBot\Objects\Update;

class GetUpdatesMethod extends TelegramMethod
Expand Down
6 changes: 5 additions & 1 deletion tests/Feature/HandleUpdatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use WeStacks\TeleBot\Bot;
use WeStacks\TeleBot\Exception\TeleBotMehtodException;
use WeStacks\TeleBot\Objects\Update;
use WeStacks\TeleBot\Tests\Helpers\StartCommandHandler;

Expand Down Expand Up @@ -42,13 +43,16 @@ public function testHandleUpdates()

public function testHandleUpdatesUsingObject()
{
$this->expectNotToPerformAssertions(); // Check telegram output. You should get "Hello, World!" from StartCommandHandler::class
// 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);
}

$this->expectException(TeleBotMehtodException::class);
$this->bot->addHandler(Update::class);
}
}

0 comments on commit 761a397

Please sign in to comment.