diff --git a/src/Api.php b/src/Api.php index 81fe3bf..4a3894d 100644 --- a/src/Api.php +++ b/src/Api.php @@ -14,6 +14,7 @@ use BushlanovDev\MaxMessengerBot\Models\AbstractModel; use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\AbstractAttachmentRequest; use BushlanovDev\MaxMessengerBot\Models\Attachments\Requests\PhotoAttachmentRequest; use BushlanovDev\MaxMessengerBot\Models\BotInfo; +use BushlanovDev\MaxMessengerBot\Models\Chat; use BushlanovDev\MaxMessengerBot\Models\Message; use BushlanovDev\MaxMessengerBot\Models\MessageLink; use BushlanovDev\MaxMessengerBot\Models\Result; @@ -43,6 +44,7 @@ class Api private const string ACTION_SUBSCRIPTIONS = '/subscriptions'; private const string ACTION_MESSAGES = '/messages'; private const string ACTION_UPLOADS = '/uploads'; + private const string ACTION_CHATS = '/chats'; private readonly ClientApiInterface $client; @@ -237,13 +239,13 @@ class Api * @param string $filePath Path to the file on the local disk. * * @return AbstractAttachmentRequest - * @throws ReflectionException - * @throws SerializationException * @throws InvalidArgumentException * @throws RuntimeException * @throws LogicException - * @throws NetworkException * @throws ClientApiException + * @throws NetworkException + * @throws SerializationException + * @throws ReflectionException */ public function uploadAttachment(UploadType $type, string $filePath): AbstractAttachmentRequest { @@ -277,4 +279,22 @@ class Api ), }; } + + /** + * Returns info about chat. + * + * @param int $chatId Requested chat identifier. + * + * @return Chat + * @throws ClientApiException + * @throws NetworkException + * @throws SerializationException + * @throws ReflectionException + */ + public function getChat(int $chatId): Chat + { + return $this->modelFactory->createChat( + $this->client->request(self::METHOD_GET, self::ACTION_CHATS . '/' . $chatId) + ); + } } diff --git a/src/Enums/ChatStatus.php b/src/Enums/ChatStatus.php new file mode 100644 index 0000000..2c3cfe2 --- /dev/null +++ b/src/Enums/ChatStatus.php @@ -0,0 +1,14 @@ + $data + * + * @return Chat + * @throws ReflectionException + */ + public function createChat(array $data): Chat + { + return Chat::fromArray($data); + } } diff --git a/src/Models/Chat.php b/src/Models/Chat.php new file mode 100644 index 0000000..bbb6a7b --- /dev/null +++ b/src/Models/Chat.php @@ -0,0 +1,50 @@ +expectExceptionMessageMatches('/File not found or not readable/'); $this->api->uploadAttachment(UploadType::Image, '/path/to/non/existent/file.jpg'); } + + #[Test] + public function getChatCallsClientAndFactoryCorrectly(): void + { + $chatId = 100123456789; + $rawResponseData = [ + 'chat_id' => $chatId, + 'type' => 'chat', + 'status' => 'active', + 'last_event_time' => 1678886400000, + 'participants_count' => 50, + 'is_public' => false, + 'title' => 'Test Chat Title', + 'icon' => null, + 'owner_id' => null, + 'link' => null, + 'description' => null, + 'dialog_with_user' => null, + 'messages_count' => null, + 'chat_message_id' => null, + 'pinned_message' => null, + ]; + + $expectedChatObject = Chat::fromArray($rawResponseData); + + $this->clientMock + ->expects($this->once()) + ->method('request') + ->with('GET', '/chats/' . $chatId) + ->willReturn($rawResponseData); + + $this->modelFactoryMock + ->expects($this->once()) + ->method('createChat') + ->with($rawResponseData) + ->willReturn($expectedChatObject); + + $result = $this->api->getChat($chatId); + + $this->assertSame($expectedChatObject, $result); + } }