tg-file-decoder/tests/IntegrationTest.php

124 lines
4.8 KiB
PHP
Raw Normal View History

2024-04-01 14:28:39 +02:00
<?php declare(strict_types=1);
2020-02-03 21:17:53 +01:00
namespace danog\Decoder\Test;
use CURLFile;
use danog\Decoder\FileId;
2024-04-01 17:36:44 +02:00
use danog\Decoder\FileIdType;
2020-02-03 21:17:53 +01:00
use danog\Decoder\UniqueFileId;
2024-04-01 17:39:42 +02:00
use PHPUnit\Framework\Attributes\DataProvider;
2020-02-03 21:17:53 +01:00
use PHPUnit\Framework\TestCase;
2024-04-01 18:03:39 +02:00
/** @internal */
2020-02-03 21:17:53 +01:00
class IntegrationTest extends TestCase
{
2024-04-01 17:39:42 +02:00
#[DataProvider('provideFileIdsAndType')]
2024-04-01 17:36:44 +02:00
public function testAll(FileIdType $type, string $fileIdStr, string $uniqueFileIdStr): void
2020-02-03 21:17:53 +01:00
{
$fileId = FileId::fromBotAPI($fileIdStr);
2024-04-01 17:36:44 +02:00
$this->assertSame($type, $fileId->type);
2020-02-03 21:17:53 +01:00
$this->assertSame($fileIdStr, $fileId->getBotAPI());
$uniqueFileId = UniqueFileId::fromUniqueBotAPI($uniqueFileIdStr);
2024-04-01 17:36:44 +02:00
$this->assertSame($type->toUnique(), $uniqueFileId->type);
2020-02-03 21:17:53 +01:00
$this->assertSame($uniqueFileIdStr, $uniqueFileId->getUniqueBotAPI());
$this->assertSame($uniqueFileIdStr, $fileId->getUnique()->getUniqueBotAPI());
}
2024-04-01 17:36:44 +02:00
/** @psalm-suppress MixedArrayAccess, MixedAssignment, PossiblyInvalidArgument, MixedArgument */
2024-04-01 17:39:42 +02:00
public static function provideFileIdsAndType(): \Generator
2020-02-03 21:17:53 +01:00
{
2020-08-24 13:28:17 +02:00
foreach (['CAADBAADwwADmFmqDf6xBrPTReqHFgQ', 'CAACAgQAAxkBAAIC4l9CWDGzVUcDejU0TETLWbOdfsCoAALDAAOYWaoN_rEGs9NF6ocbBA', 'CAADBAADwwADmFmqDf6xBrPTReqHAg'] as $fileId) {
yield [
2024-04-01 17:36:44 +02:00
FileIdType::STICKER,
2020-08-24 13:28:17 +02:00
$fileId,
'AgADwwADmFmqDQ'
];
}
2020-02-03 21:17:53 +01:00
$dest = \getenv('DEST');
$token = \getenv('TOKEN');
2024-04-01 17:39:42 +02:00
foreach (self::provideChats() as $chat) {
2024-04-01 17:36:44 +02:00
/**
* @var array{
* small_file_id: string,
* small_file_unique_id: string,
* big_file_id: string,
* big_file_unique_id: string
* }
*/
2020-02-03 23:47:37 +01:00
$result = \json_decode(\file_get_contents("https://api.telegram.org/bot$token/getChat?chat_id=$chat"), true)['result']['photo'];
yield [
2024-04-01 17:36:44 +02:00
FileIdType::PROFILE_PHOTO,
2020-02-03 23:47:37 +01:00
$result['small_file_id'],
$result['small_file_unique_id'],
];
yield [
2024-04-01 17:36:44 +02:00
FileIdType::fromBotApiType('profile_photo'),
2020-02-03 23:47:37 +01:00
$result['big_file_id'],
$result['big_file_unique_id'],
];
}
2024-04-01 17:39:42 +02:00
foreach (self::provideUrls() as $type => $url) {
2020-02-03 21:17:53 +01:00
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) {
2024-04-01 17:36:44 +02:00
/** @var string $type */
2020-02-03 23:47:37 +01:00
yield [
2024-04-01 17:36:44 +02:00
FileIdType::fromBotApiType($type),
2020-02-03 21:17:53 +01:00
$subResult['file_id'],
$subResult['file_unique_id']
];
if (isset($subResult['thumb'])) {
2020-02-03 23:47:37 +01:00
yield [
2024-04-01 17:36:44 +02:00
FileIdType::fromBotApiType('thumbnail'),
2020-02-03 21:17:53 +01:00
$subResult['thumb']['file_id'],
$subResult['thumb']['file_unique_id']
];
}
}
}
}
2024-04-01 17:36:44 +02:00
/**
* @psalm-suppress InvalidReturnStatement, InvalidReturnType
* @return list<string>
*/
2024-04-01 17:39:42 +02:00
public static function provideChats(): array
2020-02-03 23:47:37 +01:00
{
return [\getenv('DEST'), '@MadelineProto'];
}
2024-04-01 17:39:42 +02:00
public static function provideUrls(): array
2020-02-03 21:17:53 +01:00
{
return [
2023-11-30 18:05:33 +01:00
'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',
2020-02-03 21:17:53 +01:00
'document' => 'https://github.com/danog/danog.github.io/raw/master/lol/index_htm_files/0.gif',
'voice' => 'https://daniil.it/audio_2020-02-01_18-09-08.ogg',
'video_note' => 'https://daniil.it/round.mp4'
];
}
}