From 761a39767bb27727765361cc1699123d15ededc1 Mon Sep 17 00:00:00 2001 From: PunyFlash Date: Thu, 3 Sep 2020 16:51:49 +0300 Subject: [PATCH] Fix handler validation --- src/Bot.php | 3 +-- src/Methods/GetUpdatesMethod.php | 2 -- tests/Feature/HandleUpdatesTest.php | 6 +++++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Bot.php b/src/Bot.php index 0ed40a3..0655f99 100644 --- a/src/Bot.php +++ b/src/Bot.php @@ -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; @@ -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; diff --git a/src/Methods/GetUpdatesMethod.php b/src/Methods/GetUpdatesMethod.php index ea98efc..98b9fd9 100644 --- a/src/Methods/GetUpdatesMethod.php +++ b/src/Methods/GetUpdatesMethod.php @@ -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 diff --git a/tests/Feature/HandleUpdatesTest.php b/tests/Feature/HandleUpdatesTest.php index 439ace5..305bc77 100644 --- a/tests/Feature/HandleUpdatesTest.php +++ b/tests/Feature/HandleUpdatesTest.php @@ -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; @@ -42,7 +43,7 @@ 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([]); @@ -50,5 +51,8 @@ public function testHandleUpdatesUsingObject() { $this->bot->handleUpdate($update); } + + $this->expectException(TeleBotMehtodException::class); + $this->bot->addHandler(Update::class); } } \ No newline at end of file