Skip to content

Testing

You should never need a real WhatsApp account to test your application logic. The Client talks to WhatsApp Web through the Pam\WhatsApp\Contract\Session interface, and Client::forSession() accepts any implementation. A small fake session lets you emit events, run your listeners, and assert on what they sent.

tests/Support/FakeWhatsAppSession.php
<?php
declare(strict_types=1);
namespace Tests\Support;
use Pam\WhatsApp\Bridge\BridgeEvent;
use Pam\WhatsApp\Contract\MessageData;
use Pam\WhatsApp\Contract\Session;
use Pam\WhatsApp\EventType;
use Pam\WhatsApp\MessageContentType;
use Pam\WhatsApp\MessageType;
final class FakeWhatsAppSession implements Session
{
/** @var list<array{chatId: string, body: string, quoted: ?string}> */
public array $sent = [];
private ?\Closure $listener = null;
public function initialize(callable $listener): void
{
$this->listener = $listener(...);
$listener(new BridgeEvent(EventType::Ready, ['timestamp' => 1_700_000_000]));
}
public function receive(string $from, string $body): void
{
($this->listener)(new BridgeEvent(EventType::MessageReceived, [
'id' => 'incoming-'.count($this->sent),
'chatId' => $from,
'from' => $from,
'to' => '[email protected]',
'body' => $body,
'fromMe' => false,
'timestamp' => 1_700_000_001,
'type' => MessageType::Text->value,
'contentType' => MessageContentType::Text->value,
]));
}
public function sendText(string $chatId, string $body, ?string $quotedMessageId = null): MessageData
{
$this->sent[] = ['chatId' => $chatId, 'body' => $body, 'quoted' => $quotedMessageId];
return new MessageData(
'sent-'.count($this->sent), $chatId, '[email protected]', $chatId, $body,
true, 1_700_000_002, MessageType::Text, MessageContentType::Text,
);
}
public function sendContent(string $chatId, array $content, array $options = []): MessageData
{
$quoted = is_string($options['quotedMessageId'] ?? null) ? $options['quotedMessageId'] : null;
return $this->sendText($chatId, (string) ($content['text'] ?? ''), $quoted);
}
public function invoke(string $method, array $arguments = []): mixed
{
return null; // return canned results per $method when your code needs them
}
public function pump(float $timeoutSeconds): bool
{
return false;
}
public function logout(): void
{
}
public function close(): void
{
}
}

Register your real listeners on a client backed by the fake, then drive it:

tests/Unit/PingCommandTest.php
<?php
declare(strict_types=1);
namespace Tests\Unit;
use Pam\WhatsApp\Client;
use Pam\WhatsApp\ClientState;
use Pam\WhatsApp\Event\MessageReceived;
use PHPUnit\Framework\TestCase;
use Tests\Support\FakeWhatsAppSession;
final class PingCommandTest extends TestCase
{
public function testItAnswersPing(): void
{
$session = new FakeWhatsAppSession();
$client = Client::forSession($session);
$client->onMessage(static function (MessageReceived $event): void {
if ($event->message->body === '!ping') {
$event->message->reply('pong');
}
});
$client->initialize();
self::assertSame(ClientState::Ready, $client->state);
$session->receive('[email protected]', '!ping');
self::assertSame('[email protected]', $session->sent[0]['chatId']);
self::assertSame('pong', $session->sent[0]['body']);
self::assertNotNull($session->sent[0]['quoted']);
}
}
Terminal window
pam vendor/bin/phpunit

Methods that read data, such as getChats() or getNumberId(), call invoke() with the bridge method name. Return the payloads your test needs from invoke(), keyed by $method.

The package itself is certified against a real account with an authenticated suite that is read-only unless mutations are explicitly enabled. If you run similar end-to-end checks:

  • use a dedicated test account and a dedicated chat or group you control, never a real customer conversation;
  • keep the test session directory outside the repository;
  • never print QR payloads in CI logs.

The certification procedure, environment variables, and mutation guards are described in the package’s CERTIFICATION.md.