This commit is contained in:
Daniil Gentili 2024-04-01 17:36:44 +02:00
parent 9d9c253848
commit 34338dba16
3 changed files with 49 additions and 13 deletions

View File

@ -11,6 +11,7 @@
> >
<projectFiles> <projectFiles>
<directory name="src" /> <directory name="src" />
<directory name="tests" />
<ignoreFiles> <ignoreFiles>
<directory name="vendor" /> <directory name="vendor" />
</ignoreFiles> </ignoreFiles>

View File

@ -98,6 +98,30 @@ enum FileIdType: int
case SIZE = 17; case SIZE = 17;
case NONE = 18; case NONE = 18;
public static function fromBotApiType(string $type): self
{
return match ($type) {
'thumbnail' => self::THUMBNAIL,
'profile_photo' => self::PROFILE_PHOTO,
'photo' => self::PHOTO,
'voice' => self::VOICE,
'video' => self::VIDEO,
'document' => self::DOCUMENT,
'encrypted' => self::ENCRYPTED,
'temp' => self::TEMP,
'sticker' => self::STICKER,
'audio' => self::AUDIO,
'animation' => self::ANIMATION,
'encrypted_thumbnail' => self::ENCRYPTED_THUMBNAIL,
'wallpaper' => self::WALLPAPER,
'video_note' => self::VIDEO_NOTE,
'secure_raw' => self::SECURE_RAW,
'secure' => self::SECURE,
'background' => self::BACKGROUND,
'size' => self::SIZE,
};
}
/** /**
* Convert file ID type to unique file ID type. * Convert file ID type to unique file ID type.
*/ */

View File

@ -4,38 +4,36 @@ namespace danog\Decoder\Test;
use CURLFile; use CURLFile;
use danog\Decoder\FileId; use danog\Decoder\FileId;
use danog\Decoder\FileIdType;
use danog\Decoder\UniqueFileId; use danog\Decoder\UniqueFileId;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use const danog\Decoder\FULL_UNIQUE_MAP; /** @api */
use const danog\Decoder\TYPES_IDS;
class IntegrationTest extends TestCase class IntegrationTest extends TestCase
{ {
/** /**
* @param string $type Expected type
*
* @dataProvider provideFileIdsAndType * @dataProvider provideFileIdsAndType
*/ */
public function testAll(string $type, string $fileIdStr, string $uniqueFileIdStr) public function testAll(FileIdType $type, string $fileIdStr, string $uniqueFileIdStr): void
{ {
$fileId = FileId::fromBotAPI($fileIdStr); $fileId = FileId::fromBotAPI($fileIdStr);
$this->assertSame($type, $fileId->getTypeName()); $this->assertSame($type, $fileId->type);
$this->assertSame($fileIdStr, $fileId->getBotAPI()); $this->assertSame($fileIdStr, $fileId->getBotAPI());
$uniqueFileId = UniqueFileId::fromUniqueBotAPI($uniqueFileIdStr); $uniqueFileId = UniqueFileId::fromUniqueBotAPI($uniqueFileIdStr);
$this->assertSame(FULL_UNIQUE_MAP[TYPES_IDS[$type]], $uniqueFileId->getType()); $this->assertSame($type->toUnique(), $uniqueFileId->type);
$this->assertSame($uniqueFileIdStr, $uniqueFileId->getUniqueBotAPI()); $this->assertSame($uniqueFileIdStr, $uniqueFileId->getUniqueBotAPI());
$this->assertSame($uniqueFileIdStr, $fileId->getUnique()->getUniqueBotAPI()); $this->assertSame($uniqueFileIdStr, $fileId->getUnique()->getUniqueBotAPI());
} }
/** @psalm-suppress MixedArrayAccess, MixedAssignment, PossiblyInvalidArgument, MixedArgument */
public function provideFileIdsAndType(): \Generator public function provideFileIdsAndType(): \Generator
{ {
foreach (['CAADBAADwwADmFmqDf6xBrPTReqHFgQ', 'CAACAgQAAxkBAAIC4l9CWDGzVUcDejU0TETLWbOdfsCoAALDAAOYWaoN_rEGs9NF6ocbBA', 'CAADBAADwwADmFmqDf6xBrPTReqHAg'] as $fileId) { foreach (['CAADBAADwwADmFmqDf6xBrPTReqHFgQ', 'CAACAgQAAxkBAAIC4l9CWDGzVUcDejU0TETLWbOdfsCoAALDAAOYWaoN_rEGs9NF6ocbBA', 'CAADBAADwwADmFmqDf6xBrPTReqHAg'] as $fileId) {
yield [ yield [
'sticker', FileIdType::STICKER,
$fileId, $fileId,
'AgADwwADmFmqDQ' 'AgADwwADmFmqDQ'
]; ];
@ -44,14 +42,22 @@ class IntegrationTest extends TestCase
$dest = \getenv('DEST'); $dest = \getenv('DEST');
$token = \getenv('TOKEN'); $token = \getenv('TOKEN');
foreach ($this->provideChats() as $chat) { foreach ($this->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']; $result = \json_decode(\file_get_contents("https://api.telegram.org/bot$token/getChat?chat_id=$chat"), true)['result']['photo'];
yield [ yield [
'profile_photo', FileIdType::PROFILE_PHOTO,
$result['small_file_id'], $result['small_file_id'],
$result['small_file_unique_id'], $result['small_file_unique_id'],
]; ];
yield [ yield [
'profile_photo', FileIdType::fromBotApiType('profile_photo'),
$result['big_file_id'], $result['big_file_id'],
$result['big_file_unique_id'], $result['big_file_unique_id'],
]; ];
@ -78,14 +84,15 @@ class IntegrationTest extends TestCase
$botResult = [$botResult]; $botResult = [$botResult];
} }
foreach ($botResult as $subResult) { foreach ($botResult as $subResult) {
/** @var string $type */
yield [ yield [
$type, FileIdType::fromBotApiType($type),
$subResult['file_id'], $subResult['file_id'],
$subResult['file_unique_id'] $subResult['file_unique_id']
]; ];
if (isset($subResult['thumb'])) { if (isset($subResult['thumb'])) {
yield [ yield [
'thumbnail', FileIdType::fromBotApiType('thumbnail'),
$subResult['thumb']['file_id'], $subResult['thumb']['file_id'],
$subResult['thumb']['file_unique_id'] $subResult['thumb']['file_unique_id']
]; ];
@ -93,6 +100,10 @@ class IntegrationTest extends TestCase
} }
} }
} }
/**
* @psalm-suppress InvalidReturnStatement, InvalidReturnType
* @return list<string>
*/
public function provideChats(): array public function provideChats(): array
{ {
return [\getenv('DEST'), '@MadelineProto']; return [\getenv('DEST'), '@MadelineProto'];