Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alies-dev committed May 14, 2024
1 parent aede9f1 commit 2a53886
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
executionOrder="depends,defects"
beStrictAboutOutputDuringTests="true"
beStrictAboutCoverageMetadata="true"
displayDetailsOnTestsThatTriggerWarnings="true"
failOnRisky="true"
failOnWarning="true"
cacheDirectory=".phpunit.cache"
Expand Down
48 changes: 48 additions & 0 deletions tests/DialogSerializationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace KootLabs\TelegramBotDialogs\Tests;

use KootLabs\TelegramBotDialogs\Dialogs\HelloExampleDialog;
use KootLabs\TelegramBotDialogs\Tests\Fakes\FakeBot;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use Telegram\Bot\Objects\Update;

#[CoversClass(\KootLabs\TelegramBotDialogs\Dialog::class)]
final class DialogSerializationTest extends TestCase
{
use FakeBot;

private const RANDOM_CHAT_ID = 42;

#[Test]
public function it_can_be_serialized_and_unserialized_on_the_first_step(): void
{
$bot = $this->createBotWithQueuedResponse();
$dialog = new HelloExampleDialog(self::RANDOM_CHAT_ID, $bot);

$unserializedDialog = unserialize(serialize($dialog));

$this->assertSame($dialog->getChatId(), $unserializedDialog->getChatId());
$this->assertSame($dialog->getUserId(), $unserializedDialog->getUserId());
$this->assertSame($dialog->ttl(), $unserializedDialog->ttl());
$this->assertSame($dialog->isStart(), $unserializedDialog->isStart());
}

#[Test]
public function it_can_be_serialized_and_unserialized_on_step_after_first(): void
{
$bot = $this->createBotWithQueuedResponse();
$dialog = new HelloExampleDialog(self::RANDOM_CHAT_ID, $bot);
$dialog->proceed(new Update([]));

$unserializedDialog = unserialize(serialize($dialog));

$this->assertSame($dialog->getChatId(), $unserializedDialog->getChatId());
$this->assertSame($dialog->getUserId(), $unserializedDialog->getUserId());
$this->assertSame($dialog->ttl(), $unserializedDialog->ttl());
$this->assertSame($dialog->isStart(), $unserializedDialog->isStart());
}
}
21 changes: 21 additions & 0 deletions tests/Fakes/FakeBot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace KootLabs\TelegramBotDialogs\Tests\Fakes;

use Telegram\Bot\Api;

trait FakeBot
{
use FakeHttp;

public function createBotWithQueuedResponse(array $data = [], int $statusCode = 200, array $headers = []): Api
{
return new Api(
'fake',
false,
$this->getGuzzleHttpClient([$this->makeFakeServerResponse($data, $statusCode, $headers)])
);
}
}
49 changes: 49 additions & 0 deletions tests/Fakes/FakeHttp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace KootLabs\TelegramBotDialogs\Tests\Fakes;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use Telegram\Bot\HttpClients\GuzzleHttpClient;

trait FakeHttp
{
/**
* This collection contains a history of all requests and responses
* sent using the client.
*/
protected array $history = [];

public function getGuzzleHttpClient(array $responsesToQueue = []): GuzzleHttpClient
{
$client = $this->createClientWithQueuedResponse($responsesToQueue);

return new GuzzleHttpClient($client);
}

protected function createClientWithQueuedResponse(array $responsesToQueue): Client
{
$this->history = [];
$handler = HandlerStack::create(new MockHandler($responsesToQueue));
$handler->push(Middleware::history($this->history));

return new Client(['handler' => $handler]);
}

public function makeFakeServerResponse(array $data, int $statusCode = 200, array $headers = []): Response
{
return new Response(
$statusCode,
$headers,
json_encode([
'ok' => true,
'result' => $data,
], \JSON_THROW_ON_ERROR)
);
}
}

0 comments on commit 2a53886

Please sign in to comment.