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.
A fake session
Section titled “A fake session”<?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, '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( 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 { }}Writing a test
Section titled “Writing a test”Register your real listeners on a client backed by the fake, then drive it:
<?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);
self::assertSame('pong', $session->sent[0]['body']); self::assertNotNull($session->sent[0]['quoted']); }}pam vendor/bin/phpunitMethods 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.
Live certification
Section titled “Live certification”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.