Skip to content

Commit

Permalink
Add setGameScore method (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Jun 29, 2024
1 parent b7250aa commit 0f072c7
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
- New #84: Add `refundStarPayment` method.
- New #85: Add `setPassportDataErrors` method and `PassportElementError*` types.
- New #86: Add `sendGame` method.
- New #87: Add `setGameScore` 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
59 changes: 59 additions & 0 deletions src/Method/Game/SetGameScore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Method\Game;

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

/**
* @see https://core.telegram.org/bots/api#setgamescore
*/
final readonly class SetGameScore implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int $userId,
private int $score,
private ?bool $force = null,
private ?bool $disableEditMessage = null,
private ?int $chatId = null,
private ?int $messageId = null,
private ?string $inlineMessageId = null,
) {
}

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

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

public function getData(): array
{
return array_filter(
[
'user_id' => $this->userId,
'score' => $this->score,
'force' => $this->force,
'disable_edit_message' => $this->disableEditMessage,
'chat_id' => $this->chatId,
'message_id' => $this->messageId,
'inline_message_id' => $this->inlineMessageId,
],
static fn(mixed $value): bool => $value !== null,
);
}

public function prepareResult(mixed $result): Message|true
{
return $result === true
? $result
: Message::fromTelegramResult($result);
}
}
28 changes: 28 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Vjik\TelegramBot\Api\Method\ForwardMessage;
use Vjik\TelegramBot\Api\Method\ForwardMessages;
use Vjik\TelegramBot\Api\Method\Game\SendGame;
use Vjik\TelegramBot\Api\Method\Game\SetGameScore;
use Vjik\TelegramBot\Api\Method\GetBusinessConnection;
use Vjik\TelegramBot\Api\Method\GetChat;
use Vjik\TelegramBot\Api\Method\GetChatAdministrators;
Expand Down Expand Up @@ -2202,6 +2203,33 @@ public function setCustomEmojiStickerSetThumbnail(string $name, ?string $customE
return $this->send(new SetCustomEmojiStickerSetThumbnail($name, $customEmojiId));
}

/**
* @see https://core.telegram.org/bots/api#setgamescore
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function setGameScore(
int $userId,
int $score,
?bool $force = null,
?bool $disableEditMessage = null,
?int $chatId = null,
?int $messageId = null,
?string $inlineMessageId = null,
): FailResult|Message|true {
return $this->send(
new SetGameScore(
$userId,
$score,
$force,
$disableEditMessage,
$chatId,
$messageId,
$inlineMessageId
)
);
}

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

declare(strict_types=1);

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

use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\Game\SetGameScore;
use Vjik\TelegramBot\Api\Request\HttpMethod;
use Vjik\TelegramBot\Api\Type\Message;

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

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('setGameScore', $method->getApiMethod());
$this->assertSame(
[
'user_id' => 1,
'score' => 2,
],
$method->getData(),
);
}

public function testFull(): void
{
$method = new SetGameScore(
1,
2,
true,
false,
10,
20,
'id1',
);

$this->assertSame(
[
'user_id' => 1,
'score' => 2,
'force' => true,
'disable_edit_message' => false,
'chat_id' => 10,
'message_id' => 20,
'inline_message_id' => 'id1',
],
$method->getData()
);
}

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

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

$preparedResult = $method->prepareResult([
'message_id' => 7,
'date' => 1620000000,
'chat' => [
'id' => 1,
'type' => 'private',
],
]);
$this->assertInstanceOf(Message::class, $preparedResult);
$this->assertSame(7, $preparedResult->messageId);
}
}
2 changes: 1 addition & 1 deletion tests/Method/UpdatingMessage/EditMessageMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testBase(): void
);
}

public function testFull1(): void
public function testFull(): void
{
$file = new InputFile((new StreamFactory())->createStream());
$media = new InputMediaPhoto($file);
Expand Down
12 changes: 12 additions & 0 deletions tests/TelegramBotApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,18 @@ public function testSetCustomEmojiStickerSetThumbnail(): void
$this->assertTrue($result);
}

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

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

$this->assertTrue($result);
}

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

0 comments on commit 0f072c7

Please sign in to comment.