mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-08-30 12:07:05 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 32e393e044 |
@@ -11,8 +11,8 @@ final readonly class Message extends AbstractModel
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param int $timestamp Unix-time when message was created.
|
* @param int $timestamp Unix-time when message was created.
|
||||||
* @param MessageBody $body Body of created message. Text + attachments.
|
|
||||||
* @param Recipient $recipient Message recipient. Could be user or chat.
|
* @param Recipient $recipient Message recipient. Could be user or chat.
|
||||||
|
* @param MessageBody|null $body Body of created message. Text + attachments.
|
||||||
* @param User|null $sender User who sent this message. Can be null if message has been posted on behalf of a channel.
|
* @param User|null $sender User who sent this message. Can be null if message has been posted on behalf of a channel.
|
||||||
* @param string|null $url Message public URL. Can be null for dialogs or non-public chats/channels.
|
* @param string|null $url Message public URL. Can be null for dialogs or non-public chats/channels.
|
||||||
* @param LinkedMessage|null $link Forwarded or replied message.
|
* @param LinkedMessage|null $link Forwarded or replied message.
|
||||||
@@ -20,8 +20,8 @@ final readonly class Message extends AbstractModel
|
|||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public int $timestamp,
|
public int $timestamp,
|
||||||
public MessageBody $body,
|
|
||||||
public Recipient $recipient,
|
public Recipient $recipient,
|
||||||
|
public ?MessageBody $body,
|
||||||
public ?User $sender,
|
public ?User $sender,
|
||||||
public ?string $url,
|
public ?string $url,
|
||||||
public ?LinkedMessage $link,
|
public ?LinkedMessage $link,
|
||||||
|
|||||||
@@ -0,0 +1,108 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Attachments\Payloads;
|
||||||
|
|
||||||
|
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
|
||||||
|
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentRequestPayload;
|
||||||
|
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoToken;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
use PHPUnit\Framework\Attributes\Test;
|
||||||
|
use PHPUnit\Framework\Attributes\UsesClass;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
#[CoversClass(PhotoAttachmentRequestPayload::class)]
|
||||||
|
#[UsesClass(PhotoToken::class)]
|
||||||
|
#[UsesClass(ArrayOf::class)]
|
||||||
|
final class PhotoAttachmentRequestPayloadTest extends TestCase
|
||||||
|
{
|
||||||
|
#[Test]
|
||||||
|
public function canBeCreatedWithUrlOnly(): void
|
||||||
|
{
|
||||||
|
$payload = new PhotoAttachmentRequestPayload(url: 'https://example.com/photo.jpg');
|
||||||
|
|
||||||
|
$this->assertSame('https://example.com/photo.jpg', $payload->url);
|
||||||
|
$this->assertNull($payload->token);
|
||||||
|
$this->assertNull($payload->photos);
|
||||||
|
|
||||||
|
$expectedArray = [
|
||||||
|
'url' => 'https://example.com/photo.jpg',
|
||||||
|
'token' => null,
|
||||||
|
'photos' => null,
|
||||||
|
];
|
||||||
|
$this->assertEquals($expectedArray, $payload->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Test]
|
||||||
|
public function canBeCreatedWithTokenOnly(): void
|
||||||
|
{
|
||||||
|
$payload = new PhotoAttachmentRequestPayload(token: 'uploaded_token_abc');
|
||||||
|
|
||||||
|
$this->assertSame('uploaded_token_abc', $payload->token);
|
||||||
|
$this->assertNull($payload->url);
|
||||||
|
$this->assertNull($payload->photos);
|
||||||
|
|
||||||
|
$expectedArray = [
|
||||||
|
'token' => 'uploaded_token_abc',
|
||||||
|
'url' => null,
|
||||||
|
'photos' => null,
|
||||||
|
];
|
||||||
|
$this->assertEquals($expectedArray, $payload->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Test]
|
||||||
|
public function canBeCreatedWithPhotosOnly(): void
|
||||||
|
{
|
||||||
|
$photos = [
|
||||||
|
new PhotoToken('token_1'),
|
||||||
|
new PhotoToken('token_2'),
|
||||||
|
];
|
||||||
|
$payload = new PhotoAttachmentRequestPayload(photos: $photos);
|
||||||
|
|
||||||
|
$this->assertSame($photos, $payload->photos);
|
||||||
|
$this->assertNull($payload->url);
|
||||||
|
$this->assertNull($payload->token);
|
||||||
|
|
||||||
|
$expectedArray = [
|
||||||
|
'photos' => [
|
||||||
|
['token' => 'token_1'],
|
||||||
|
['token' => 'token_2'],
|
||||||
|
],
|
||||||
|
'url' => null,
|
||||||
|
'token' => null,
|
||||||
|
];
|
||||||
|
$this->assertEquals($expectedArray, $payload->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data provider for invalid constructor arguments.
|
||||||
|
*
|
||||||
|
* @return array<string, array{0: string|null, 1: string|null, 2: array|null}>
|
||||||
|
*/
|
||||||
|
public static function invalidPayloadProvider(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'all null (no arguments)' => [null, null, null],
|
||||||
|
'url and token provided' => ['https://a.com', 'token123', null],
|
||||||
|
'url and photos provided' => ['https://a.com', null, [new PhotoToken('t')]],
|
||||||
|
'token and photos provided' => [null, 'token123', [new PhotoToken('t')]],
|
||||||
|
'all three arguments provided' => ['https://a.com', 'token123', [new PhotoToken('t')]],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Test]
|
||||||
|
#[DataProvider('invalidPayloadProvider')]
|
||||||
|
public function constructorThrowsExceptionForInvalidArguments(
|
||||||
|
?string $url,
|
||||||
|
?string $token,
|
||||||
|
?array $photos
|
||||||
|
): void {
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
$this->expectExceptionMessage('Provide exactly one of "url", "token", or "photos" for PhotoAttachmentRequestPayload.');
|
||||||
|
|
||||||
|
new PhotoAttachmentRequestPayload($url, $token, $photos);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,6 +28,11 @@ final class MessageTest extends TestCase
|
|||||||
{
|
{
|
||||||
$data = [
|
$data = [
|
||||||
'timestamp' => time(),
|
'timestamp' => time(),
|
||||||
|
'recipient' => [
|
||||||
|
'chat_type' => 'dialog',
|
||||||
|
'user_id' => 123,
|
||||||
|
'chat_id' => null,
|
||||||
|
],
|
||||||
'body' => [
|
'body' => [
|
||||||
'mid' => 'mid.456.xyz',
|
'mid' => 'mid.456.xyz',
|
||||||
'seq' => 101,
|
'seq' => 101,
|
||||||
@@ -35,11 +40,6 @@ final class MessageTest extends TestCase
|
|||||||
'attachments' => null,
|
'attachments' => null,
|
||||||
'markup' => null,
|
'markup' => null,
|
||||||
],
|
],
|
||||||
'recipient' => [
|
|
||||||
'chat_type' => 'dialog',
|
|
||||||
'user_id' => 123,
|
|
||||||
'chat_id' => null,
|
|
||||||
],
|
|
||||||
'sender' =>[
|
'sender' =>[
|
||||||
'user_id' => 123,
|
'user_id' => 123,
|
||||||
'first_name' => 'John',
|
'first_name' => 'John',
|
||||||
|
|||||||
@@ -79,8 +79,8 @@ final class WebhookHandlerTest extends TestCase
|
|||||||
);
|
);
|
||||||
$message = new Message(
|
$message = new Message(
|
||||||
$data['message']['timestamp'],
|
$data['message']['timestamp'],
|
||||||
$messageBody,
|
|
||||||
$recipient,
|
$recipient,
|
||||||
|
$messageBody,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
|
|||||||
Reference in New Issue
Block a user