-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters