forked from zarincheg/telegram-bot-dialogs
-
Notifications
You must be signed in to change notification settings - Fork 6
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
4 changed files
with
119 additions
and
0 deletions.
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,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()); | ||
} | ||
} |
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,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)]) | ||
); | ||
} | ||
} |
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,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) | ||
); | ||
} | ||
} |