Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setStickerSetThumbnail method #61

Merged
merged 6 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- New #58: Add `setStickerPositionInSet`, `setStickerEmojiList`, `setStickerKeywords` and `setStickerMaskPosition`
methods.
- New #60: Add `setStickerSetTitle` method.
- New #61: Add `setStickerSetThumbnail` method.
- 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
53 changes: 53 additions & 0 deletions src/Method/Sticker/SetStickerSetThumbnail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method\Sticker;

use Vjik\TelegramBot\Api\ParseResult\ValueHelper;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface;
use Vjik\TelegramBot\Api\Type\InputFile;

/**
* @see https://core.telegram.org/bots/api#setstickersetthumbnail
*/
final readonly class SetStickerSetThumbnail implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private string $name,
private int $userId,
private string $format,
private InputFile|string|null $thumbnail = null,
) {
}

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

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

public function getData(): array
{
return array_filter(
[
'name' => $this->name,
'user_id' => $this->userId,
'thumbnail' => $this->thumbnail,
'format' => $this->format,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): true
{
ValueHelper::assertTrueResult($result);
return $result;
}
}
22 changes: 22 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
use Vjik\TelegramBot\Api\Method\Sticker\SetStickerKeywords;
use Vjik\TelegramBot\Api\Method\Sticker\SetStickerMaskPosition;
use Vjik\TelegramBot\Api\Method\Sticker\SetStickerPositionInSet;
use Vjik\TelegramBot\Api\Method\Sticker\SetStickerSetThumbnail;
use Vjik\TelegramBot\Api\Method\Sticker\SetStickerSetTitle;
use Vjik\TelegramBot\Api\Method\Sticker\UploadStickerFile;
use Vjik\TelegramBot\Api\Method\UnbanChatMember;
Expand Down Expand Up @@ -1647,6 +1648,27 @@ public function setStickerPositionInSet(string $sticker, int $position): FailRes
return $this->send(new SetStickerPositionInSet($sticker, $position));
}

/**
* @see https://core.telegram.org/bots/api#setstickersetthumbnail
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function setStickerSetThumbnail(
string $name,
int $userId,
string $format,
InputFile|string|null $thumbnail = null,
): FailResult|true {
return $this->send(
new SetStickerSetThumbnail(
$name,
$userId,
$format,
$thumbnail
)
);
}

/**
* @see https://core.telegram.org/bots/api#setstickersettitle
*
Expand Down
57 changes: 57 additions & 0 deletions tests/Method/Sticker/SetStickerSetThumbnailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method\Sticker;

use HttpSoft\Message\StreamFactory;
use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\Sticker\SetStickerSetThumbnail;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Type\InputFile;

final class SetStickerSetThumbnailTest extends TestCase
{
public function testBase(): void
{
$method = new SetStickerSetThumbnail('animals_by_my_bot', 123, 'static');

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('setStickerSetThumbnail', $method->getApiMethod());
$this->assertSame(
[
'name' => 'animals_by_my_bot',
'user_id' => 123,
'format' => 'static',
],
$method->getData(),
);
}

public function testFull(): void
{
$file = new InputFile((new StreamFactory())->createStream());
$method = new SetStickerSetThumbnail('animals_by_my_bot', 123, 'static', $file);

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('setStickerSetThumbnail', $method->getApiMethod());
$this->assertSame(
[
'name' => 'animals_by_my_bot',
'user_id' => 123,
'thumbnail' => $file,
'format' => 'static',
],
$method->getData(),
);
}

public function testPrepareResult(): void
{
$method = new SetStickerSetThumbnail('animals_by_my_bot', 123, 'static');

$preparedResult = $method->prepareResult(true);

$this->assertTrue($preparedResult);
}
}
12 changes: 12 additions & 0 deletions tests/TelegramBotApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,18 @@ public function testSetStickerPositionInSet(): void
$this->assertTrue($result);
}

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

$result = $api->setStickerSetThumbnail('animals_by_boy', 123, 'static');

$this->assertTrue($result);
}

public function testSetStickerSetTitle(): void
{
$api = $this->createApi([
Expand Down
Loading