diff --git a/src/Client.php b/src/Client.php index 422621b..89c245d 100644 --- a/src/Client.php +++ b/src/Client.php @@ -302,6 +302,10 @@ protected function configure(array $options): void */ public function waitForData(float $maxSeconds): ?bool { + if (!$this->isConnected()) { + return null; + } + return $this->socket->waitForData($maxSeconds); } } diff --git a/src/Socket/AbstractSocket.php b/src/Socket/AbstractSocket.php index b58b922..8904321 100644 --- a/src/Socket/AbstractSocket.php +++ b/src/Socket/AbstractSocket.php @@ -233,6 +233,10 @@ public function send(string $data): ?int */ public function waitForData(float $maxSeconds): ?bool { + if (null === $this->socket) { + return null; + } + $read = [$this->socket]; $write = null; $except = null; diff --git a/tests/ClientTest.php b/tests/ClientTest.php index fe95f8f..d153bf4 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -89,6 +89,7 @@ public function testSend(): void $instance->addRequestHeader('X-Test', 'Custom Request Header'); self::assertNull($instance->receive(), 'Receive before connect'); + self::assertNull($instance->waitForData(0), 'Wait for data before connect'); $success = $instance->connect(); self::assertTrue($success, 'Client can connect to test server'); @@ -123,6 +124,7 @@ public function testSend(): void $instance->disconnect(); self::assertFalse($instance->isConnected()); + self::assertNull($instance->waitForData(0), 'Wait for data after disconnect'); } finally { $helper->tearDown(); } diff --git a/tests/Socket/ClientSocketTest.php b/tests/Socket/ClientSocketTest.php index f631a2e..3c1170d 100644 --- a/tests/Socket/ClientSocketTest.php +++ b/tests/Socket/ClientSocketTest.php @@ -110,6 +110,13 @@ public function testSendTooEarly(): void $instance->send('foo'); } + public function testWaitForDataTooEarly(): void + { + $instance = self::getInstance('ws://localhost:8000'); + + self::assertNull($instance->waitForData(0)); + } + /** * Test the connect, send, receive method. */