Added editBotInfo & editChat & getVideoAttachmentDetails

This commit is contained in:
Alex
2025-08-01 08:42:39 +03:00
parent 8fa63fa10b
commit f5acd0ee88
15 changed files with 627 additions and 66 deletions
+113
View File
@@ -36,11 +36,13 @@ use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\ShareAttachmentRequ
use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\StickerAttachmentRequest;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\VideoAttachmentRequest;
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
use BushlanovDev\MaxMessengerBot\Models\BotPatch;
use BushlanovDev\MaxMessengerBot\Models\Chat;
use BushlanovDev\MaxMessengerBot\Models\ChatAdmin;
use BushlanovDev\MaxMessengerBot\Models\ChatList;
use BushlanovDev\MaxMessengerBot\Models\ChatMember;
use BushlanovDev\MaxMessengerBot\Models\ChatMembersList;
use BushlanovDev\MaxMessengerBot\Models\ChatPatch;
use BushlanovDev\MaxMessengerBot\Models\Message;
use BushlanovDev\MaxMessengerBot\Models\MessageBody;
use BushlanovDev\MaxMessengerBot\Models\Recipient;
@@ -53,6 +55,8 @@ use BushlanovDev\MaxMessengerBot\Models\Updates\BotStartedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageCreatedUpdate;
use BushlanovDev\MaxMessengerBot\Models\UploadEndpoint;
use BushlanovDev\MaxMessengerBot\Models\User;
use BushlanovDev\MaxMessengerBot\Models\VideoAttachmentDetails;
use BushlanovDev\MaxMessengerBot\Models\VideoUrls;
use BushlanovDev\MaxMessengerBot\WebhookHandler;
use GuzzleHttp\Psr7\ServerRequest;
use InvalidArgumentException;
@@ -110,6 +114,10 @@ use RuntimeException;
#[UsesClass(ArrayOf::class)]
#[UsesClass(ChatMembersList::class)]
#[UsesClass(ChatAdmin::class)]
#[UsesClass(BotPatch::class)]
#[UsesClass(ChatPatch::class)]
#[UsesClass(VideoAttachmentDetails::class)]
#[UsesClass(VideoUrls::class)]
final class ApiTest extends TestCase
{
use PHPMock;
@@ -2099,4 +2107,109 @@ final class ApiTest extends TestCase
$result = $this->api->editMessage($messageId, attachments: []);
$this->assertSame($expectedResult, $result);
}
#[Test]
public function editBotInfoSendsCorrectPatchBody(): void
{
$patch = new BotPatch(name: 'New Bot Name', description: null);
$expectedBody = [
'name' => 'New Bot Name',
'description' => null,
];
$rawResponseData = [
'user_id' => 123,
'first_name' => 'New Bot Name',
'is_bot' => true,
'last_activity_time' => 1,
'description' => null,
];
$expectedBotInfo = new BotInfo(123, 'New Bot Name', null, null, true, 1, null, null, null, null);
$this->clientMock
->expects($this->once())
->method('request')
->with('PATCH', '/me', [], $expectedBody)
->willReturn($rawResponseData);
$this->modelFactoryMock
->expects($this->once())
->method('createBotInfo')
->with($rawResponseData)
->willReturn($expectedBotInfo);
$this->api->editBotInfo($patch);
}
#[Test]
public function editChatSendsCorrectPatchBody(): void
{
$chatId = 12345;
$uri = sprintf('/chats/%d', $chatId);
$patch = new ChatPatch(title: 'New Chat Title', notify: false);
$expectedBody = [
'title' => 'New Chat Title',
'notify' => false,
];
$rawResponse = [
'chat_id' => $chatId,
'title' => 'New Chat Title',
'type' => 'chat',
'status' => 'active',
'last_event_time' => 1,
'participants_count' => 1,
'is_public' => false,
];
$expectedChat = Chat::fromArray($rawResponse);
$this->clientMock
->expects($this->once())
->method('request')
->with('PATCH', $uri, [], $expectedBody)
->willReturn($rawResponse);
$this->modelFactoryMock
->expects($this->once())
->method('createChat')
->with($rawResponse)
->willReturn($expectedChat);
$result = $this->api->editChat($chatId, $patch);
$this->assertSame($expectedChat, $result);
}
#[Test]
public function getVideoAttachmentDetailsCallsClientCorrectly(): void
{
$videoToken = 'some_video_token_xyz';
$uri = sprintf('/videos/%s', $videoToken);
$rawResponse = [
'token' => $videoToken,
'width' => 1920,
'height' => 1080,
'duration' => 120,
'urls' => ['mp4_1080' => 'http://example.com/video.mp4'],
];
$expectedDetails = VideoAttachmentDetails::fromArray($rawResponse);
$this->clientMock
->expects($this->once())
->method('request')
->with('GET', $uri)
->willReturn($rawResponse);
$this->modelFactoryMock
->expects($this->once())
->method('createVideoAttachmentDetails')
->with($rawResponse)
->willReturn($expectedDetails);
$result = $this->api->getVideoAttachmentDetails($videoToken);
$this->assertSame($expectedDetails, $result);
}
}
+26
View File
@@ -8,6 +8,7 @@ use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
use BushlanovDev\MaxMessengerBot\Enums\ChatAdminPermission;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
use BushlanovDev\MaxMessengerBot\ModelFactory;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentRequestPayload;
use BushlanovDev\MaxMessengerBot\Models\BotCommand;
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
use BushlanovDev\MaxMessengerBot\Models\Chat;
@@ -28,6 +29,8 @@ use BushlanovDev\MaxMessengerBot\Models\Updates\MessageChatCreatedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\MessageCreatedUpdate;
use BushlanovDev\MaxMessengerBot\Models\UploadEndpoint;
use BushlanovDev\MaxMessengerBot\Models\User;
use BushlanovDev\MaxMessengerBot\Models\VideoAttachmentDetails;
use BushlanovDev\MaxMessengerBot\Models\VideoUrls;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
@@ -55,6 +58,9 @@ use PHPUnit\Framework\TestCase;
#[UsesClass(ChatList::class)]
#[UsesClass(ChatMember::class)]
#[UsesClass(ChatMembersList::class)]
#[UsesClass(VideoAttachmentDetails::class)]
#[UsesClass(PhotoAttachmentRequestPayload::class)]
#[UsesClass(VideoUrls::class)]
final class ModelFactoryTest extends TestCase
{
private ModelFactory $factory;
@@ -480,4 +486,24 @@ final class ModelFactoryTest extends TestCase
$this->assertEmpty($list->members);
$this->assertNull($list->marker);
}
#[Test]
public function createVideoAttachmentDetailsSuccessfully(): void
{
$rawData = [
'token' => 'vid_token',
'width' => 1280,
'height' => 720,
'duration' => 60,
'urls' => ['mp4_720' => 'http://a.com/720.mp4'],
'thumbnail' => ['token' => 'thumb_token'],
];
$details = $this->factory->createVideoAttachmentDetails($rawData);
$this->assertInstanceOf(VideoAttachmentDetails::class, $details);
$this->assertSame('vid_token', $details->token);
$this->assertInstanceOf(VideoUrls::class, $details->urls);
$this->assertInstanceOf(PhotoAttachmentRequestPayload::class, $details->thumbnail);
}
}
+61
View File
@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentRequestPayload;
use BushlanovDev\MaxMessengerBot\Models\BotPatch;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(BotPatch::class)]
#[UsesClass(PhotoAttachmentRequestPayload::class)]
final class BotPatchTest extends TestCase
{
#[Test]
public function toArrayIncludesOnlyExplicitlySetFields(): void
{
$patch = new BotPatch(name: 'New Name');
$this->assertEquals(['name' => 'New Name'], $patch->toArray());
}
#[Test]
public function toArrayIncludesFieldsSetToNull(): void
{
$patch = new BotPatch(description: null);
$this->assertEquals(['description' => null], $patch->toArray());
}
#[Test]
public function toArrayHandlesMultipleSetFields(): void
{
$photoPayload = new PhotoAttachmentRequestPayload(token: 'photo123');
$patch = new BotPatch(
name: 'Updated Bot',
description: null,
photo: $photoPayload
);
$expected = [
'name' => 'Updated Bot',
'description' => null,
'photo' => [
'url' => null,
'token' => 'photo123',
'photos' => null,
],
];
$this->assertEquals($expected, $patch->toArray());
}
#[Test]
public function toArrayIsEmptyWhenNoArgumentsPassed(): void
{
$patch = new BotPatch();
$this->assertEmpty($patch->toArray());
}
}
+54
View File
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentRequestPayload;
use BushlanovDev\MaxMessengerBot\Models\ChatPatch;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(ChatPatch::class)]
#[UsesClass(PhotoAttachmentRequestPayload::class)]
final class ChatPatchTest extends TestCase
{
#[Test]
public function toArrayIncludesOnlySetFields(): void
{
$patch = new ChatPatch(title: 'New Title');
$this->assertEquals(['title' => 'New Title'], $patch->toArray());
}
#[Test]
public function toArrayHandlesMultipleFields(): void
{
$photoPayload = new PhotoAttachmentRequestPayload(token: 'icon_token');
$patch = new ChatPatch(
title: 'Updated Chat',
pin: 'mid.12345',
icon: $photoPayload
);
$expected = [
'title' => 'Updated Chat',
'pin' => 'mid.12345',
'icon' => [
'url' => null,
'token' => 'icon_token',
'photos' => null,
],
];
$this->assertEquals($expected, $patch->toArray());
}
#[Test]
public function toArrayIsEmptyForEmptyPatch(): void
{
$patch = new ChatPatch();
$this->assertEmpty($patch->toArray());
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentRequestPayload;
use BushlanovDev\MaxMessengerBot\Models\VideoAttachmentDetails;
use BushlanovDev\MaxMessengerBot\Models\VideoUrls;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(VideoAttachmentDetails::class)]
#[UsesClass(VideoUrls::class)]
#[UsesClass(PhotoAttachmentRequestPayload::class)]
final class VideoAttachmentDetailsTest extends TestCase
{
#[Test]
public function canBeCreatedWithAllData(): void
{
$data = [
'token' => 'video_token_123',
'width' => 1920,
'height' => 1080,
'duration' => 125,
'urls' => ['mp4_1080' => 'http://example.com/video.mp4'],
'thumbnail' => ['token' => 'thumb_token_456'],
];
$details = VideoAttachmentDetails::fromArray($data);
$this->assertInstanceOf(VideoAttachmentDetails::class, $details);
$this->assertSame('video_token_123', $details->token);
$this->assertSame(125, $details->duration);
$this->assertInstanceOf(VideoUrls::class, $details->urls);
$this->assertInstanceOf(PhotoAttachmentRequestPayload::class, $details->thumbnail);
$this->assertSame('http://example.com/video.mp4', $details->urls->mp4_1080);
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models;
use BushlanovDev\MaxMessengerBot\Models\VideoUrls;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[CoversClass(VideoUrls::class)]
final class VideoUrlsTest extends TestCase
{
#[Test]
public function canBeCreatedWithAllFields(): void
{
$data = [
'mp4_720' => 'http://example.com/video_720p.mp4',
'mp4_480' => 'http://example.com/video_480p.mp4',
];
$urls = VideoUrls::fromArray($data);
$this->assertInstanceOf(VideoUrls::class, $urls);
$this->assertSame('http://example.com/video_720p.mp4', $urls->mp4_720);
$this->assertNull($urls->mp4_1080);
}
}