Skip to content

Commit

Permalink
Add getUserChatBoosts method and UserChatBoosts type (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Jun 28, 2024
1 parent 33a4fea commit e5a8675
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
- New #67: Add `editGeneralForumTopic`, `closeGeneralForumTopic`, `reopenGeneralForumTopic`, `hideGeneralForumTopic`
and `unhideGeneralForumTopic` methods.
- New #68: Add `answerCallbackQuery` method.
- New #69: Add `getUserChatBoosts` method and `UserChatBoosts` type.
- Chg #24: Move update methods to `Vjik\TelegramBot\Api\Method\Update` namespace, and update types to
`Vjik\TelegramBot\Api\Type\Update` namespace.
- Chg #30: Remove `TelegramRequestWithFilesInterface`.
Expand Down
44 changes: 44 additions & 0 deletions src/Method/GetUserChatBoosts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method;

use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Type\UserChatBoosts;

/**
* @see https://core.telegram.org/bots/api#getuserchatboosts
*/
final readonly class GetUserChatBoosts implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int|string $chatId,
private int $userId,
) {
}

public function getHttpMethod(): HttpMethod
{
return HttpMethod::GET;
}

public function getApiMethod(): string
{
return 'getUserChatBoosts';
}

public function getData(): array
{
return [
'chat_id' => $this->chatId,
'user_id' => $this->userId,
];
}

public function prepareResult(mixed $result): UserChatBoosts
{
return UserChatBoosts::fromTelegramResult($result);
}
}
13 changes: 13 additions & 0 deletions src/ParseResult/ValueHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DateTimeImmutable;
use Vjik\TelegramBot\Api\Type\BusinessOpeningHoursInterval;
use Vjik\TelegramBot\Api\Type\ChatBoost;
use Vjik\TelegramBot\Api\Type\InlineKeyboardButton;
use Vjik\TelegramBot\Api\Type\MessageEntity;
use Vjik\TelegramBot\Api\Type\Passport\EncryptedPassportElement;
Expand Down Expand Up @@ -454,6 +455,18 @@ public static function getArrayOfStickers(array $result, ?string $key = null): a
);
}

/**
* @return ChatBoost[]
*/
public static function getArrayOfChatBoosts(array $result, ?string $key = null): array
{
$stickers = $key === null ? $result : ValueHelper::getArray($result, $key);
return array_map(
static fn($item) => ChatBoost::fromTelegramResult($item),
$stickers
);
}

/**
* @return PassportFile[]|null
*/
Expand Down
14 changes: 14 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Vjik\TelegramBot\Api\Method\GetMyDescription;
use Vjik\TelegramBot\Api\Method\GetMyName;
use Vjik\TelegramBot\Api\Method\GetMyShortDescription;
use Vjik\TelegramBot\Api\Method\GetUserChatBoosts;
use Vjik\TelegramBot\Api\Method\GetUserProfilePhotos;
use Vjik\TelegramBot\Api\Method\HideGeneralForumTopic;
use Vjik\TelegramBot\Api\Method\LeaveChat;
Expand Down Expand Up @@ -141,6 +142,7 @@
use Vjik\TelegramBot\Api\Type\Sticker\Sticker;
use Vjik\TelegramBot\Api\Type\Sticker\StickerSet;
use Vjik\TelegramBot\Api\Type\User;
use Vjik\TelegramBot\Api\Type\UserChatBoosts;
use Vjik\TelegramBot\Api\Type\UserProfilePhotos;
use Vjik\TelegramBot\Api\Method\Update\DeleteWebhook;
use Vjik\TelegramBot\Api\Method\Update\GetUpdates;
Expand Down Expand Up @@ -787,6 +789,18 @@ public function getUpdates(
return $this->send(new GetUpdates($offset, $limit, $timeout, $allowedUpdates));
}

/**
* @see https://core.telegram.org/bots/api#getuserchatboosts
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function getUserChatBoosts(int|string $chatId, int $userId): FailResult|UserChatBoosts
{
return $this->send(
new GetUserChatBoosts($chatId, $userId)
);
}

/**
* @see https://core.telegram.org/bots/api#getuserprofilephotos
*
Expand Down
29 changes: 29 additions & 0 deletions src/Type/UserChatBoosts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Type;

use Vjik\TelegramBot\Api\ParseResult\ValueHelper;

/**
* @see https://core.telegram.org/bots/api#userchatboosts
*/
final readonly class UserChatBoosts
{
/**
* @param ChatBoost[] $boosts
*/
public function __construct(
public array $boosts,
) {
}

public static function fromTelegramResult(mixed $result): self
{
ValueHelper::assertArrayResult($result);
return new self(
ValueHelper::getArrayOfChatBoosts($result, 'boosts'),
);
}
}
38 changes: 38 additions & 0 deletions tests/Method/GetUserChatBoostsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method;

use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\GetUserChatBoosts;
use Vjik\TelegramBot\Api\Request\HttpMethod;

