Testing & Fakes โ
Testing Telegram bots often poses challenges: calling real Telegram API servers during automated tests is slow, rate-limited, and may inadvertently message real users.
tueen/telegram ships with built-in testing fakes inspired by modern testing paradigms. With Telegram::fake(), you can intercept all outbound API calls, stub custom responses, and assert that expected API methods were called with precise parameters.
๐งช 1. Quick Example โ
use PHPUnit\Framework\TestCase;
use Tueen\Telegram\Telegram;
use Tueen\Telegram\Types\Update;
class WelcomeBotTest extends TestCase
{
public function test_it_sends_welcome_message_on_start(): void
{
// 1. Swap real client with in-memory recording fake
$bot = Telegram::fake();
// 2. Simulate an incoming update
$update = new Update([
'update_id' => 1,
'message' => [
'message_id' => 10,
'chat' => ['id' => 12345, 'type' => 'private'],
'text' => '/start',
],
]);
// 3. Run handler or controller
$bot->onCommand('start', function (Update $update, Telegram $bot) {
$bot->sendMessage(
chatId: $update->findChat()->id,
text: 'Welcome!'
);
});
$bot->router->dispatch($update, $bot);
// 4. Assert that sendMessage was called with expected parameters
$bot->assertSent('sendMessage', function (array $params) {
return $params['chat_id'] === 12345 && $params['text'] === 'Welcome!';
});
$bot->assertSentCount('sendMessage', 1);
}
}๐ ๏ธ 2. Response Stubbing โ
By default, Telegram::fake() provides a generic success response (['ok' => true, 'result' => []]) for any method called.
You can customize response stubs to test how your application handles specific Telegram responses or errors:
Stubbing Successful Responses โ
$bot = Telegram::fake();
// Stub a specific method response:
$bot->fakeResponse('getMe', [
'ok' => true,
'result' => [
'id' => 999999,
'is_bot' => true,
'first_name' => 'TestBot',
'username' => 'test_bot',
],
]);
$user = $bot->getMe();
$this->assertSame('test_bot', $user->username);Stubbing Telegram API Errors โ
Test how your system responds when Telegram returns an error (e.g. user blocked the bot):
$bot = Telegram::fake();
$bot->fakeError('sendMessage', 'Forbidden: bot was blocked by the user', 403);
// In ErrorHandlingMode::EXCEPTION:
$this->expectException(\Tueen\Telegram\Exceptions\TelegramException::class);
$bot->sendMessage(chatId: 123, text: 'Hello');๐ 3. Inspecting Sent Requests โ
You can inspect the recorded requests directly:
$requests = $bot->sentRequests(); // list of recorded Request objects
$lastRequest = $bot->lastSentRequest();
echo $lastRequest->method; // e.g. "sendMessage"
print_r($lastRequest->params);๐งญ 4. Testing & Fakes API Catalog โ
Below is the complete reference of assertion methods, stubbing helpers, and inspection APIs available on Tueen\Telegram\Testing\TelegramFake.
๐ก๏ธ Assertion Methods (TelegramFake) โ
assertSent(string $method, ?callable $callback = null): voidassertNotSent(string $method, ?callable $callback = null): voidassertSentCount(string $method, int $expectedCount): voidassertNothingSent(): void๐ญ Stubbing & Inspection Methods (TelegramFake) โ
Telegram::fake(array $responses = [], string $botToken = 'FAKE_BOT_TOKEN'): TelegramFakefakeResponse(string $method, array $response): staticfakeError(string $method, string $description, int $errorCode = 400): staticsentRequests(): array<int, Request>lastSentRequest(): ?Request