Skip to content

Commit 32e393e

Browse files
committed
fix #1 TypeError Api::sendUserMessage после отправки сообщения
Added PhotoAttachmentRequestPayloadTest
1 parent 728536f commit 32e393e

4 files changed

Lines changed: 116 additions & 8 deletions

File tree

src/Models/Message.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
{
1212
/**
1313
* @param int $timestamp Unix-time when message was created.
14-
* @param MessageBody $body Body of created message. Text + attachments.
1514
* @param Recipient $recipient Message recipient. Could be user or chat.
15+
* @param MessageBody|null $body Body of created message. Text + attachments.
1616
* @param User|null $sender User who sent this message. Can be null if message has been posted on behalf of a channel.
1717
* @param string|null $url Message public URL. Can be null for dialogs or non-public chats/channels.
1818
* @param LinkedMessage|null $link Forwarded or replied message.
1919
* @param MessageStat|null $stat Message statistics. Available only for channels.
2020
*/
2121
public function __construct(
2222
public int $timestamp,
23-
public MessageBody $body,
2423
public Recipient $recipient,
24+
public ?MessageBody $body,
2525
public ?User $sender,
2626
public ?string $url,
2727
public ?LinkedMessage $link,
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BushlanovDev\MaxMessengerBot\Tests\Models\Attachments\Payloads;
6+
7+
use BushlanovDev\MaxMessengerBot\Attributes\ArrayOf;
8+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoAttachmentRequestPayload;
9+
use BushlanovDev\MaxMessengerBot\Models\Attachments\Payloads\PhotoToken;
10+
use InvalidArgumentException;
11+
use PHPUnit\Framework\Attributes\CoversClass;
12+
use PHPUnit\Framework\Attributes\DataProvider;
13+
use PHPUnit\Framework\Attributes\Test;
14+
use PHPUnit\Framework\Attributes\UsesClass;
15+
use PHPUnit\Framework\TestCase;
16+
17+
#[CoversClass(PhotoAttachmentRequestPayload::class)]
18+
#[UsesClass(PhotoToken::class)]
19+
#[UsesClass(ArrayOf::class)]
20+
final class PhotoAttachmentRequestPayloadTest extends TestCase
21+
{
22+
#[Test]
23+
public function canBeCreatedWithUrlOnly(): void
24+
{
25+
$payload = new PhotoAttachmentRequestPayload(url: 'https://example.com/photo.jpg');
26+
27+
$this->assertSame('https://example.com/photo.jpg', $payload->url);
28+
$this->assertNull($payload->token);
29+
$this->assertNull($payload->photos);
30+
31+
$expectedArray = [
32+
'url' => 'https://example.com/photo.jpg',
33+
'token' => null,
34+
'photos' => null,
35+
];
36+
$this->assertEquals($expectedArray, $payload->toArray());
37+
}
38+
39+
#[Test]
40+
public function canBeCreatedWithTokenOnly(): void
41+
{
42+
$payload = new PhotoAttachmentRequestPayload(token: 'uploaded_token_abc');
43+
44+
$this->assertSame('uploaded_token_abc', $payload->token);
45+
$this->assertNull($payload->url);
46+
$this->assertNull($payload->photos);
47+
48+
$expectedArray = [
49+
'token' => 'uploaded_token_abc',
50+
'url' => null,
51+
'photos' => null,
52+
];
53+
$this->assertEquals($expectedArray, $payload->toArray());
54+
}
55+
56+
#[Test]
57+
public function canBeCreatedWithPhotosOnly(): void
58+
{
59+
$photos = [
60+
new PhotoToken('token_1'),
61+
new PhotoToken('token_2'),
62+
];
63+
$payload = new PhotoAttachmentRequestPayload(photos: $photos);
64+
65+
$this->assertSame($photos, $payload->photos);
66+
$this->assertNull($payload->url);
67+
$this->assertNull($payload->token);
68+
69+
$expectedArray = [
70+
'photos' => [
71+
['token' => 'token_1'],
72+
['token' => 'token_2'],
73+
],
74+
'url' => null,
75+
'token' => null,
76+
];
77+
$this->assertEquals($expectedArray, $payload->toArray());
78+
}
79+
80+
/**
81+
* Data provider for invalid constructor arguments.
82+
*
83+
* @return array<string, array{0: string|null, 1: string|null, 2: array|null}>
84+
*/
85+
public static function invalidPayloadProvider(): array
86+
{
87+
return [
88+
'all null (no arguments)' => [null, null, null],
89+
'url and token provided' => ['https://a.com', 'token123', null],
90+
'url and photos provided' => ['https://a.com', null, [new PhotoToken('t')]],
91+
'token and photos provided' => [null, 'token123', [new PhotoToken('t')]],
92+
'all three arguments provided' => ['https://a.com', 'token123', [new PhotoToken('t')]],
93+
];
94+
}
95+
96+
#[Test]
97+
#[DataProvider('invalidPayloadProvider')]
98+
public function constructorThrowsExceptionForInvalidArguments(
99+
?string $url,
100+
?string $token,
101+
?array $photos
102+
): void {
103+
$this->expectException(InvalidArgumentException::class);
104+
$this->expectExceptionMessage('Provide exactly one of "url", "token", or "photos" for PhotoAttachmentRequestPayload.');
105+
106+
new PhotoAttachmentRequestPayload($url, $token, $photos);
107+
}
108+
}

tests/Models/MessageTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ public function canBeCreatedFromArrayWithAllData(): void
2828
{
2929
$data = [
3030
'timestamp' => time(),
31+
'recipient' => [
32+
'chat_type' => 'dialog',
33+
'user_id' => 123,
34+
'chat_id' => null,
35+
],
3136
'body' => [
3237
'mid' => 'mid.456.xyz',
3338
'seq' => 101,
3439
'text' => 'Hello, **world**!',
3540
'attachments' => null,
3641
'markup' => null,
3742
],
38-
'recipient' => [
39-
'chat_type' => 'dialog',
40-
'user_id' => 123,
41-
'chat_id' => null,
42-
],
4343
'sender' =>[
4444
'user_id' => 123,
4545
'first_name' => 'John',

tests/WebhookHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ private function createRealUpdateObject(array $data): MessageCreatedUpdate
7979
);
8080
$message = new Message(
8181
$data['message']['timestamp'],
82-
$messageBody,
8382
$recipient,
83+
$messageBody,
8484
null,
8585
null,
8686
null,

0 commit comments

Comments
 (0)