Core\Test::sentSocket
Core\Test::sentSocket(): array<Core\Socket\Message>Core\Test::sentSocket() returns every message your program sent over a fake WebSocket. You create the fake service with Core\Test::answerSocket. The list is in the order the messages were sent, and the first message comes first. Each item is a Core\Socket\Message.
For a text message, text() returns the text and bytes() returns null. For a binary message, it is the other way round. The list contains the messages of every fake socket in the test, and reading it does not clear it. Before your program sends anything, the list is empty.
The examples below show how to read the sent messages, how to count them, and a test of a chat client that joins a room.
Return value
Returns array<Core\Socket\Message>. One Core\Socket\Message for each frame, in the order the program sent them. text is set for a send, and bytes for a sendBytes. When two sockets are open at once, their frames are mixed in the order they were written. To check them apart, a test uses one peer at a time.
Examples
Read what was sent
<?nvs// Shows how to read the messages your program sent over a fake WebSocket.
use Core\Http\Client;use Core\Socket\Message;use Core\Test;
Test::answerSocket('wss://chat.example.com/room', ['welcome']);
var $socket = Client::openSocket('wss://chat.example.com/room');$socket->send('hello');$socket->sendBytes('OK' as bytes);
// The first message is text, and the second one is binary.// This prints `text hello` and `bytes 2`.foreach (Test::sentSocket() as Message $message) { if ($message->text() != null) { echo 'text ', $message->text() ?? '', "\n"; } else { echo 'bytes ', Core\Bytes::length($message->bytes() ?? ('' as tainted bytes)), "\n"; }}text hellobytes 2Count the messages
<?nvs// Shows that the list starts empty and grows with each message your program sends.
use Core\Http\Client;use Core\Test;
Test::answerSocket('wss://feed.example.com/*', []);
// Nothing was sent yet, so this prints `0`.echo Core\Arr::count(Test::sentSocket()), "\n";
// Two sockets send one message each. The list contains the messages of both.var $first = Client::openSocket('wss://feed.example.com/news');var $second = Client::openSocket('wss://feed.example.com/sport');$first->send('follow news');$second->send('follow sport');
// This prints `2`, and then `2` again, because reading the list does not clear it.echo Core\Arr::count(Test::sentSocket()), "\n";echo Core\Arr::count(Test::sentSocket()), "\n";022A chat client that joins a room
<?nvs// Shows how to test that a chat client sends the right messages when it joins a room.
use Core\Http\Client;use Core\Socket\Message;use Core\Test;
class ChatClient{ // Joins a room, says hello, and closes the connection. public static function greet(string $room, string $name): void { var $socket = Client::openSocket('wss://chat.example.com/' . $room); $socket->send('join ' . $room); $socket->send('hello from ' . $name); $socket->close(); }}
Test::answerSocket('wss://chat.example.com/*', []);ChatClient::greet('support', 'Ada');
// The test collects the text of each message and compares the whole list.// `text()` returns a `tainted string`, so the list has that type too.array<tainted string> $texts = [];foreach (Test::sentSocket() as Message $message) { $texts[] = $message->text() ?? '(binary)';}Test::assertSame(['join support', 'hello from Ada'], $texts);
// This prints `the client joined and said hello`.echo "the client joined and said hello\n";the client joined and said hello