mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-10-11 06:19:18 +00:00
Added getChats & getChatByLink & deleteChat & sendAction
This commit is contained in:
@@ -10,6 +10,7 @@ use BushlanovDev\MaxMessengerBot\ClientApiInterface;
|
||||
use BushlanovDev\MaxMessengerBot\Enums\AttachmentType;
|
||||
use BushlanovDev\MaxMessengerBot\Enums\ButtonType;
|
||||
use BushlanovDev\MaxMessengerBot\Enums\MessageFormat;
|
||||
use BushlanovDev\MaxMessengerBot\Enums\SenderAction;
|
||||
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
|
||||
use BushlanovDev\MaxMessengerBot\Enums\UploadType;
|
||||
use BushlanovDev\MaxMessengerBot\Exceptions\SerializationException;
|
||||
@@ -34,6 +35,7 @@ use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\StickerAttachmentRe
|
||||
use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\VideoAttachmentRequest;
|
||||
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Chat;
|
||||
use BushlanovDev\MaxMessengerBot\Models\ChatList;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Message;
|
||||
use BushlanovDev\MaxMessengerBot\Models\MessageBody;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Recipient;
|
||||
@@ -98,6 +100,7 @@ use RuntimeException;
|
||||
#[UsesClass(LocationAttachmentRequestPayload::class)]
|
||||
#[UsesClass(ShareAttachmentRequest::class)]
|
||||
#[UsesClass(ShareAttachmentRequestPayload::class)]
|
||||
#[UsesClass(ChatList::class)]
|
||||
final class ApiTest extends TestCase
|
||||
{
|
||||
use PHPMock;
|
||||
@@ -1197,4 +1200,158 @@ final class ApiTest extends TestCase
|
||||
|
||||
$this->assertSame($expectedMessageObject, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getChatsPassesAllParametersToClient(): void
|
||||
{
|
||||
$count = 30;
|
||||
$marker = 12345;
|
||||
$expectedQuery = ['count' => $count, 'marker' => $marker];
|
||||
|
||||
$rawResponse = ['chats' => [], 'marker' => 54321];
|
||||
$expectedChatList = new ChatList([], 54321);
|
||||
|
||||
$this->clientMock->expects($this->once())
|
||||
->method('request')
|
||||
->with('GET', '/chats', $expectedQuery)
|
||||
->willReturn($rawResponse);
|
||||
|
||||
$this->modelFactoryMock->expects($this->once())
|
||||
->method('createChatList')
|
||||
->with($rawResponse)
|
||||
->willReturn($expectedChatList);
|
||||
|
||||
$result = $this->api->getChats($count, $marker);
|
||||
|
||||
$this->assertSame($expectedChatList, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getChatsHandlesNullParametersCorrectly(): void
|
||||
{
|
||||
$expectedQuery = [];
|
||||
$rawResponse = ['chats' => [], 'marker' => null];
|
||||
$expectedChatList = new ChatList([], null);
|
||||
|
||||
$this->clientMock->expects($this->once())
|
||||
->method('request')
|
||||
->with('GET', '/chats', $expectedQuery)
|
||||
->willReturn($rawResponse);
|
||||
|
||||
$this->modelFactoryMock->expects($this->once())
|
||||
->method('createChatList')
|
||||
->with($rawResponse)
|
||||
->willReturn($expectedChatList);
|
||||
|
||||
$result = $this->api->getChats(null, null);
|
||||
|
||||
$this->assertSame($expectedChatList, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getChatByLinkCallsClientAndFactoryCorrectly(): void
|
||||
{
|
||||
$chatLink = '@test_channel';
|
||||
$rawResponseData = [
|
||||
'chat_id' => 987,
|
||||
'type' => 'channel',
|
||||
'status' => 'active',
|
||||
'last_event_time' => 1,
|
||||
'participants_count' => 100,
|
||||
'is_public' => true,
|
||||
'title' => 'Test Channel',
|
||||
];
|
||||
|
||||
$expectedChatObject = Chat::fromArray($rawResponseData);
|
||||
|
||||
$this->clientMock
|
||||
->expects($this->once())
|
||||
->method('request')
|
||||
->with('GET', '/chats/' . $chatLink)
|
||||
->willReturn($rawResponseData);
|
||||
|
||||
$this->modelFactoryMock
|
||||
->expects($this->once())
|
||||
->method('createChat')
|
||||
->with($rawResponseData)
|
||||
->willReturn($expectedChatObject);
|
||||
|
||||
$result = $this->api->getChatByLink($chatLink);
|
||||
|
||||
$this->assertSame($expectedChatObject, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getChatByLinkHandlesLinkWithoutAtSymbol(): void
|
||||
{
|
||||
$chatLink = 'test_channel_no_at';
|
||||
$rawResponseData = [
|
||||
'chat_id' => 987,
|
||||
'type' => 'channel',
|
||||
'status' => 'active',
|
||||
'last_event_time' => 1,
|
||||
'participants_count' => 100,
|
||||
'is_public' => true,
|
||||
'title' => 'Test Channel',
|
||||
];
|
||||
$expectedChatObject = Chat::fromArray($rawResponseData);
|
||||
|
||||
$this->clientMock->method('request')->willReturn($rawResponseData);
|
||||
$this->modelFactoryMock->method('createChat')->willReturn($expectedChatObject);
|
||||
|
||||
$this->api->getChatByLink($chatLink);
|
||||
|
||||
$this->expectNotToPerformAssertions();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function deleteChatCallsClientAndFactoryCorrectly(): void
|
||||
{
|
||||
$chatId = 123456789;
|
||||
$rawResponseData = ['success' => true, 'message' => null];
|
||||
$expectedResultObject = new Result(true, null);
|
||||
|
||||
$this->clientMock
|
||||
->expects($this->once())
|
||||
->method('request')
|
||||
->with(self::equalTo('DELETE'), self::equalTo('/chats/' . $chatId))
|
||||
->willReturn($rawResponseData);
|
||||
|
||||
$this->modelFactoryMock
|
||||
->expects($this->once())
|
||||
->method('createResult')
|
||||
->with($rawResponseData)
|
||||
->willReturn($expectedResultObject);
|
||||
|
||||
$result = $this->api->deleteChat($chatId);
|
||||
|
||||
$this->assertSame($expectedResultObject, $result);
|
||||
$this->assertTrue($result->success);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function sendActionCallsClientCorrectly(): void
|
||||
{
|
||||
$chatId = 12345;
|
||||
$action = SenderAction::TypingOn;
|
||||
$uri = '/chats/' . $chatId . '/actions';
|
||||
$expectedBody = ['action' => 'typing_on'];
|
||||
$rawResponse = ['success' => true];
|
||||
$expectedResult = new Result(true, null);
|
||||
|
||||
$this->clientMock
|
||||
->expects($this->once())
|
||||
->method('request')
|
||||
->with(self::equalTo('POST'), self::equalTo($uri), self::equalTo([]), self::equalTo($expectedBody))
|
||||
->willReturn($rawResponse);
|
||||
|
||||
$this->modelFactoryMock
|
||||
->expects($this->once())
|
||||
->method('createResult')
|
||||
->with($rawResponse)
|
||||
->willReturn($expectedResult);
|
||||
|
||||
$result = $this->api->sendAction($chatId, $action);
|
||||
$this->assertSame($expectedResult, $result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ use BushlanovDev\MaxMessengerBot\ModelFactory;
|
||||
use BushlanovDev\MaxMessengerBot\Models\BotCommand;
|
||||
use BushlanovDev\MaxMessengerBot\Models\BotInfo;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Chat;
|
||||
use BushlanovDev\MaxMessengerBot\Models\ChatList;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Image;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Message;
|
||||
use BushlanovDev\MaxMessengerBot\Models\MessageBody;
|
||||
@@ -48,6 +49,7 @@ use PHPUnit\Framework\TestCase;
|
||||
#[UsesClass(Image::class)]
|
||||
#[UsesClass(ChatTitleChangedUpdate::class)]
|
||||
#[UsesClass(MessageChatCreatedUpdate::class)]
|
||||
#[UsesClass(ChatList::class)]
|
||||
final class ModelFactoryTest extends TestCase
|
||||
{
|
||||
private ModelFactory $factory;
|
||||
@@ -314,4 +316,39 @@ final class ModelFactoryTest extends TestCase
|
||||
$this->assertInstanceOf(ChatTitleChangedUpdate::class, $updateList->updates[2]);
|
||||
$this->assertInstanceOf(MessageChatCreatedUpdate::class, $updateList->updates[3]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function createChatListDelegatesCreationToChatListModel(): void
|
||||
{
|
||||
$rawData = [
|
||||
'chats' => [
|
||||
[
|
||||
'chat_id' => 101,
|
||||
'type' => 'chat',
|
||||
'status' => 'active',
|
||||
'last_event_time' => 1,
|
||||
'participants_count' => 5,
|
||||
'is_public' => false,
|
||||
],
|
||||
[
|
||||
'chat_id' => 102,
|
||||
'type' => 'dialog',
|
||||
'status' => 'suspended',
|
||||
'last_event_time' => 2,
|
||||
'participants_count' => 2,
|
||||
'is_public' => false,
|
||||
],
|
||||
],
|
||||
'marker' => 98765,
|
||||
];
|
||||
|
||||
$chatList = $this->factory->createChatList($rawData);
|
||||
|
||||
$this->assertInstanceOf(ChatList::class, $chatList);
|
||||
|
||||
$this->assertSame(98765, $chatList->marker);
|
||||
$this->assertCount(2, $chatList->chats);
|
||||
$this->assertInstanceOf(Chat::class, $chatList->chats[0]);
|
||||
$this->assertSame(101, $chatList->chats[0]->chatId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BushlanovDev\MaxMessengerBot\Tests\Models;
|
||||
|
||||
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
|
||||
use BushlanovDev\MaxMessengerBot\Models\Chat;
|
||||
use BushlanovDev\MaxMessengerBot\Models\ChatList;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\Attributes\UsesClass;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[CoversClass(ChatList::class)]
|
||||
#[UsesClass(Chat::class)]
|
||||
#[UsesClass(ArrayOf::class)]
|
||||
final class ChatListTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function canBeCreatedFromArray(): void
|
||||
{
|
||||
$rawData = [
|
||||
'chats' => [
|
||||
[
|
||||
'chat_id' => 123,
|
||||
'type' => 'chat',
|
||||
'status' => 'active',
|
||||
'last_event_time' => 1,
|
||||
'participants_count' => 10,
|
||||
'is_public' => false,
|
||||
],
|
||||
[
|
||||
'chat_id' => 456,
|
||||
'type' => 'dialog',
|
||||
'status' => 'active',
|
||||
'last_event_time' => 2,
|
||||
'participants_count' => 2,
|
||||
'is_public' => false,
|
||||
],
|
||||
],
|
||||
'marker' => 98765,
|
||||
];
|
||||
|
||||
$chatList = ChatList::fromArray($rawData);
|
||||
|
||||
$this->assertInstanceOf(ChatList::class, $chatList);
|
||||
$this->assertCount(2, $chatList->chats);
|
||||
$this->assertInstanceOf(Chat::class, $chatList->chats[0]);
|
||||
$this->assertSame(123, $chatList->chats[0]->chatId);
|
||||
$this->assertSame(98765, $chatList->marker);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function canBeCreatedWithEmptyChatsAndNullMarker(): void
|
||||
{
|
||||
$rawData = ['chats' => [], 'marker' => null];
|
||||
$chatList = ChatList::fromArray($rawData);
|
||||
|
||||
$this->assertEmpty($chatList->chats);
|
||||
$this->assertNull($chatList->marker);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user