Skip to content

Commit

Permalink
Simplify test code by extracting common code to instance variable
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Jan 20, 2025
1 parent 7e1d2fa commit 29e4d7d
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/test/php/web/unittest/server/WebsocketProtocolTest.class.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php namespace web\unittest\server;

use test\{Assert, Test};
use test\{Assert, Before, Test};
use util\Bytes;
use web\unittest\Channel;
use websocket\Listener;
use xp\web\srv\WebsocketProtocol;

class WebsocketProtocolTest {
private $noop;

/**
* Handles incoming payload with a given listener
Expand All @@ -29,6 +30,13 @@ private function handle($chunks, $listener) {
return $socket->out;
}

#[Before]
public function noop() {
$this->noop= function($conn, $message) {
// NOOP
};
}

#[Test]
public function can_create() {
new WebsocketProtocol(new class() extends Listener {
Expand Down Expand Up @@ -68,37 +76,25 @@ public function send_messages() {

#[Test]
public function answers_ping_with_pong_automatically() {
$out= $this->handle(["\x89\x04", "Test"], function($conn, $message) {
// NOOP
});

$out= $this->handle(["\x89\x04", "Test"], $this->noop);
Assert::equals(["\x8a\x04Test"], $out);
}

#[Test]
public function default_close() {
$out= $this->handle(["\x88\x00"], function($conn, $message) {
// NOOP
});

$out= $this->handle(["\x88\x00"], $this->noop);
Assert::equals(["\x88\x02\x03\xe8"], $out);
}

#[Test]
public function answer_with_client_code_and_reason() {
$out= $this->handle(["\x88\x06", "\x03\xe8Test"], function($conn, $message) {
// NOOP
});

$out= $this->handle(["\x88\x06", "\x03\xe8Test"], $this->noop);
Assert::equals(["\x88\x06\x03\xe8Test"], $out);
}

#[Test]
public function protocol_error() {
$out= $this->handle(["\x88\x02", "\x03\xf7"], function($conn, $message) {
// NOOP
});

$out= $this->handle(["\x88\x02", "\x03\xf7"], $this->noop);
Assert::equals(["\x88\x02\x03\xea"], $out);
}
}

0 comments on commit 29e4d7d

Please sign in to comment.