Parse comment and bot admin permission updates

UpdateType already had comment_created, comment_edited,
comment_removed and bot_admin_permissions_changed, but ModelFactory
had no models for them and threw "Unknown or unsupported update type".
Adds the models from schema 0.0.33, maps them in createUpdate() and
adds on*() shortcuts to UpdateDispatcher and MaxBotManager.
This commit is contained in:
Timofey
2026-09-28 00:13:13 +05:00
parent 749c0aab5b
commit d1637add72
12 changed files with 454 additions and 0 deletions
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
use BushlanovDev\MaxMessengerBot\Enums\ChatAdminPermission;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\Updates\BotAdminPermissionsChangedUpdate;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(BotAdminPermissionsChangedUpdate::class)]
#[UsesClass(ArrayOf::class)]
final class BotAdminPermissionsChangedUpdateTest extends TestCase
{
#[Test]
public function canBeCreatedFromArray(): void
{
$update = BotAdminPermissionsChangedUpdate::fromArray([
'update_type' => UpdateType::BotAdminPermissionChanged->value,
'timestamp' => 1678886400000,
'chat_id' => -100,
'user_id' => 42,
'bot_id' => 7,
'is_channel' => true,
'is_admin' => true,
'permissions' => ['read_all_messages', 'pin_message'],
]);
$this->assertSame(UpdateType::BotAdminPermissionChanged, $update->updateType);
$this->assertSame(-100, $update->chatId);
$this->assertSame(42, $update->userId);
$this->assertSame(7, $update->botId);
$this->assertTrue($update->isChannel);
$this->assertTrue($update->isAdmin);
$this->assertSame([ChatAdminPermission::ReadAllMessages, ChatAdminPermission::PinMessage], $update->permissions);
}
#[Test]
public function permissionsAreNullWhenTheBotIsNoLongerAdmin(): void
{
$update = BotAdminPermissionsChangedUpdate::fromArray([
'update_type' => UpdateType::BotAdminPermissionChanged->value,
'timestamp' => 1678886400000,
'chat_id' => -100,
'user_id' => 42,
'bot_id' => 7,
'is_channel' => false,
'is_admin' => false,
]);
$this->assertFalse($update->isAdmin);
$this->assertNull($update->permissions);
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
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\CommentCreatedUpdate;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(CommentCreatedUpdate::class)]
#[UsesClass(Message::class)]
#[UsesClass(MessageBody::class)]
#[UsesClass(Recipient::class)]
final class CommentCreatedUpdateTest extends TestCase
{
#[Test]
public function canBeCreatedFromArray(): void
{
$update = CommentCreatedUpdate::fromArray([
'update_type' => UpdateType::CommentCreated->value,
'timestamp' => 1678886400000,
'message' => [
'timestamp' => 1678886400000,
'body' => ['mid' => 'mid.comment', 'seq' => 5, 'text' => 'Nice post'],
'recipient' => ['chat_type' => 'channel', 'chat_id' => -100],
],
]);
$this->assertSame(UpdateType::CommentCreated, $update->updateType);
$this->assertSame(1678886400000, $update->timestamp);
$this->assertSame('mid.comment', $update->message->body?->mid);
$this->assertSame('Nice post', $update->message->body?->text);
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
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\CommentEditedUpdate;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(CommentEditedUpdate::class)]
#[UsesClass(Message::class)]
#[UsesClass(MessageBody::class)]
#[UsesClass(Recipient::class)]
final class CommentEditedUpdateTest extends TestCase
{
#[Test]
public function canBeCreatedFromArray(): void
{
$update = CommentEditedUpdate::fromArray([
'update_type' => UpdateType::CommentEdited->value,
'timestamp' => 1678886400000,
'message' => [
'timestamp' => 1678886400000,
'body' => ['mid' => 'mid.comment', 'seq' => 5, 'text' => 'Nice post'],
'recipient' => ['chat_type' => 'channel', 'chat_id' => -100],
],
]);
$this->assertSame(UpdateType::CommentEdited, $update->updateType);
$this->assertSame(1678886400000, $update->timestamp);
$this->assertSame('mid.comment', $update->message->body?->mid);
$this->assertSame('Nice post', $update->message->body?->text);
}
}
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Updates;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\Models\Updates\CommentRemovedUpdate;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[CoversClass(CommentRemovedUpdate::class)]
final class CommentRemovedUpdateTest extends TestCase
{
#[Test]
public function canBeCreatedFromArray(): void
{
$update = CommentRemovedUpdate::fromArray([
'update_type' => UpdateType::CommentRemoved->value,
'timestamp' => 1678886400000,
'message_id' => 'mid.comment',
'chat_id' => -100,
'user_id' => 42,
'post_id' => 'mid.post',
]);
$this->assertSame(UpdateType::CommentRemoved, $update->updateType);
$this->assertSame('mid.comment', $update->messageId);
$this->assertSame(-100, $update->chatId);
$this->assertSame(42, $update->userId);
$this->assertSame('mid.post', $update->postId);
}
}