From 29e4d7dd1a07e85d154627cd1b46cc1a162d895c Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 20 Jan 2025 19:11:20 +0100 Subject: [PATCH] Simplify test code by extracting common code to instance variable --- .../server/WebsocketProtocolTest.class.php | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/test/php/web/unittest/server/WebsocketProtocolTest.class.php b/src/test/php/web/unittest/server/WebsocketProtocolTest.class.php index 264db46..1228192 100755 --- a/src/test/php/web/unittest/server/WebsocketProtocolTest.class.php +++ b/src/test/php/web/unittest/server/WebsocketProtocolTest.class.php @@ -1,12 +1,13 @@ out; } + #[Before] + public function noop() { + $this->noop= function($conn, $message) { + // NOOP + }; + } + #[Test] public function can_create() { new WebsocketProtocol(new class() extends Listener { @@ -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); } } \ No newline at end of file