Skip to content

Testing

The client talks to Telegram Web through a small Pam\TelegramWeb\Session interface. Pass your own implementation as the second constructor argument, and you can emit bridge events, run your listeners, and assert on the calls they made, with no browser and no account.

tests/Support/FakeTelegramSession.php
<?php
declare(strict_types=1);
namespace Tests\Support;
use Pam\TelegramWeb\Session;
final class FakeTelegramSession implements Session
{
/** @var list<array{function: string, arguments: list<mixed>}> */
public array $calls = [];
/** @var array<string, mixed> canned results by function name */
public array $results = [];
private ?\Closure $onEvent = null;
public function start(\Closure $onEvent): void
{
$this->onEvent = $onEvent;
}
public function pump(float $timeout): void
{
}
public function call(string $function, array $arguments = [], float $timeout = 30.0): mixed
{
$this->calls[] = ['function' => $function, 'arguments' => $arguments];
return $this->results[$function] ?? null;
}
public function close(): void
{
}
/** @param array<string, mixed> $payload */
public function emit(string $type, array $payload = []): void
{
($this->onEvent)($type, $payload);
}
}
tests/Unit/PingTest.php
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Pam\TelegramWeb\Client;
use Pam\TelegramWeb\ClientOptions;
use Pam\TelegramWeb\Event\MessageReceived;
use PHPUnit\Framework\TestCase;
use Tests\Support\FakeTelegramSession;
final class PingTest extends TestCase
{
public function testItAnswersPingFromOtherPeople(): void
{
$session = new FakeTelegramSession();
$client = new Client(new ClientOptions(), $session);
$client->onMessage(static function (MessageReceived $event): void {
if (!$event->message->outgoing && $event->message->text === 'ping') {
$event->message->reply('pong');
}
});
$client->initialize();
$session->emit('ready', ['me' => ['id' => 7, 'first_name' => 'Alice']]);
$session->emit('message', [
'message' => ['id' => 55, 'peerId' => 42, 'fromId' => 42, 'message' => 'ping', 'date' => 1_700_000_000],
'sender' => 'Bob',
'chat' => 'Bob',
]);
self::assertTrue($client->isReady());
self::assertSame('sendText', $session->calls[0]['function']);
self::assertSame([42, 'pong', ['replyTo' => 55]], $session->calls[0]['arguments']);
}
}

The bridge event types are qr, password_required, ready, message, message_edited, messages_deleted, and logout. The function names your fake receives in call() (sendText, invoke, inputPeer, and so on) are the bridge functions behind each client method; return canned values from $results when your code reads data.

Terminal window
pam vendor/bin/phpunit

The package repository ships interactive integration scripts that log in with a real account. If you write similar checks, use a dedicated test account, send only to your own Saved Messages or to a group that contains only you, and delete what the test sent. Keep the test session directory outside the repository.