Skip to content

Commit

Permalink
PHP 8.2 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn-se committed Oct 27, 2022
1 parent 271831e commit e5a1ec0
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 47 deletions.
45 changes: 0 additions & 45 deletions lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,46 +61,6 @@ public function getCloseStatus(): ?int
return $this->close_status;
}

/**
* Convenience method to send text message
* @param string $payload Content as string
*/
public function text(string $payload): void
{
$message = $this->msg_factory->create('text', $payload);
$this->pushMessage($message);
}

/**
* Convenience method to send binary message
* @param string $payload Content as binary string
*/
public function binary(string $payload): void
{
$message = $this->msg_factory->create('binary', $payload);
$this->pushMessage($message);
}

/**
* Convenience method to send ping
* @param string $payload Optional text as string
*/
public function ping(string $payload = ''): void
{
$message = $this->msg_factory->create('ping', $payload);
$this->pushMessage($message);
}

/**
* Convenience method to send unsolicited pong
* @param string $payload Optional text as string
*/
public function pong(string $payload = ''): void
{
$message = $this->msg_factory->create('pong', $payload);
$this->pushMessage($message);
}

/**
* Tell the socket to close.
*
Expand Down Expand Up @@ -357,11 +317,6 @@ private function autoRespond(array $frame)

/* ---------- Stream I/O methods ------------------------------------------------- */

public function getStream()
{
return $this->isConnected() ? $this->stream : null;
}

/**
* Close connection stream.
* @return bool
Expand Down
4 changes: 3 additions & 1 deletion lib/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public function __toString(): string
// Split messages into frames
public function getFrames(bool $masked = true, int $framesize = 4096): array
{

$frames = [];
foreach (str_split($this->getContent(), $framesize) as $payload) {
$split = str_split($this->getContent(), $framesize) ?: [''];
foreach ($split as $payload) {
$frames[] = [false, $payload, 'continuation', $masked];
}
$frames[0][2] = $this->opcode;
Expand Down
11 changes: 10 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="tests/bootstrap.php"
colors="true"
convertDeprecationsToExceptions="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<coverage>
<include>
<directory suffix=".php">lib/</directory>
Expand Down
7 changes: 7 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,11 @@ public function testUnconnectedClient(): void
$this->assertNull($client->getRemoteName());
$this->assertNull($client->getCloseStatus());
}

public function testDeprecated(): void
{
$client = new Client('ws://localhost:8000/my/mock/path');
$this->expectDeprecation();
$this->assertNull($client->getPier());
}
}
33 changes: 33 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,37 @@ public function testUnconnectedServer(): void
$this->assertNull($server->getCloseStatus());
$this->assertTrue(MockSocket::isEmpty());
}

public function testFailedHandshake(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
$this->assertTrue(MockSocket::isEmpty());

MockSocket::initialize('server.accept-failed-handshake', $this);
$server->accept();
$this->expectException('WebSocket\ConnectionException');
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Could not read from stream');
$server->send('Connect');
$this->assertFalse($server->isConnected());
$this->assertTrue(MockSocket::isEmpty());
}

public function testServerDisconnect(): void
{
MockSocket::initialize('server.construct', $this);
$server = new Server();
$this->assertTrue(MockSocket::isEmpty());
MockSocket::initialize('server.accept', $this);
$server->accept();
$server->send('Connect');
$this->assertTrue($server->isConnected());
$this->assertTrue(MockSocket::isEmpty());

MockSocket::initialize('server.disconnect', $this);
$server->disconnect();
$this->assertFalse($server->isConnected());
$this->assertTrue(MockSocket::isEmpty());
}
}
32 changes: 32 additions & 0 deletions tests/scripts/server.accept-failed-handshake.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"function": "stream_socket_accept",
"params": [
"@mock-socket"
],
"return": "@mock-stream"
},
{
"function": "stream_socket_get_name",
"params": [
"@mock-stream"
],
"return": "127.0.0.1:12345"
},
{
"function": "stream_get_line",
"params": [
"@mock-stream",
1024,
"\r\n"
],
"return": false
},
{
"function": "get_resource_type",
"params": [
"@mock-stream"
],
"return": ""
}
]
24 changes: 24 additions & 0 deletions tests/scripts/server.disconnect.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[

{
"function": "get_resource_type",
"params": [
"@mock-stream"
],
"return": "stream"
},
{
"function": "fclose",
"params": [
"@mock-stream"
],
"return": true
},
{
"function": "get_resource_type",
"params": [
"@mock-stream"
],
"return": ""
}
]

0 comments on commit e5a1ec0

Please sign in to comment.