From fd9b0af1b442ba17f59c5a144336fb20b5337d97 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 21 Apr 2024 13:51:07 +0200 Subject: [PATCH] Entity refactoring --- docs | 2 +- .../Message/Entities/BankCard.php | 4 ++ .../Message/Entities/Blockquote.php | 4 ++ src/EventHandler/Message/Entities/Bold.php | 4 ++ .../Message/Entities/BotCommand.php | 4 ++ src/EventHandler/Message/Entities/Cashtag.php | 4 ++ src/EventHandler/Message/Entities/Code.php | 4 ++ .../Message/Entities/CustomEmoji.php | 21 ++++--- src/EventHandler/Message/Entities/Email.php | 4 ++ src/EventHandler/Message/Entities/Hashtag.php | 4 ++ .../Message/Entities/InputMentionName.php | 21 ++++--- src/EventHandler/Message/Entities/Italic.php | 4 ++ src/EventHandler/Message/Entities/Mention.php | 4 ++ .../Message/Entities/MentionName.php | 22 ++++--- .../Message/Entities/MessageEntity.php | 60 +++++++++---------- src/EventHandler/Message/Entities/Phone.php | 6 +- src/EventHandler/Message/Entities/Pre.php | 21 ++++--- src/EventHandler/Message/Entities/Spoiler.php | 4 ++ src/EventHandler/Message/Entities/Strike.php | 4 ++ src/EventHandler/Message/Entities/TextUrl.php | 21 ++++--- .../Message/Entities/Underline.php | 4 ++ src/EventHandler/Message/Entities/Url.php | 4 ++ src/TL/Conversion/BotAPI.php | 5 +- src/TL/TL.php | 4 ++ tests/danog/MadelineProto/EntitiesTest.php | 52 ++++++++++++++++ 25 files changed, 221 insertions(+), 70 deletions(-) diff --git a/docs b/docs index 422f5aa4f..c39c48b1b 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit 422f5aa4f6c2cec63663d5b82f383113b4fab80f +Subproject commit c39c48b1b164b5ea6bd95a9e6660f50b361f9ec8 diff --git a/src/EventHandler/Message/Entities/BankCard.php b/src/EventHandler/Message/Entities/BankCard.php index 4e4f2db99..6e8681aba 100644 --- a/src/EventHandler/Message/Entities/BankCard.php +++ b/src/EventHandler/Message/Entities/BankCard.php @@ -11,4 +11,8 @@ final class BankCard extends MessageEntity { return ['type' => 'bank_card', 'offset' => $this->offset, 'length' => $this->length]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityBankCard', 'offset' => $this->offset, 'length' => $this->length]; + } } diff --git a/src/EventHandler/Message/Entities/Blockquote.php b/src/EventHandler/Message/Entities/Blockquote.php index e44f698b4..6fe53fffa 100644 --- a/src/EventHandler/Message/Entities/Blockquote.php +++ b/src/EventHandler/Message/Entities/Blockquote.php @@ -11,4 +11,8 @@ final class Blockquote extends MessageEntity { return ['type' => 'block_quote', 'offset' => $this->offset, 'length' => $this->length]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityBlockquote', 'offset' => $this->offset, 'length' => $this->length]; + } } diff --git a/src/EventHandler/Message/Entities/Bold.php b/src/EventHandler/Message/Entities/Bold.php index 5a43cbfc5..6a06021fa 100644 --- a/src/EventHandler/Message/Entities/Bold.php +++ b/src/EventHandler/Message/Entities/Bold.php @@ -11,4 +11,8 @@ final class Bold extends MessageEntity { return ['type' => 'bold', 'offset' => $this->offset, 'length' => $this->length]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityBold', 'offset' => $this->offset, 'length' => $this->length]; + } } diff --git a/src/EventHandler/Message/Entities/BotCommand.php b/src/EventHandler/Message/Entities/BotCommand.php index ef5507897..05e28eb7a 100644 --- a/src/EventHandler/Message/Entities/BotCommand.php +++ b/src/EventHandler/Message/Entities/BotCommand.php @@ -11,4 +11,8 @@ final class BotCommand extends MessageEntity { return ['type' => 'bot_command', 'offset' => $this->offset, 'length' => $this->length]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityBotCommand', 'offset' => $this->offset, 'length' => $this->length]; + } } diff --git a/src/EventHandler/Message/Entities/Cashtag.php b/src/EventHandler/Message/Entities/Cashtag.php index 350fcaa91..ed2bbda9a 100644 --- a/src/EventHandler/Message/Entities/Cashtag.php +++ b/src/EventHandler/Message/Entities/Cashtag.php @@ -11,4 +11,8 @@ final class Cashtag extends MessageEntity { return ['type' => 'cashtag', 'offset' => $this->offset, 'length' => $this->length]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityCashtag', 'offset' => $this->offset, 'length' => $this->length]; + } } diff --git a/src/EventHandler/Message/Entities/Code.php b/src/EventHandler/Message/Entities/Code.php index 6055f4c5b..76107b35d 100644 --- a/src/EventHandler/Message/Entities/Code.php +++ b/src/EventHandler/Message/Entities/Code.php @@ -11,4 +11,8 @@ final class Code extends MessageEntity { return ['type' => 'code', 'offset' => $this->offset, 'length' => $this->length]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityCode', 'offset' => $this->offset, 'length' => $this->length]; + } } diff --git a/src/EventHandler/Message/Entities/CustomEmoji.php b/src/EventHandler/Message/Entities/CustomEmoji.php index 943742efa..7decbdcab 100644 --- a/src/EventHandler/Message/Entities/CustomEmoji.php +++ b/src/EventHandler/Message/Entities/CustomEmoji.php @@ -7,17 +7,24 @@ namespace danog\MadelineProto\EventHandler\Message\Entities; */ final class CustomEmoji extends MessageEntity { - /** Document ID of the [custom emoji](https://core.telegram.org/api/custom-emoji). */ - public readonly int $documentId; + public function __construct( + /** Offset of message entity within message (in UTF-16 code units) */ + public readonly int $offset, - /** @internal */ - protected function __construct(array $rawEntities) - { - parent::__construct($rawEntities); - $this->documentId = $rawEntities['document_id'] ?? $rawEntities['custom_emoji_id']; + /** Length of message entity within message (in UTF-16 code units) */ + public readonly int $length, + + /** Document ID of the [custom emoji](https://core.telegram.org/api/custom-emoji). */ + public readonly int $documentId + ) { } + public function toBotAPI(): array { return ['type' => 'custom_emoji', 'offset' => $this->offset, 'length' => $this->length, 'custom_emoji_id' => $this->documentId]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityCustomEmoji', 'offset' => $this->offset, 'length' => $this->length, 'document_id' => $this->documentId]; + } } diff --git a/src/EventHandler/Message/Entities/Email.php b/src/EventHandler/Message/Entities/Email.php index 410473cf6..6b09d986d 100644 --- a/src/EventHandler/Message/Entities/Email.php +++ b/src/EventHandler/Message/Entities/Email.php @@ -11,4 +11,8 @@ final class Email extends MessageEntity { return ['type' => 'email', 'offset' => $this->offset, 'length' => $this->length]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityEmail', 'offset' => $this->offset, 'length' => $this->length]; + } } diff --git a/src/EventHandler/Message/Entities/Hashtag.php b/src/EventHandler/Message/Entities/Hashtag.php index 5d2427e3e..0ebd26b81 100644 --- a/src/EventHandler/Message/Entities/Hashtag.php +++ b/src/EventHandler/Message/Entities/Hashtag.php @@ -11,4 +11,8 @@ final class Hashtag extends MessageEntity { return ['type' => 'hashtag', 'offset' => $this->offset, 'length' => $this->length]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityHashtag', 'offset' => $this->offset, 'length' => $this->length]; + } } diff --git a/src/EventHandler/Message/Entities/InputMentionName.php b/src/EventHandler/Message/Entities/InputMentionName.php index 597a51080..52f54a2c2 100644 --- a/src/EventHandler/Message/Entities/InputMentionName.php +++ b/src/EventHandler/Message/Entities/InputMentionName.php @@ -7,17 +7,24 @@ namespace danog\MadelineProto\EventHandler\Message\Entities; */ final class InputMentionName extends MessageEntity { - /** Identifier of the user that was mentioned */ - public readonly int|string $userId; + public function __construct( + /** Offset of message entity within message (in UTF-16 code units) */ + public readonly int $offset, - /** @internal */ - protected function __construct(array $rawEntities) - { - parent::__construct($rawEntities); - $this->userId = $rawEntities['user_id']; + /** Length of message entity within message (in UTF-16 code units) */ + public readonly int $length, + + /** Identifier of the user that was mentioned */ + public readonly int|string $userId + ) { } + public function toBotAPI(): array { return ['type' => 'text_mention', 'offset' => $this->offset, 'length' => $this->length, 'user' => ['id' => $this->userId]]; } + public function toMTProto(): array + { + return ['_' => 'inputMessageEntityMentionName', 'offset' => $this->offset, 'length' => $this->length, 'user_id' => $this->userId]; + } } diff --git a/src/EventHandler/Message/Entities/Italic.php b/src/EventHandler/Message/Entities/Italic.php index e690a3a58..7952b823c 100644 --- a/src/EventHandler/Message/Entities/Italic.php +++ b/src/EventHandler/Message/Entities/Italic.php @@ -11,4 +11,8 @@ final class Italic extends MessageEntity { return ['type' => 'italic', 'offset' => $this->offset, 'length' => $this->length]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityItalic', 'offset' => $this->offset, 'length' => $this->length]; + } } diff --git a/src/EventHandler/Message/Entities/Mention.php b/src/EventHandler/Message/Entities/Mention.php index c0e11ef2c..65a446d07 100644 --- a/src/EventHandler/Message/Entities/Mention.php +++ b/src/EventHandler/Message/Entities/Mention.php @@ -11,4 +11,8 @@ final class Mention extends MessageEntity { return ['type' => 'mention', 'offset' => $this->offset, 'length' => $this->length]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityMention', 'offset' => $this->offset, 'length' => $this->length]; + } } diff --git a/src/EventHandler/Message/Entities/MentionName.php b/src/EventHandler/Message/Entities/MentionName.php index d4f12e7b6..253111ba5 100644 --- a/src/EventHandler/Message/Entities/MentionName.php +++ b/src/EventHandler/Message/Entities/MentionName.php @@ -7,17 +7,25 @@ namespace danog\MadelineProto\EventHandler\Message\Entities; */ final class MentionName extends MessageEntity { - /** Identifier of the user that was mentioned */ - public readonly int $userId; - /** @internal */ - protected function __construct(array $rawEntities) - { - parent::__construct($rawEntities); - $this->userId = $rawEntities['user_id'] ?? $rawEntities['user']['id']; + public function __construct( + /** Offset of message entity within message (in UTF-16 code units) */ + public readonly int $offset, + + /** Length of message entity within message (in UTF-16 code units) */ + public readonly int $length, + + /** Identifier of the user that was mentioned */ + public readonly int $userId + ) { } + public function toBotAPI(): array { return ['type' => 'text_mention', 'offset' => $this->offset, 'length' => $this->length, 'user' => ['id' => $this->userId]]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityMentionName', 'offset' => $this->offset, 'length' => $this->length, 'user_id' => $this->userId]; + } } diff --git a/src/EventHandler/Message/Entities/MessageEntity.php b/src/EventHandler/Message/Entities/MessageEntity.php index 547394d0f..804128ed7 100644 --- a/src/EventHandler/Message/Entities/MessageEntity.php +++ b/src/EventHandler/Message/Entities/MessageEntity.php @@ -12,17 +12,13 @@ use ReflectionProperty; */ abstract class MessageEntity implements JsonSerializable { - /** Offset of message entity within message (in UTF-16 code units) */ - public readonly int $offset; + public function __construct( + /** Offset of message entity within message (in UTF-16 code units) */ + public readonly int $offset, - /** Length of message entity within message (in UTF-16 code units) */ - public readonly int $length; - - /** @internal */ - protected function __construct(array $rawEntities) - { - $this->offset = $rawEntities['offset']; - $this->length = $rawEntities['length']; + /** Length of message entity within message (in UTF-16 code units) */ + public readonly int $length + ) { } /** @@ -37,26 +33,26 @@ abstract class MessageEntity implements JsonSerializable public static function fromRawEntity(array $entity): self { return match ($entity['_'] ?? $entity['type']) { - 'mention', 'messageEntityMention' => new Mention($entity), - 'hashtag', 'messageEntityHashtag' => new Hashtag($entity), - 'bot_command', 'messageEntityBotCommand' => new BotCommand($entity), - 'email', 'messageEntityEmail' => new Email($entity), - 'bold', 'messageEntityBold' => new Bold($entity), - 'italic', 'messageEntityItalic' => new Italic($entity), - 'url', 'messageEntityUrl' => new Url($entity), - 'code', 'messageEntityCode' => new Code($entity), - 'pre', 'messageEntityPre' => new Pre($entity), - 'text_link', 'messageEntityTextUrl' => new TextUrl($entity), - 'text_mention', 'messageEntityMentionName' => new MentionName($entity), - 'inputMessageEntityMentionName' => new InputMentionName($entity), - 'phone_number', 'messageEntityPhone' => new Phone($entity), - 'cashtag', 'messageEntityCashtag' => new Cashtag($entity), - 'underline', 'messageEntityUnderline' => new Underline($entity), - 'strikethrough', 'messageEntityStrike' => new Strike($entity), - 'block_quote', 'messageEntityBlockquote' => new Blockquote($entity), - 'messageEntityBankCard' => new BankCard($entity), - 'spoiler', 'messageEntitySpoiler' => new Spoiler($entity), - 'custom_emoji', 'messageEntityCustomEmoji' => new CustomEmoji($entity), + 'mention', 'messageEntityMention' => new Mention($entity['offset'], $entity['length']), + 'hashtag', 'messageEntityHashtag' => new Hashtag($entity['offset'], $entity['length']), + 'bot_command', 'messageEntityBotCommand' => new BotCommand($entity['offset'], $entity['length']), + 'email', 'messageEntityEmail' => new Email($entity['offset'], $entity['length']), + 'bold', 'messageEntityBold' => new Bold($entity['offset'], $entity['length']), + 'italic', 'messageEntityItalic' => new Italic($entity['offset'], $entity['length']), + 'url', 'messageEntityUrl' => new Url($entity['offset'], $entity['length']), + 'code', 'messageEntityCode' => new Code($entity['offset'], $entity['length']), + 'pre', 'messageEntityPre' => new Pre($entity['offset'], $entity['length'], $entity['language']), + 'text_link', 'messageEntityTextUrl' => new TextUrl($entity['offset'], $entity['length'], $entity['url']), + 'text_mention', 'messageEntityMentionName' => new MentionName($entity['offset'], $entity['length'], $entity['user_id'] ?? $entity['user']['id']), + 'inputMessageEntityMentionName' => new InputMentionName($entity['offset'], $entity['length'], $entity['user_id']), + 'phone_number', 'messageEntityPhone' => new Phone($entity['offset'], $entity['length']), + 'cashtag', 'messageEntityCashtag' => new Cashtag($entity['offset'], $entity['length']), + 'underline', 'messageEntityUnderline' => new Underline($entity['offset'], $entity['length']), + 'strikethrough', 'messageEntityStrike' => new Strike($entity['offset'], $entity['length']), + 'block_quote', 'messageEntityBlockquote' => new Blockquote($entity['offset'], $entity['length']), + 'bank_card', 'messageEntityBankCard' => new BankCard($entity['offset'], $entity['length']), + 'spoiler', 'messageEntitySpoiler' => new Spoiler($entity['offset'], $entity['length']), + 'custom_emoji', 'messageEntityCustomEmoji' => new CustomEmoji($entity['offset'], $entity['length'], $entity['document_id'] ?? $entity['custom_emoji_id']), default => throw new AssertionError("Unknown entity type: ".($entity['_'] ?? $entity['type'])) }; } @@ -65,6 +61,10 @@ abstract class MessageEntity implements JsonSerializable * Convert entity to bot API entity. */ abstract public function toBotAPI(): array; + /** + * Convert entity to MTProto entity. + */ + abstract public function toMTProto(): array; /** @internal */ public function jsonSerialize(): mixed diff --git a/src/EventHandler/Message/Entities/Phone.php b/src/EventHandler/Message/Entities/Phone.php index 8db0a8c46..02acdaf85 100644 --- a/src/EventHandler/Message/Entities/Phone.php +++ b/src/EventHandler/Message/Entities/Phone.php @@ -9,6 +9,10 @@ final class Phone extends MessageEntity { public function toBotAPI(): array { - return ['type' => 'phone', 'offset' => $this->offset, 'length' => $this->length]; + return ['type' => 'phone_number', 'offset' => $this->offset, 'length' => $this->length]; + } + public function toMTProto(): array + { + return ['_' => 'messageEntityPhone', 'offset' => $this->offset, 'length' => $this->length]; } } diff --git a/src/EventHandler/Message/Entities/Pre.php b/src/EventHandler/Message/Entities/Pre.php index 242fb0c03..50c687b3d 100644 --- a/src/EventHandler/Message/Entities/Pre.php +++ b/src/EventHandler/Message/Entities/Pre.php @@ -7,15 +7,18 @@ namespace danog\MadelineProto\EventHandler\Message\Entities; */ final class Pre extends MessageEntity { - /** Programming language of the code */ - public readonly string $language; + public function __construct( + /** Offset of message entity within message (in UTF-16 code units) */ + public readonly int $offset, - /** @internal */ - protected function __construct(array $rawEntities) - { - parent::__construct($rawEntities); - $this->language = $rawEntities['language']; + /** Length of message entity within message (in UTF-16 code units) */ + public readonly int $length, + + /** Programming language of the code */ + public readonly string $language + ) { } + public function toBotAPI(): array { $res = ['type' => 'pre', 'offset' => $this->offset, 'length' => $this->length]; @@ -24,4 +27,8 @@ final class Pre extends MessageEntity } return $res; } + public function toMTProto(): array + { + return ['_' => 'messageEntityPre', 'offset' => $this->offset, 'length' => $this->length, 'language' => $this->language]; + } } diff --git a/src/EventHandler/Message/Entities/Spoiler.php b/src/EventHandler/Message/Entities/Spoiler.php index aaf958e7c..5f0801197 100644 --- a/src/EventHandler/Message/Entities/Spoiler.php +++ b/src/EventHandler/Message/Entities/Spoiler.php @@ -11,4 +11,8 @@ final class Spoiler extends MessageEntity { return ['type' => 'spoiler', 'offset' => $this->offset, 'length' => $this->length]; } + public function toMTProto(): array + { + return ['_' => 'messageEntitySpoiler', 'offset' => $this->offset, 'length' => $this->length]; + } } diff --git a/src/EventHandler/Message/Entities/Strike.php b/src/EventHandler/Message/Entities/Strike.php index 27df5359c..b49da7413 100644 --- a/src/EventHandler/Message/Entities/Strike.php +++ b/src/EventHandler/Message/Entities/Strike.php @@ -11,4 +11,8 @@ final class Strike extends MessageEntity { return ['type' => 'strikethrough', 'offset' => $this->offset, 'length' => $this->length]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityStrike', 'offset' => $this->offset, 'length' => $this->length]; + } } diff --git a/src/EventHandler/Message/Entities/TextUrl.php b/src/EventHandler/Message/Entities/TextUrl.php index 14b77187c..75865bc12 100644 --- a/src/EventHandler/Message/Entities/TextUrl.php +++ b/src/EventHandler/Message/Entities/TextUrl.php @@ -7,17 +7,24 @@ namespace danog\MadelineProto\EventHandler\Message\Entities; */ final class TextUrl extends MessageEntity { - /** The actual URL */ - public readonly string $url; + public function __construct( + /** Offset of message entity within message (in UTF-16 code units) */ + public readonly int $offset, - /** @internal */ - protected function __construct(array $rawEntities) - { - parent::__construct($rawEntities); - $this->url = $rawEntities['url']; + /** Length of message entity within message (in UTF-16 code units) */ + public readonly int $length, + + /** The actual URL */ + public readonly string $url + ) { } + public function toBotAPI(): array { return ['type' => 'text_link', 'offset' => $this->offset, 'length' => $this->length, 'url' => $this->url]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityTextUrl', 'offset' => $this->offset, 'length' => $this->length, 'url' => $this->url]; + } } diff --git a/src/EventHandler/Message/Entities/Underline.php b/src/EventHandler/Message/Entities/Underline.php index 833e392d3..8f439f403 100644 --- a/src/EventHandler/Message/Entities/Underline.php +++ b/src/EventHandler/Message/Entities/Underline.php @@ -11,4 +11,8 @@ final class Underline extends MessageEntity { return ['type' => 'underline', 'offset' => $this->offset, 'length' => $this->length]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityUnderline', 'offset' => $this->offset, 'length' => $this->length]; + } } diff --git a/src/EventHandler/Message/Entities/Url.php b/src/EventHandler/Message/Entities/Url.php index 24d21b12a..b7e7223fa 100644 --- a/src/EventHandler/Message/Entities/Url.php +++ b/src/EventHandler/Message/Entities/Url.php @@ -11,4 +11,8 @@ final class Url extends MessageEntity { return ['type' => 'url', 'offset' => $this->offset, 'length' => $this->length]; } + public function toMTProto(): array + { + return ['_' => 'messageEntityUrl', 'offset' => $this->offset, 'length' => $this->length]; + } } diff --git a/src/TL/Conversion/BotAPI.php b/src/TL/Conversion/BotAPI.php index 5e1fdd3f2..3f53a548c 100644 --- a/src/TL/Conversion/BotAPI.php +++ b/src/TL/Conversion/BotAPI.php @@ -27,7 +27,6 @@ use danog\MadelineProto\Logger; use danog\MadelineProto\MTProto; use danog\MadelineProto\StrTools; use danog\MadelineProto\Tools; -use danog\TelegramEntities\Entities; use Throwable; /** @@ -407,12 +406,12 @@ trait BotAPI $arguments['parse_mode'] = str_replace('textParseMode', '', $arguments['parse_mode']['_']); } if (stripos($arguments['parse_mode'], 'markdown') !== false) { - $entities = Entities::fromMarkdown($arguments[$key]); + $entities = StrTools::markdownToMessageEntities($arguments[$key]); $arguments[$key] = $entities->message; $arguments['entities'] = array_merge($arguments['entities'] ?? [], $entities->entities); unset($arguments['parse_mode']); } elseif (stripos($arguments['parse_mode'], 'html') !== false) { - $entities = Entities::fromHtml($arguments[$key]); + $entities = StrTools::htmlToMessageEntities($arguments[$key]); $arguments[$key] = $entities->message; $arguments['entities'] = array_merge($arguments['entities'] ?? [], $entities->entities); unset($arguments['parse_mode']); diff --git a/src/TL/TL.php b/src/TL/TL.php index 4a3249853..fb7a46457 100644 --- a/src/TL/TL.php +++ b/src/TL/TL.php @@ -20,6 +20,7 @@ declare(strict_types=1); namespace danog\MadelineProto\TL; +use danog\MadelineProto\EventHandler\Message\Entities\MessageEntity; use danog\MadelineProto\Lang; use danog\MadelineProto\Logger; use danog\MadelineProto\Magic; @@ -579,6 +580,9 @@ final class TL implements TLInterface return $object; } } + if ($object instanceof MessageEntity) { + $object = $object->toMTProto(); + } if ($type['type'] === 'InputMessage' && !\is_array($object)) { $object = ['_' => 'inputMessageID', 'id' => $object]; } elseif (isset($this->typeMismatch[$type['type']]) && (!\is_array($object) || isset($object['_']) && $this->constructors->findByPredicate($object['_'])['type'] !== $type['type'])) { diff --git a/tests/danog/MadelineProto/EntitiesTest.php b/tests/danog/MadelineProto/EntitiesTest.php index d8773c48e..6b56a70fb 100644 --- a/tests/danog/MadelineProto/EntitiesTest.php +++ b/tests/danog/MadelineProto/EntitiesTest.php @@ -4,6 +4,26 @@ declare(strict_types=1); namespace danog\MadelineProto\Test; +use danog\MadelineProto\EventHandler\Message\Entities\BankCard; +use danog\MadelineProto\EventHandler\Message\Entities\Blockquote; +use danog\MadelineProto\EventHandler\Message\Entities\Bold; +use danog\MadelineProto\EventHandler\Message\Entities\BotCommand; +use danog\MadelineProto\EventHandler\Message\Entities\Cashtag; +use danog\MadelineProto\EventHandler\Message\Entities\Code; +use danog\MadelineProto\EventHandler\Message\Entities\CustomEmoji; +use danog\MadelineProto\EventHandler\Message\Entities\Email; +use danog\MadelineProto\EventHandler\Message\Entities\Hashtag; +use danog\MadelineProto\EventHandler\Message\Entities\Italic; +use danog\MadelineProto\EventHandler\Message\Entities\Mention; +use danog\MadelineProto\EventHandler\Message\Entities\MentionName; +use danog\MadelineProto\EventHandler\Message\Entities\MessageEntity; +use danog\MadelineProto\EventHandler\Message\Entities\Phone; +use danog\MadelineProto\EventHandler\Message\Entities\Pre; +use danog\MadelineProto\EventHandler\Message\Entities\Spoiler; +use danog\MadelineProto\EventHandler\Message\Entities\Strike; +use danog\MadelineProto\EventHandler\Message\Entities\TextUrl; +use danog\MadelineProto\EventHandler\Message\Entities\Underline; +use danog\MadelineProto\EventHandler\Message\Entities\Url; use danog\MadelineProto\StrTools; use danog\MadelineProto\TextEntities; use danog\MadelineProto\Tools; @@ -29,6 +49,38 @@ class EntitiesTest extends TestCase $this->assertEquals(['aπŸ‘', 'aπŸ‘'], StrTools::mbStrSplit('aπŸ‘aπŸ‘', 3)); $this->assertEquals(['πŸ‡ΊπŸ‡¦', 'πŸ‡ΊπŸ‡¦'], StrTools::mbStrSplit('πŸ‡ΊπŸ‡¦πŸ‡ΊπŸ‡¦', 4)); } + /** + * @dataProvider provideEntityObjects + */ + public function testAllEntities(MessageEntity $message): void + { + $this->assertTrue(MessageEntity::fromRawEntity($message->toBotAPI()) == $message); + $this->assertTrue(MessageEntity::fromRawEntity($message->toMTProto()) == $message); + } + public static function provideEntityObjects(): array + { + return [ + [new BankCard(1, 2)], + [new Blockquote(1, 2)], + [new Bold(1, 2)], + [new BotCommand(1, 2)], + [new Cashtag(1, 2)], + [new Code(1, 2)], + [new CustomEmoji(1, 2, 12345)], + [new Email(1, 2)], + [new Hashtag(1, 2)], + [new Italic(1, 2)], + [new MentionName(1, 2, 12345)], + [new Mention(1, 2)], + [new Phone(1, 2)], + [new Pre(1, 2, "language")], + [new Spoiler(1, 2)], + [new Strike(1, 2)], + [new TextUrl(1, 2, "https://google.com")], + [new Underline(1, 2)], + [new Url(1, 2)], + ]; + } private static function sendMessage(string $message, string $parse_mode): TextEntities { return match ($parse_mode) {