Update tests and Throw InvalidArgumentException when building an empty comment body

This commit is contained in:
Alex
2026-10-01 11:26:19 +03:00
parent 093fafaff4
commit 21f6cb9e83
4 changed files with 223 additions and 4 deletions
+111
View File
@@ -5,12 +5,17 @@ declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests;
use BushlanovDev\MaxMessengerBot\Api;
use BushlanovDev\MaxMessengerBot\Enums\ChatAdminPermission;
use BushlanovDev\MaxMessengerBot\Enums\ChatType;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\Message;
use BushlanovDev\MaxMessengerBot\Models\MessageBody;
use BushlanovDev\MaxMessengerBot\Models\Recipient;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotAdminPermissionsChangedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotStartedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\CommentCreatedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\CommentEditedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\CommentRemovedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageCreatedUpdate;
use BushlanovDev\MaxMessengerBot\Models\UserWithPhoto;
use BushlanovDev\MaxMessengerBot\UpdateDispatcher;
@@ -28,6 +33,10 @@ use PHPUnit\Framework\TestCase;
#[UsesClass(MessageBody::class)]
#[UsesClass(Recipient::class)]
#[UsesClass(MessageCreatedUpdate::class)]
#[UsesClass(CommentCreatedUpdate::class)]
#[UsesClass(CommentEditedUpdate::class)]
#[UsesClass(CommentRemovedUpdate::class)]
#[UsesClass(BotAdminPermissionsChangedUpdate::class)]
final class UpdateDispatcherTest extends TestCase
{
private Api $apiMock;
@@ -117,4 +126,106 @@ final class UpdateDispatcherTest extends TestCase
$this->assertFalse($commandCalled, 'onCommand handler should not be called for a regular message.');
$this->assertTrue($messageHandlerCalled, 'onMessageCreated handler was not called.');
}
#[Test]
public function onCommentCreatedDispatchesCommentCreatedUpdate(): void
{
$wasCalled = false;
$message = new Message(
time(),
new Recipient(ChatType::Channel, null, -100),
new MessageBody('mid.c1', 1, 'A comment', null, null),
new UserWithPhoto(50, 'Anna', null, null, false, time(), null, null, null),
null,
null,
null,
);
$update = new CommentCreatedUpdate(time(), $message);
$this->dispatcher->onCommentCreated(
function ($receivedUpdate, $receivedApi) use (&$wasCalled, $update) {
$this->assertSame($update, $receivedUpdate);
$this->assertSame($this->apiMock, $receivedApi);
$wasCalled = true;
},
);
$this->dispatcher->dispatch($update);
$this->assertTrue($wasCalled, 'onCommentCreated handler was not called.');
}
#[Test]
public function onCommentEditedDispatchesCommentEditedUpdate(): void
{
$wasCalled = false;
$message = new Message(
time(),
new Recipient(ChatType::Channel, null, -100),
new MessageBody('mid.c1', 2, 'Edited comment', null, null),
new UserWithPhoto(50, 'Anna', null, null, false, time(), null, null, null),
null,
null,
null,
);
$update = new CommentEditedUpdate(time(), $message);
$this->dispatcher->onCommentEdited(
function ($receivedUpdate, $receivedApi) use (&$wasCalled, $update) {
$this->assertSame($update, $receivedUpdate);
$this->assertSame($this->apiMock, $receivedApi);
$wasCalled = true;
},
);
$this->dispatcher->dispatch($update);
$this->assertTrue($wasCalled, 'onCommentEdited handler was not called.');
}
#[Test]
public function onCommentRemovedDispatchesCommentRemovedUpdate(): void
{
$wasCalled = false;
$update = new CommentRemovedUpdate(time(), 'mid.c1', -100, 50, 'mid.post');
$this->dispatcher->onCommentRemoved(
function ($receivedUpdate, $receivedApi) use (&$wasCalled, $update) {
$this->assertSame($update, $receivedUpdate);
$this->assertSame($this->apiMock, $receivedApi);
$wasCalled = true;
},
);
$this->dispatcher->dispatch($update);
$this->assertTrue($wasCalled, 'onCommentRemoved handler was not called.');
}
#[Test]
public function onBotAdminPermissionsChangedDispatchesPermissionsUpdate(): void
{
$wasCalled = false;
$update = new BotAdminPermissionsChangedUpdate(
time(),
-100,
50,
999,
true,
true,
[ChatAdminPermission::ReadAllMessages, ChatAdminPermission::Write],
);
$this->dispatcher->onBotAdminPermissionsChanged(
function ($receivedUpdate, $receivedApi) use (&$wasCalled, $update) {
$this->assertSame($update, $receivedUpdate);
$this->assertSame($this->apiMock, $receivedApi);
$wasCalled = true;
},
);
$this->dispatcher->dispatch($update);
$this->assertTrue($wasCalled, 'onBotAdminPermissionsChanged handler was not called.');
}
}