-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathIntegrationTest.php
More file actions
126 lines (116 loc) · 4.78 KB
/
IntegrationTest.php
File metadata and controls
126 lines (116 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php declare(strict_types=1);
namespace danog\Decoder\Test;
use CURLFile;
use danog\Decoder\FileId;
use danog\Decoder\FileIdType;
use danog\Decoder\UniqueFileId;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
/**
* @api
* @internal
*/
class IntegrationTest extends TestCase
{
#[DataProvider('provideFileIdsAndType')]
public function testAll(FileIdType $type, string $fileIdStr, string $uniqueFileIdStr): void
{
$fileId = FileId::fromBotAPI($fileIdStr);
$this->assertSame($type, $fileId->type);
$this->assertSame($fileIdStr, $fileId->getBotAPI());
$uniqueFileId = UniqueFileId::fromUniqueBotAPI($uniqueFileIdStr);
$this->assertSame($type->toUnique(), $uniqueFileId->type);
$this->assertSame($uniqueFileIdStr, $uniqueFileId->getUniqueBotAPI());
$this->assertSame($uniqueFileIdStr, $fileId->getUnique()->getUniqueBotAPI());
}
/** @psalm-suppress MixedArrayAccess, MixedAssignment, PossiblyInvalidArgument, MixedArgument */
public static function provideFileIdsAndType(): \Generator
{
foreach (['CAADBAADwwADmFmqDf6xBrPTReqHFgQ', 'CAACAgQAAxkBAAIC4l9CWDGzVUcDejU0TETLWbOdfsCoAALDAAOYWaoN_rEGs9NF6ocbBA', 'CAADBAADwwADmFmqDf6xBrPTReqHAg'] as $fileId) {
yield [
FileIdType::STICKER,
$fileId,
'AgADwwADmFmqDQ'
];
}
$dest = \getenv('DEST');
$token = \getenv('TOKEN');
foreach (self::provideChats() as $chat) {
/**
* @var array{
* small_file_id: string,
* small_file_unique_id: string,
* big_file_id: string,
* big_file_unique_id: string
* }
*/
$result = \json_decode(\file_get_contents("https://api.telegram.org/bot$token/getChat?chat_id=$chat"), true)['result']['photo'];
yield [
FileIdType::PROFILE_PHOTO,
$result['small_file_id'],
$result['small_file_unique_id'],
];
yield [
FileIdType::from('profile_photo'),
$result['big_file_id'],
$result['big_file_unique_id'],
];
}
foreach (self::provideUrls() as $type => $url) {
if ($type === 'video_note') {
\copy($url, \basename($url));
$handle = \curl_init("https://api.telegram.org/bot$token/sendVideoNote?chat_id=$dest");
\curl_setopt($handle, CURLOPT_POST, true);
\curl_setopt($handle, CURLOPT_POSTFIELDS, [
$type => new CURLFile(\basename($url))
]);
\curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$botResult = \json_decode(\curl_exec($handle), true);
\curl_close($handle);
\unlink(\basename($url));
} else {
$botResult = \json_decode(\file_get_contents("https://api.telegram.org/bot$token/send$type?chat_id=$dest&$type=$url"), true);
}
$botResult = $botResult['result'][$type];
if ($type !== 'photo') {
$botResult = [$botResult];
}
foreach ($botResult as $subResult) {
/** @var string $type */
yield [
FileIdType::from($type),
$subResult['file_id'],
$subResult['file_unique_id']
];
if (isset($subResult['thumb'])) {
yield [
FileIdType::from('thumbnail'),
$subResult['thumb']['file_id'],
$subResult['thumb']['file_unique_id']
];
}
}
}
}
/**
* @psalm-suppress InvalidReturnStatement, InvalidReturnType
* @return list<string>
*/
public static function provideChats(): array
{
return [\getenv('DEST'), '@MadelineProto'];
}
public static function provideUrls(): array
{
return [
'sticker' => 'https://github.com/danog/MadelineProto/raw/v8/tests/lel.webp?raw=true',
'photo' => 'https://github.com/danog/MadelineProto/raw/v8/tests/faust.jpg',
'audio' => 'https://github.com/danog/MadelineProto/raw/v8/tests/mosconi.mp3?raw=true',
'video' => 'https://github.com/danog/MadelineProto/raw/v8/tests/swing.mp4?raw=true',
'animation' => 'https://github.com/danog/MadelineProto/raw/v8/tests/pony.mp4?raw=true',
'document' => 'https://github.com/danog/danog.github.io/raw/master/lol/index_htm_files/0.gif',
'voice' => 'https://paste.daniil.it/a.ogg',
'video_note' => 'https://paste.daniil.it/round.mp4'
];
}
}