aymericcucherousset/telegram-bot
A simple PHP Telegram Bot library for sending messages, handling updates, and interacting with Telegram’s Bot API. Includes helpers for requests, webhooks/long polling, keyboards, and common methods to build bots quickly and cleanly.
Testing is a first-class concern in the telegram-bot-sdk. The package is designed for high testability, leveraging strict typing, value objects, and clear boundaries between domain and infrastructure.
Write fast, isolated tests for your domain logic, handlers, and value objects. Use PHPUnit as the test runner. Example:
use Aymericcucherousset\TelegramBot\Value\ChatId;
use PHPUnit\Framework\TestCase;
class ChatIdTest extends TestCase
{
public function testValueObject(): void
{
$chatId = new ChatId(123456);
$this->assertSame(123456, $chatId->toInt());
}
}
The SDK relies on PSR-18 HTTP clients. For unit tests, mock the HTTP client to avoid real API calls. Use libraries like php-http/mock-client or PHPUnit's built-in mocking:
use Aymericcucherousset\TelegramBot\Api\HttpTelegramClient;
use Psr\Http\Client\ClientInterface;
use PHPUnit\Framework\TestCase;
class HttpTelegramClientTest extends TestCase
{
public function testSendRequest(): void
{
$mock = $this->createMock(ClientInterface::class);
// Configure $mock to expect/send requests
$client = new HttpTelegramClient('TOKEN', $mock);
// ...assertions...
}
}
See the tests/ directory for real-world examples and patterns.
When testing code that uses UpdateFactory, remember that it now requires a Bot instance as the second argument:
use Aymericcucherousset\TelegramBot\Update\UpdateFactory;
use Aymericcucherousset\TelegramBot\Bot\Bot;
$bot = new Bot($client); // $client is your TelegramClientInterface implementation (can be a fake/mock)
$update = UpdateFactory::fromJson($json, $bot);
How can I help you explore Laravel packages today?