Compare commits

..

3 Commits

Author SHA1 Message Date
Alex f5acd0ee88 Added editBotInfo & editChat & getVideoAttachmentDetails 2025-08-01 08:42:39 +03:00
Alex 8fa63fa10b Added addAdmins & addMembers & editMessage & answerOnCallback 2025-07-31 09:31:33 +03:00
Alex eb5f6c7bbe Test fix 2025-07-30 09:00:24 +03:00
19 changed files with 1218 additions and 123 deletions
+88 -35
View File
@@ -2,6 +2,7 @@
[![Actions status](https://github.com/BushlanovDev/max-bot-api-client-php/actions/workflows/ci.yml/badge.svg?style=flat-square)](https://github.com/BushlanovDev/max-bot-api-client-php/actions)
[![Coverage](https://raw.githubusercontent.com/BushlanovDev/max-bot-api-client-php/refs/heads/master/badge-coverage.svg?v=1)](https://github.com/BushlanovDev/max-bot-api-client-php/actions)
[![Packagist Version](https://img.shields.io/packagist/v/bushlanov-dev/max-bot-api-client-php.svg?style=flat-square)](https://packagist.org/packages/bushlanov-dev/max-bot-api-client-php)
[![PHP version](https://img.shields.io/badge/php-%3E%3D%208.3-8892BF.svg?style=flat-square)](https://github.com/BushlanovDev/max-bot-api-client-php)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
@@ -9,65 +10,117 @@
> На мой взгляд `Max Messenger` является ни чем иным как малварью, созданной для слежки за гражданами РФ. Настоятельно
> не рекомендую использовать его на реальных устройствах, с настоящим номером телефона, и для личной переписки.
> [!IMPORTANT]
> Библиотека в стадии активной разработки.
## Быстрый старт
> Если вы новичок, то можете прочитать [официальную документацию](https://dev.max.ru/), написанную разработчиками Max
> Если вы новичок, то можете прочитать [официальную документацию](https://dev.max.ru/), написанную разработчиками Max.
### Получение токена
Откройте диалог с [MasterBot](https://max.ru/MasterBot), следуйте инструкциям и создайте нового бота. После создания
бота MasterBot отправит вам токен.
### Установка библиотеки
```bash
composer require bushlanov-dev/max-bot-api-client-php
```
### Использование
Отправка сообщения с клавиатурой
```php
$api = new \BushlanovDev\MaxMessengerBot\Api('YOUR_BOT_API_TOKEN');
$api->sendMessage(
userId: 123, // ID пользователя получателя сообщения
chatId: 321, // Или ID чата, в который нужно отправить сообщение
text: 'Привет!', // Текст сообщения, вы можете использовать HTML или Markdown
attachments: [
new InlineKeyboardAttachmentRequest([
[new CallbackButton('Нажми меня!', 'payload_button1')],
[new LinkButton('Нажми меня!', 'https://example.com')],
]),
],
format: MessageFormat::Markdown, // Формат сообщения (Markdown или HTML)
);
```
Подписка на вэб хуки
```php
$api->subscribe(
new \BushlanovDev\MaxMessengerBot\Models\SubscriptionRequest(
'https://example.com/webhook', // URL на который будут приходить хуки
'super_secret', // Секретная фраза для проверки хуков
[
// Типы хуков которые вы хотите получать (либо ничего не указывать, чтобы получать все)
\BushlanovDev\MaxMessengerBot\Enums\UpdateType::MessageCreated,
],
),
);
```
Обработка хуков
```php
$webhookHandler = $api->createWebhookHandler();
$webhookHandler->addHandler(UpdateType::BotStarted, function (BotStartedUpdate $update, Api $api) {
$api->sendMessage(
chatId: $update->chatId,
text: 'Я запущен!',
);
});
```
## Реализованные методы
#### Bots
- [x] `GET /me` (`getBotInfo`) *Получение информации о боте.*
- [ ] `PATCH /me` (`editBotInfo`) *Редактирование информации о боте.*
- [x] `GET /me` (`getBotInfo`) - *Получение информации о боте.*
- [x] `PATCH /me` (`editBotInfo`) - *Редактирование информации о боте.*
#### Chats
- [x] `GET /chats` (`getChats`) *Получение списка всех чатов бота.*
- [x] `GET /chats/{chatLink}` (`getChatByLink`) *Получение информации о чате по ссылке.*
- [x] `GET /chats/{chatId}` (`getChat`) *Получение информации о чате по ID.*
- [ ] `PATCH /chats/{chatId}` (`editChat`) *Редактирование информации о чате.*
- [x] `DELETE /chats/{chatId}` (`deleteChat`) *Удаление чата.*
- [x] `POST /chats/{chatId}/actions` (`sendAction`) *Отправка действия в чат (например, "печатает...").*
- [x] `GET /chats/{chatId}/pin` (`getPinnedMessage`) *Получение закрепленного сообщения.*
- [x] `PUT /chats/{chatId}/pin` (`pinMessage`) *Закрепление сообщения.*
- [x] `DELETE /chats/{chatId}/pin` (`unpinMessage`) *Открепление сообщения.*
- [x] `GET /chats/{chatId}/members/me` (`getMembership`) *Получение информации о членстве бота в чате.*
- [x] `DELETE /chats/{chatId}/members/me` (`leaveChat`) *Выход бота из чата.*
- [x] `GET /chats/{chatId}/members/admins` (`getAdmins`) *Получение администраторов чата.*
- [ ] `POST /chats/{chatId}/members/admins` (`postAdmins`) *Назначение администраторов чата.*
- [x] `DELETE /chats/{chatId}/members/admins/{userId}` (`deleteAdmins`) *Снятие прав администратора.*
- [x] `GET /chats/{chatId}/members` (`getMembers`) *Получение участников чата.*
- [ ] `POST /chats/{chatId}/members` (`addMembers`) *Добавление участников в чат.*
- [x] `DELETE /chats/{chatId}/members` (`removeMember`) *Удаление участника из чата.*
- [x] `GET /chats` (`getChats`) - *Получение списка всех чатов бота.*
- [x] `GET /chats/{chatLink}` (`getChatByLink`) - *Получение информации о чате по ссылке.*
- [x] `GET /chats/{chatId}` (`getChat`) - *Получение информации о чате по ID.*
- [x] `PATCH /chats/{chatId}` (`editChat`) - *Редактирование информации о чате.*
- [x] `DELETE /chats/{chatId}` (`deleteChat`) - *Удаление чата.*
- [x] `POST /chats/{chatId}/actions` (`sendAction`) - *Отправка действия в чат (например, "печатает...").*
- [x] `GET /chats/{chatId}/pin` (`getPinnedMessage`) - *Получение закрепленного сообщения.*
- [x] `PUT /chats/{chatId}/pin` (`pinMessage`) - *Закрепление сообщения.*
- [x] `DELETE /chats/{chatId}/pin` (`unpinMessage`) - *Открепление сообщения.*
- [x] `GET /chats/{chatId}/members/me` (`getMembership`) - *Получение информации о членстве бота в чате.*
- [x] `DELETE /chats/{chatId}/members/me` (`leaveChat`) - *Выход бота из чата.*
- [x] `GET /chats/{chatId}/members/admins` (`getAdmins`) - *Получение администраторов чата.*
- [x] `POST /chats/{chatId}/members/admins` (`addAdmins`) - *Назначение администраторов чата.*
- [x] `DELETE /chats/{chatId}/members/admins/{userId}` (`deleteAdmins`) - *Снятие прав администратора.*
- [x] `GET /chats/{chatId}/members` (`getMembers`) - *Получение участников чата.*
- [x] `POST /chats/{chatId}/members` (`addMembers`) - *Добавление участников в чат.*
- [x] `DELETE /chats/{chatId}/members` (`deleteMember`) - *Удаление участника из чата.*
#### Subscriptions
- [x] `GET /subscriptions` (`getSubscriptions`) *Получение списка Webhook-подписок.*
- [x] `POST /subscriptions` (`subscribe`) *Создание Webhook-подписки.*
- [x] `DELETE /subscriptions` (`unsubscribe`) *Удаление Webhook-подписки.*
- [x] `GET /updates` (`getUpdates`) *Получение обновлений через Long-Polling.*
- [x] `GET /subscriptions` (`getSubscriptions`) - *Получение списка Webhook-подписок.*
- [x] `POST /subscriptions` (`subscribe`) - *Создание Webhook-подписки.*
- [x] `DELETE /subscriptions` (`unsubscribe`) - *Удаление Webhook-подписки.*
- [x] `GET /updates` (`getUpdates`) - *Получение обновлений через Long-Polling.*
#### Upload
- [x] `POST /uploads` (`getUploadUrl`) *Получение URL для загрузки файла.*
- [x] `POST /uploads` (`getUploadUrl`) - *Получение URL для загрузки файла.*
#### Messages
- [x] `GET /messages` (`getMessages`) *Получение списка сообщений из чата.*
- [x] `POST /messages` (`sendMessage`) *Отправка сообщения.*
- [ ] `PUT /messages` (`editMessage`) *Редактирование сообщения.*
- [x] `DELETE /messages` (`deleteMessage`) *Удаление сообщения.*
- [x] `GET /messages/{messageId}` (`getMessageById`) *Получение сообщения по ID.*
- [ ] `GET /videos/{videoToken}` (`getVideoAttachmentDetails`) *Получение детальной информации о видео.*
- [ ] `POST /answers` (`answerOnCallback`) *Ответ на нажатие callback-кнопки.*
- [x] `GET /messages` (`getMessages`) - *Получение списка сообщений из чата.*
- [x] `POST /messages` (`sendMessage`) - *Отправка сообщения.*
- [x] `PUT /messages` (`editMessage`) - *Редактирование сообщения.*
- [x] `DELETE /messages` (`deleteMessage`) - *Удаление сообщения.*
- [x] `GET /messages/{messageId}` (`getMessageById`) - *Получение сообщения по ID.*
- [x] `GET /videos/{videoToken}` (`getVideoAttachmentDetails`) - *Получение детальной информации о видео.*
- [x] `POST /answers` (`answerOnCallback`) - *Ответ на нажатие callback-кнопки.*
## Лицензия
+305 -16
View File
@@ -19,10 +19,13 @@ use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\FileAttachmentReque
use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\PhotoAttachmentRequest;
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\MessageLink;
use BushlanovDev\MaxMessengerBot\Models\Result;
@@ -30,6 +33,7 @@ use BushlanovDev\MaxMessengerBot\Models\Subscription;
use BushlanovDev\MaxMessengerBot\Models\UpdateList;
use BushlanovDev\MaxMessengerBot\Models\Updates\AbstractUpdate;
use BushlanovDev\MaxMessengerBot\Models\UploadEndpoint;
use BushlanovDev\MaxMessengerBot\Models\VideoAttachmentDetails;
use InvalidArgumentException;
use LogicException;
use Psr\Http\Message\ServerRequestInterface;
@@ -50,7 +54,7 @@ class Api
private const string METHOD_GET = 'GET';
private const string METHOD_POST = 'POST';
private const string METHOD_DELETE = 'DELETE';
// private const string METHOD_PATCH = 'PATCH';
private const string METHOD_PATCH = 'PATCH';
private const string METHOD_PUT = 'PUT';
private const string ACTION_ME = '/me';
@@ -65,6 +69,8 @@ class Api
private const string ACTION_CHATS_MEMBERS_ADMINS_ID = '/chats/%d/members/admins/%d';
private const string ACTION_CHATS_MEMBERS = '/chats/%d/members';
private const string ACTION_UPDATES = '/updates';
private const string ACTION_ANSWERS = '/answers';
private const string ACTION_VIDEO_DETAILS = '/videos/%s';
private readonly ClientApiInterface $client;
@@ -348,7 +354,7 @@ class Api
}
/**
* Sends a message to a chat.
* Sends a message to a chat or user.
*
* @param int|null $userId Fill this parameter if you want to send message to user.
* @param int|null $chatId Fill this if you send message to chat.
@@ -381,27 +387,76 @@ class Api
'disable_link_preview' => $disableLinkPreview,
];
$body = [
'text' => $text,
'format' => $format?->value,
'notify' => $notify,
'link' => $link,
'attachments' => $attachments !== null ? array_map(
fn(AbstractModel $attachment) => $attachment->toArray(),
$attachments,
) : null,
];
$response = $this->client->request(
self::METHOD_POST,
self::ACTION_MESSAGES,
array_filter($query, fn($item) => null !== $item),
array_filter($body, fn($item) => null !== $item),
$this->buildNewMessageBody($text, $attachments, $format, $link, $notify),
);
return $this->modelFactory->createMessage($response['message']);
}
/**
* Sends a message to a user.
*
* @param int|null $userId Fill this parameter if you want to send message to user.
* @param string|null $text Message text.
* @param AbstractAttachmentRequest[]|null $attachments Message attachments.
* @param MessageFormat|null $format Message format.
* @param MessageLink|null $link Link to message.
* @param bool $notify If false, chat participants would not be notified.
* @param bool $disableLinkPreview If false, server will not generate media preview for links in text.
*
* @return Message
* @throws ClientApiException
* @throws NetworkException
* @throws ReflectionException
* @throws SerializationException
* @codeCoverageIgnore
*/
public function sendUserMessage(
?int $userId = null,
?string $text = null,
?array $attachments = null,
?MessageFormat $format = null,
?MessageLink $link = null,
bool $notify = true,
bool $disableLinkPreview = false,
): Message {
return $this->sendMessage($userId, null, $text, $attachments, $format, $link, $notify, $disableLinkPreview);
}
/**
* Sends a message to a chat.
*
* @param int|null $chatId Fill this if you send message to chat.
* @param string|null $text Message text.
* @param AbstractAttachmentRequest[]|null $attachments Message attachments.
* @param MessageFormat|null $format Message format.
* @param MessageLink|null $link Link to message.
* @param bool $notify If false, chat participants would not be notified.
* @param bool $disableLinkPreview If false, server will not generate media preview for links in text.
*
* @return Message
* @throws ClientApiException
* @throws NetworkException
* @throws ReflectionException
* @throws SerializationException
* @codeCoverageIgnore
*/
public function sendChatMessage(
?int $chatId = null,
?string $text = null,
?array $attachments = null,
?MessageFormat $format = null,
?MessageLink $link = null,
bool $notify = true,
bool $disableLinkPreview = false,
): Message {
return $this->sendMessage(null, $chatId, $text, $attachments, $format, $link, $notify, $disableLinkPreview);
}
/**
* Returns the URL for the subsequent file upload.
*
@@ -779,7 +834,7 @@ class Api
[
'message_id' => $messageId,
'notify' => $notify,
]
],
)
);
}
@@ -877,7 +932,7 @@ class Api
* @throws ReflectionException
* @throws SerializationException
*/
public function removeMember(int $chatId, int $userId, bool $block = false): Result
public function deleteMember(int $chatId, int $userId, bool $block = false): Result
{
return $this->modelFactory->createResult(
$this->client->request(
@@ -890,4 +945,238 @@ class Api
)
);
}
/**
* Sets the administrators for a chat.
*
* @param int $chatId The identifier of the chat.
* @param ChatAdmin[] $admins An array of ChatAdmin objects representing the users and their permissions.
*
* @return Result
* @throws ClientApiException
* @throws NetworkException
* @throws ReflectionException
* @throws SerializationException
*/
public function addAdmins(int $chatId, array $admins): Result
{
return $this->modelFactory->createResult(
$this->client->request(
self::METHOD_POST,
sprintf(self::ACTION_CHATS_MEMBERS_ADMINS, $chatId),
[],
['admins' => array_map(fn(ChatAdmin $admin) => $admin->toArray(), $admins)],
)
);
}
/**
* Adds members to a chat. The bot may require additional permissions.
*
* @param int $chatId The identifier of the chat.
* @param int[] $userIds An array of user identifiers to add to the chat.
*
* @return Result
* @throws ClientApiException
* @throws NetworkException
* @throws ReflectionException
* @throws SerializationException
*/
public function addMembers(int $chatId, array $userIds): Result
{
return $this->modelFactory->createResult(
$this->client->request(
self::METHOD_POST,
sprintf(self::ACTION_CHATS_MEMBERS, $chatId),
[],
['user_ids' => $userIds],
)
);
}
/**
* Sends an answer to a callback query. This should be called after a user clicks an inline button.
*
* @param string $callbackId The identifier of the callback query.
* @param string|null $notification A short text notification to show to the user.
* @param string|null $text If provided, the original message will be edited with this text.
* @param AbstractAttachmentRequest[]|null $attachments New attachments for the edited message.
* @param MessageLink|null $link New link for the edited message.
* @param MessageFormat|null $format Formatting for the new message text.
* @param bool $notify Notification setting for the edited message.
*
* @return Result
* @throws ClientApiException
* @throws NetworkException
* @throws ReflectionException
* @throws SerializationException
*/
public function answerOnCallback(
string $callbackId,
?string $notification = null,
?string $text = null,
?array $attachments = null,
?MessageLink $link = null,
?MessageFormat $format = null,
bool $notify = true,
): Result {
$answerBody = ['notification' => $notification];
if ($text !== null || $attachments !== null || $link !== null) {
$answerBody['message'] = $this->buildNewMessageBody($text, $attachments, $format, $link, $notify);
}
return $this->modelFactory->createResult(
$this->client->request(
self::METHOD_POST,
self::ACTION_ANSWERS,
['callback_id' => $callbackId],
array_filter($answerBody, fn($value) => $value !== null)
)
);
}
/**
* Edits a message that was previously sent by the bot.
* Note on attachments:
* - To leave attachments unchanged, pass `null` (default).
* - To remove all attachments, pass an empty array `[]`.
*
* @param string $messageId The identifier of the message to edit.
* @param string|null $text New message text.
* @param AbstractAttachmentRequest[]|null $attachments New message attachments.
* @param MessageFormat|null $format Formatting for the new message text.
* @param MessageLink|null $link New link for the edited message.
* @param bool $notify Notification setting for the edited message.
*
* @return Result
* @throws ClientApiException
* @throws NetworkException
* @throws ReflectionException
* @throws SerializationException
*/
public function editMessage(
string $messageId,
?string $text = null,
?array $attachments = null,
?MessageFormat $format = null,
?MessageLink $link = null,
bool $notify = true,
): Result {
return $this->modelFactory->createResult(
$this->client->request(
self::METHOD_PUT,
self::ACTION_MESSAGES,
['message_id' => $messageId],
$this->buildNewMessageBody($text, $attachments, $format, $link, $notify),
)
);
}
/**
* Edits the bot info.
*
* Example: editBotInfo(new BotPatch(name: 'New Bot Name', description: null));
*
* @param BotPatch $botPatch
*
* @return BotInfo
* @throws ClientApiException
* @throws NetworkException
* @throws ReflectionException
* @throws SerializationException
*/
public function editBotInfo(BotPatch $botPatch): BotInfo
{
return $this->modelFactory->createBotInfo(
$this->client->request(
self::METHOD_PATCH,
self::ACTION_ME,
[],
$botPatch->toArray(),
)
);
}
/**
* Edits chat info such as title, icon, etc.
* Instantiate ChatPatch with named arguments for the fields you want to change.
*
* Example:
* $patch = new ChatPatch(title: 'New Cool Title');
* $api->editChat(12345, $patch);
*
* @param int $chatId The identifier of the chat to edit.
* @param ChatPatch $chatPatch An object containing the fields to update.
*
* @return Chat
* @throws ClientApiException
* @throws NetworkException
* @throws ReflectionException
* @throws SerializationException
*/
public function editChat(int $chatId, ChatPatch $chatPatch): Chat
{
return $this->modelFactory->createChat(
$this->client->request(
self::METHOD_PATCH,
self::ACTION_CHATS . '/' . $chatId,
[],
$chatPatch->toArray(),
)
);
}
/**
* Returns detailed information about a video attachment, including playback URLs.
*
* @param string $videoToken The token of the video attachment.
*
* @return VideoAttachmentDetails
* @throws ClientApiException
* @throws NetworkException
* @throws ReflectionException
* @throws SerializationException
*/
public function getVideoAttachmentDetails(string $videoToken): VideoAttachmentDetails
{
return $this->modelFactory->createVideoAttachmentDetails(
$this->client->request(
self::METHOD_GET,
sprintf(self::ACTION_VIDEO_DETAILS, $videoToken),
)
);
}
/**
* A helper to build the 'NewMessageBody' array structure consistently.
*
* @param string|null $text
* @param AbstractAttachmentRequest[]|null $attachments
* @param MessageFormat|null $format
* @param MessageLink|null $link
* @param bool $notify
*
* @return array<string, mixed>
* @throws ReflectionException
*/
private function buildNewMessageBody(
?string $text,
?array $attachments,
?MessageFormat $format,
?MessageLink $link,
bool $notify,
): array {
$body = [
'text' => $text,
'format' => $format?->value,
'notify' => $notify,
'link' => $link,
'attachments' => $attachments !== null ? array_map(
fn(AbstractModel $attachment) => $attachment->toArray(),
$attachments,
) : null,
];
return array_filter($body, fn($item) => $item !== null);
}
}
-5
View File
@@ -5,12 +5,7 @@ declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Exceptions;
use RuntimeException;
use Throwable;
class NetworkException extends RuntimeException
{
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
@@ -5,12 +5,7 @@ declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Exceptions;
use LogicException;
use Throwable;
class SerializationException extends LogicException
{
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
+14
View File
@@ -27,6 +27,7 @@ use BushlanovDev\MaxMessengerBot\Models\Updates\MessageRemovedUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\UserAddedToChatUpdate;
use BushlanovDev\MaxMessengerBot\Models\Updates\UserRemovedFromChatUpdate;
use BushlanovDev\MaxMessengerBot\Models\UploadEndpoint;
use BushlanovDev\MaxMessengerBot\Models\VideoAttachmentDetails;
use LogicException;
use ReflectionException;
@@ -234,4 +235,17 @@ class ModelFactory
{
return ChatMembersList::fromArray($data);
}
/**
* Creates a VideoAttachmentDetails object from raw API data.
*
* @param array<string, mixed> $data
*
* @return VideoAttachmentDetails
* @throws ReflectionException
*/
public function createVideoAttachmentDetails(array $data): VideoAttachmentDetails
{
return VideoAttachmentDetails::fromArray($data);
}
}
+59 -59
View File
@@ -42,6 +42,65 @@ abstract readonly class AbstractModel
return new static(...$constructorArgs); // @phpstan-ignore-line
}
/**
* @return array<string, mixed>
* @throws ReflectionException
*/
public function toArray(): array
{
$reflectionClass = new ReflectionClass($this);
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC);
$result = [];
foreach ($properties as $property) {
if (!$property->isInitialized($this)) {
continue;
}
$phpPropertyName = $property->getName();
$value = $property->getValue($this);
$jsonKey = self::toSnakeCase($phpPropertyName);
$result[$jsonKey] = $this->convertValue($value);
}
return $result;
}
/**
* @param string $input
*
* @return string
*/
protected static function toSnakeCase(string $input): string
{
return strtolower((string)preg_replace('/(?<!^)[A-Z]/', '_$0', $input));
}
/**
* @param mixed $value
*
* @return mixed
* @throws ReflectionException
*/
protected function convertValue(mixed $value): mixed
{
if ($value instanceof AbstractModel) {
return $value->toArray();
}
if (is_array($value)) {
return array_map([$this, 'convertValue'], $value);
}
if ($value instanceof BackedEnum) {
return $value->value;
}
return $value;
}
/**
* @param mixed $value
* @param ReflectionProperty $property
@@ -114,63 +173,4 @@ abstract readonly class AbstractModel
return $value;
}
/**
* @param string $input
*
* @return string
*/
private static function toSnakeCase(string $input): string
{
return strtolower((string)preg_replace('/(?<!^)[A-Z]/', '_$0', $input));
}
/**
* @return array<string, mixed>
* @throws ReflectionException
*/
public function toArray(): array
{
$reflectionClass = new ReflectionClass($this);
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC);
$result = [];
foreach ($properties as $property) {
if (!$property->isInitialized($this)) {
continue;
}
$phpPropertyName = $property->getName();
$value = $property->getValue($this);
$jsonKey = self::toSnakeCase($phpPropertyName);
$result[$jsonKey] = $this->convertValue($value);
}
return $result;
}
/**
* @param mixed $value
*
* @return mixed
* @throws ReflectionException
*/
private function convertValue(mixed $value): mixed
{
if ($value instanceof AbstractModel) {
return $value->toArray();
}
if (is_array($value)) {
return array_map([$this, 'convertValue'], $value);
}
if ($value instanceof BackedEnum) {
return $value->value;
}
return $value;
}
}
+42
View File
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models;
/**
* An abstract base for PATCH request models. It uses a variadic constructor
* to capture named arguments, tracking which fields were explicitly set.
*/
abstract readonly class AbstractPatchModel extends AbstractModel
{
/**
* @var array<int|string, mixed>
*/
protected array $patchData;
/**
* Captures all passed named arguments into an associative array.
*
* @param mixed ...$params
*/
public function __construct(...$params)
{
$this->patchData = $params;
}
/**
* @return array<string, mixed>
* @throws \ReflectionException
*/
final public function toArray(): array
{
$result = [];
foreach ($this->patchData as $key => $value) {
$jsonKey = $this->toSnakeCase((string)$key);
$result[$jsonKey] = $this->convertValue($value);
}
return $result;
}
}
+21
View File
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentRequestPayload;
/**
* Represents the data to patch for a bot. Instantiate with named arguments.
*
* Example: new BotPatch(name: 'New Bot Name', description: null);
*
* @property-read string|null $name
* @property-read string|null $description
* @property-read BotCommand[]|null $commands
* @property-read PhotoAttachmentRequestPayload|null $photo
*/
final readonly class BotPatch extends AbstractPatchModel
{
}
+25
View File
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models;
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
use BushlanovDev\MaxMessengerBot\Enums\ChatAdminPermission;
/**
* Represents an administrator to be set in a chat, linking a user ID with their permissions.
*/
final readonly class ChatAdmin extends AbstractModel
{
/**
* @param int $userId The identifier of the user to be made an admin.
* @param ChatAdminPermission[] $permissions The list of permissions to grant to the user.
*/
public function __construct(
public int $userId,
#[ArrayOf(ChatAdminPermission::class)]
public array $permissions,
) {
}
}
+21
View File
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentRequestPayload;
/**
* Represents the data to patch for a chat. Instantiate with named arguments.
*
* Example: new ChatPatch(title: 'New Chat Title');
*
* @property-read PhotoAttachmentRequestPayload|null $icon
* @property-read string|null $title
* @property-read string|null $pin Message ID to be pinned.
* @property-read bool|null $notify
*/
final readonly class ChatPatch extends AbstractPatchModel
{
}
+31
View File
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models;
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentRequestPayload;
/**
* Contains detailed information about a video attachment.
*/
final readonly class VideoAttachmentDetails extends AbstractModel
{
/**
* @param string $token The video attachment token.
* @param int $width The width of the video in pixels.
* @param int $height The height of the video in pixels.
* @param int $duration The duration of the video in seconds.
* @param VideoUrls|null $urls URLs to download or play the video. Can be null if the video is unavailable.
* @param PhotoAttachmentRequestPayload|null $thumbnail The video's thumbnail image information.
*/
public function __construct(
public string $token,
public int $width,
public int $height,
public int $duration,
public ?VideoUrls $urls = null,
public ?PhotoAttachmentRequestPayload $thumbnail = null,
) {
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Models;
/**
* Contains URLs for a video attachment in various resolutions.
*/
final readonly class VideoUrls extends AbstractModel
{
/**
* @param string|null $mp4_1080 Video URL in 1080p resolution, if available.
* @param string|null $mp4_720 Video URL in 720p resolution, if available.
* @param string|null $mp4_480 Video URL in 480p resolution, if available.
* @param string|null $mp4_360 Video URL in 360p resolution, if available.
* @param string|null $mp4_240 Video URL in 240p resolution, if available.
* @param string|null $mp4_144 Video URL in 144p resolution, if available.
* @param string|null $hls Live streaming URL (HLS), if available.
*/
public function __construct(
public ?string $mp4_1080 = null,
public ?string $mp4_720 = null,
public ?string $mp4_480 = null,
public ?string $mp4_360 = null,
public ?string $mp4_240 = null,
public ?string $mp4_144 = null,
public ?string $hls = null,
) {
}
}
+339 -3
View File
@@ -10,6 +10,7 @@ use BushlanovDev\MaxMessengerBot\Client;
use BushlanovDev\MaxMessengerBot\ClientApiInterface;
use BushlanovDev\MaxMessengerBot\Enums\AttachmentType;
use BushlanovDev\MaxMessengerBot\Enums\ButtonType;
use BushlanovDev\MaxMessengerBot\Enums\ChatAdminPermission;
use BushlanovDev\MaxMessengerBot\Enums\MessageFormat;
use BushlanovDev\MaxMessengerBot\Enums\SenderAction;
use BushlanovDev\MaxMessengerBot\Enums\UpdateType;
@@ -35,10 +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;
@@ -51,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;
@@ -107,6 +113,11 @@ use RuntimeException;
#[UsesClass(ChatMember::class)]
#[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;
@@ -1827,7 +1838,7 @@ final class ApiTest extends TestCase
$chatId = 12345;
$userId = 678;
$uri = sprintf('/chats/%d/members', $chatId);
$expectedQuery = ['user_id' => $userId];
$expectedQuery = ['user_id' => $userId, 'block' => false];
$rawResponse = ['success' => true];
$expectedResult = new Result(true, null);
@@ -1844,7 +1855,7 @@ final class ApiTest extends TestCase
->with($rawResponse)
->willReturn($expectedResult);
$result = $this->api->removeMember($chatId, $userId);
$result = $this->api->deleteMember($chatId, $userId);
$this->assertSame($expectedResult, $result);
}
@@ -1872,8 +1883,333 @@ final class ApiTest extends TestCase
->with($rawResponse)
->willReturn($expectedResult);
$result = $this->api->removeMember($chatId, $userId, true);
$result = $this->api->deleteMember($chatId, $userId, true);
$this->assertSame($expectedResult, $result);
}
#[Test]
public function postAdminsCallsClientWithCorrectBody(): void
{
$chatId = 12345;
$uri = sprintf('/chats/%d/members/admins', $chatId);
$admins = [
new ChatAdmin(101, [ChatAdminPermission::Write]),
new ChatAdmin(202, [ChatAdminPermission::PinMessage]),
];
$expectedBody = [
'admins' => [
['user_id' => 101, 'permissions' => ['write']],
['user_id' => 202, 'permissions' => ['pin_message']],
],
];
$rawResponse = ['success' => true];
$expectedResult = new Result(true, null);
$this->clientMock
->expects($this->once())
->method('request')
->with('POST', $uri, [], $expectedBody)
->willReturn($rawResponse);
$this->modelFactoryMock
->expects($this->once())
->method('createResult')
->with($rawResponse)
->willReturn($expectedResult);
$result = $this->api->addAdmins($chatId, $admins);
$this->assertSame($expectedResult, $result);
}
#[Test]
public function addMembersCallsClientWithCorrectBody(): void
{
$chatId = 12345;
$userIds = [101, 202, 303];
$uri = sprintf('/chats/%d/members', $chatId);
$expectedBody = ['user_ids' => $userIds];
$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->addMembers($chatId, $userIds);
$this->assertSame($expectedResult, $result);
}
#[Test]
public function answerOnCallbackWithNotificationOnly(): void
{
$callbackId = 'cb.123.abc';
$notification = 'Action confirmed!';
$expectedQuery = ['callback_id' => $callbackId];
$expectedBody = ['notification' => $notification];
$rawResponse = ['success' => true];
$expectedResult = new Result(true, null);
$this->clientMock
->expects($this->once())
->method('request')
->with('POST', '/answers', $expectedQuery, $expectedBody)
->willReturn($rawResponse);
$this->modelFactoryMock
->expects($this->once())
->method('createResult')
->with($rawResponse)
->willReturn($expectedResult);
$result = $this->api->answerOnCallback($callbackId, $notification);
$this->assertSame($expectedResult, $result);
}
#[Test]
public function answerOnCallbackWithMessageEdit(): void
{
$callbackId = 'cb.456.def';
$newText = 'Message updated!';
$expectedQuery = ['callback_id' => $callbackId];
$expectedBody = [
'message' => [
'text' => $newText,
'notify' => true,
],
];
$rawResponse = ['success' => true];
$expectedResult = new Result(true, null);
$this->clientMock
->expects($this->once())
->method('request')
->with('POST', '/answers', $expectedQuery, $expectedBody)
->willReturn($rawResponse);
$this->modelFactoryMock
->expects($this->once())
->method('createResult')
->with($rawResponse)
->willReturn($expectedResult);
$result = $this->api->answerOnCallback(callbackId: $callbackId, text: $newText);
$this->assertSame($expectedResult, $result);
}
#[Test]
public function answerOnCallbackWithBothMessageAndNotification(): void
{
$callbackId = 'cb.789.ghi';
$notification = 'Done!';
$newText = 'Updated!';
$expectedQuery = ['callback_id' => $callbackId];
$expectedBody = [
'notification' => $notification,
'message' => [
'text' => $newText,
'notify' => true,
],
];
$rawResponse = ['success' => true];
$expectedResult = new Result(true, null);
$this->clientMock
->expects($this->once())
->method('request')
->with('POST', '/answers', $expectedQuery, $expectedBody)
->willReturn($rawResponse);
$this->modelFactoryMock
->expects($this->once())
->method('createResult')
->with($rawResponse)
->willReturn($expectedResult);
$result = $this->api->answerOnCallback(
callbackId: $callbackId,
notification: $notification,
text: $newText
);
$this->assertSame($expectedResult, $result);
}
#[Test]
public function editMessageCallsClientWithCorrectParameters(): void
{
$messageId = 'mid.123.abc';
$newText = 'This is the edited text.';
$expectedQuery = ['message_id' => $messageId];
$expectedBody = [
'text' => $newText,
'notify' => true,
];
$rawResponse = ['success' => true];
$expectedResult = new Result(true, null);
$this->clientMock
->expects($this->once())
->method('request')
->with('PUT', '/messages', $expectedQuery, $expectedBody)
->willReturn($rawResponse);
$this->modelFactoryMock
->expects($this->once())
->method('createResult')
->with($rawResponse)
->willReturn($expectedResult);
$result = $this->api->editMessage($messageId, $newText);
$this->assertSame($expectedResult, $result);
}
#[Test]
public function editMessageCanClearAttachmentsWithEmptyArray(): void
{
$messageId = 'mid.456.def';
$expectedQuery = ['message_id' => $messageId];
$expectedBody = [
'attachments' => [],
'notify' => true,
];
$rawResponse = ['success' => true];
$expectedResult = new Result(true, null);
$this->clientMock
->expects($this->once())
->method('request')
->with('PUT', '/messages', $expectedQuery, $expectedBody)
->willReturn($rawResponse);
$this->modelFactoryMock
->expects($this->once())
->method('createResult')
->with($rawResponse)
->willReturn($expectedResult);
$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());
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace BushlanovDev\MaxMessengerBot\Tests\Models;
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
use BushlanovDev\MaxMessengerBot\Enums\ChatAdminPermission;
use BushlanovDev\MaxMessengerBot\Models\ChatAdmin;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(ChatAdmin::class)]
#[UsesClass(ArrayOf::class)]
final class ChatAdminTest extends TestCase
{
#[Test]
public function toArraySerializesCorrectly(): void
{
$admin = new ChatAdmin(123, [ChatAdminPermission::Write, ChatAdminPermission::PinMessage]);
$expected = [
'user_id' => 123,
'permissions' => ['write', 'pin_message'],
];
$this->assertEquals($expected, $admin->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);
}
}