final class GetUserChatBoostsTest extends TestCase
{
public function testBase(): void
{
$method = new GetUserChatBoosts(1, 2);

$this->assertSame(HttpMethod::GET, $method->getHttpMethod());
$this->assertSame('getUserChatBoosts', $method->getApiMethod());
$this->assertSame(
[
'chat_id' => 1,
'user_id' => 2,
],
$method->getData()
);
}

public function testPrepareResult(): void
{
$method = new GetUserChatBoosts(1, 2);

$preparedResult = $method->prepareResult([
'boosts' => [],
]);

$this->assertSame([], $preparedResult->boosts);
}
}
78 changes: 78 additions & 0 deletions tests/ParseResult/ValueHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Vjik\TelegramBot\Api\ParseResult\TelegramParseResultException;
use Vjik\TelegramBot\Api\ParseResult\ValueHelper;
use Vjik\TelegramBot\Api\Type\BusinessOpeningHoursInterval;
use Vjik\TelegramBot\Api\Type\ChatBoost;
use Vjik\TelegramBot\Api\Type\ChatBoostSourcePremium;
use Vjik\TelegramBot\Api\Type\InlineKeyboardButton;
use Vjik\TelegramBot\Api\Type\MessageEntity;
use Vjik\TelegramBot\Api\Type\Passport\EncryptedPassportElement;
Expand Down Expand Up @@ -1150,6 +1152,82 @@ public function testGetArrayOfStickers(): void
);
}

public function testGetArrayOfChatBoosts(): void
{
$result = [
'key' => [
[
'boost_id' => 'b1',
'add_date' => 1619040000,
'expiration_date' => 1619040001,
'source' => [
'source' => 'premium',
'user' => [
'id' => 1,
'is_bot' => false,
'first_name' => 'Sergei',
],
],
],
],
'array-of-ints' => [1, 2],
'number' => 7,
];

$this->assertEquals(
[
new ChatBoost(
'b1',
new DateTimeImmutable('@1619040000'),
new DateTimeImmutable('@1619040001'),
new ChatBoostSourcePremium(new User(1, false, 'Sergei')),
)
],
ValueHelper::getArrayOfChatBoosts($result, 'key'),
);
$this->assertEquals(
[
new ChatBoost(
'b1',
new DateTimeImmutable('@1619040000'),
new DateTimeImmutable('@1619040001'),
new ChatBoostSourcePremium(new User(1, false, 'Sergei')),
)
],
ValueHelper::getArrayOfChatBoosts($result['key']),
);

$exception = null;
try {
ValueHelper::getArrayOfChatBoosts($result, 'unknown');
} catch (Throwable $exception) {
}
$this->assertInstanceOf(NotFoundKeyInResultException::class, $exception);
$this->assertSame('Not found key "unknown" in result object.', $exception->getMessage());

$exception = null;
try {
ValueHelper::getArrayOfChatBoosts($result, 'number');
} catch (Throwable $exception) {
}
$this->assertInstanceOf(InvalidTypeOfValueInResultException::class, $exception);
$this->assertSame(
'Invalid type of value for key "number". Expected type is "array", but got "int".',
$exception->getMessage()
);

$exception = null;
try {
ValueHelper::getArrayOfChatBoosts($result, 'array-of-ints');
} catch (Throwable $exception) {
}
$this->assertInstanceOf(TelegramParseResultException::class, $exception);
$this->assertSame(
'Expected result as array. Got "int".',
$exception->getMessage()
);
}

public function testGetArrayOfPassportFilesOrNull(): void
{
$result = [
Expand Down
15 changes: 15 additions & 0 deletions tests/TelegramBotApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Vjik\TelegramBot\Api\Type\Sticker\InputSticker;
use Vjik\TelegramBot\Api\Type\Sticker\Sticker;
use Vjik\TelegramBot\Api\Type\User;
use Vjik\TelegramBot\Api\Type\UserChatBoosts;
use Vjik\TelegramBot\Api\Type\UserProfilePhotos;
use Vjik\TelegramBot\Api\Type\Update\Update;
use Vjik\TelegramBot\Api\Type\Update\WebhookInfo;
Expand Down Expand Up @@ -788,6 +789,20 @@ public function testGetUpdates(): void
$this->assertSame(2, $result[1]->updateId);
}

public function testGetUserChatBoosts(): void
{
$api = $this->createApi([
'ok' => true,
'result' => [
'boosts' => [],
],
]);

$result = $api->getUserChatBoosts(1, 2);

$this->assertInstanceOf(UserChatBoosts::class, $result);
}

public function testGetUserProfilePhotos(): void
{
$api = $this->createApi([
Expand Down
52 changes: 52 additions & 0 deletions tests/Type/UserChatBoostsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Type;

use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Type\ChatBoost;
use Vjik\TelegramBot\Api\Type\ChatBoostSourcePremium;
use Vjik\TelegramBot\Api\Type\User;
use Vjik\TelegramBot\Api\Type\UserChatBoosts;

final class UserChatBoostsTest extends TestCase
{
public function testBase(): void
{
$chatBoost = new ChatBoost(
'b1',
new DateTimeImmutable(),
new DateTimeImmutable(),
new ChatBoostSourcePremium(new User(1, false, 'Sergei')),
);
$type = new UserChatBoosts([$chatBoost]);

$this->assertSame([$chatBoost], $type->boosts);
}

public function testFromTelegramResult(): void
{
$type = UserChatBoosts::fromTelegramResult([
'boosts' => [
[
'boost_id' => 'b1',
'add_date' => 1619040000,
'expiration_date' => 1619040001,
'source' => [
'source' => 'premium',
'user' => [
'id' => 1,
'is_bot' => false,
'first_name' => 'Sergei',
],
],
],
],
]);

$this->assertCount(1, $type->boosts);
$this->assertSame('b1', $type->boosts[0]->boostId);
}
}

0 comments on commit e5a8675

Please sign in to comment.