Skip to content

Commit fe0c9dd

Browse files
committed
Coverage
1 parent 93d433d commit fe0c9dd

File tree

6 files changed

+32
-87
lines changed

6 files changed

+32
-87
lines changed

.github/workflows/acceptance.yml

-15
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,6 @@ jobs:
4848
- name: Test
4949
run: make test
5050

51-
test-8-1:
52-
runs-on: ubuntu-latest
53-
name: Test PHP 8.1
54-
steps:
55-
- name: Checkout
56-
uses: actions/checkout@v2
57-
- name: Set up PHP 8.1
58-
uses: shivammathur/setup-php@v2
59-
with:
60-
php-version: '8.1'
61-
- name: Composer
62-
run: make install
63-
- name: Test
64-
run: make test
65-
6651
cs-check:
6752
runs-on: ubuntu-latest
6853
name: Code standard

lib/Client.php

-65
Original file line numberDiff line numberDiff line change
@@ -71,71 +71,6 @@ public function __toString(): string
7171
}
7272

7373

74-
/* ---------- Client operations -------------------------------------------------- */
75-
76-
/**
77-
* Set client to listen to incoming requests.
78-
* @param Closure $callback A callback function that will be called when client receives message.
79-
* function (Message $message, Connection $connection = null)
80-
* If callback function returns non-null value, the listener will halt and return that value.
81-
* Otherwise it will continue listening and propagating messages.
82-
* @return mixed Returns any non-null value returned by callback function.
83-
*/
84-
public function listen(Closure $callback)
85-
{
86-
$this->listen = true;
87-
while ($this->listen) {
88-
// Connect
89-
if (!$this->isConnected()) {
90-
$this->connect();
91-
}
92-
93-
// Handle incoming
94-
$read = $this->connection->getStream();
95-
$write = [];
96-
$except = [];
97-
if (stream_select($read, $write, $except, 0)) {
98-
foreach ($read as $stream) {
99-
try {
100-
$result = null;
101-
$peer = stream_socket_get_name($stream, true);
102-
if (empty($peer)) {
103-
$this->logger->warning("[client] Got detached stream '{$peer}'");
104-
continue;
105-
}
106-
$this->logger->debug("[client] Handling {$peer}");
107-
$message = $this->connection->pullMessage();
108-
if (!$this->connection->isConnected()) {
109-
$this->connection = null;
110-
}
111-
// Trigger callback according to filter
112-
$opcode = $message->getOpcode();
113-
if (in_array($opcode, $this->options['filter'])) {
114-
$this->last_opcode = $opcode;
115-
$result = $callback($message, $this->connection);
116-
}
117-
// If callback returns not null, exit loop and return that value
118-
if (!is_null($result)) {
119-
return $result;
120-
}
121-
} catch (Throwable $e) {
122-
$this->logger->error("[client] Error occured on {$peer}; {$e->getMessage()}");
123-
}
124-
}
125-
}
126-
}
127-
}
128-
129-
/**
130-
* Tell client to stop listening to incoming requests.
131-
* Active connections are still available when restarting listening.
132-
*/
133-
public function stop(): void
134-
{
135-
$this->listen = false;
136-
}
137-
138-
13974
/* ---------- Client option functions -------------------------------------------- */
14075

14176
/**

lib/Server.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
class Server implements LoggerAwareInterface
1818
{
19-
use LoggerAwareTrait; // provides setLogger(LoggerInterface $logger)
19+
use LoggerAwareTrait; // Provides setLogger(LoggerInterface $logger)
2020
use OpcodeTrait;
2121

2222
// Default options
@@ -29,10 +29,10 @@ class Server implements LoggerAwareInterface
2929
'timeout' => null,
3030
];
3131

32-
private $port;
33-
private $listening;
34-
private $request;
35-
private $request_path;
32+
protected $port;
33+
protected $listening;
34+
protected $request;
35+
protected $request_path;
3636
private $connections = [];
3737
private $options = [];
3838
private $listen = false;

tests/ClientTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,16 @@ public function testConvenicanceMethods(): void
475475
$this->assertEquals('127.0.0.1:8000', $client->getPier());
476476
$this->assertEquals('WebSocket\Client(127.0.0.1:12345)', "{$client}");
477477
}
478+
479+
public function testUnconnectedClient(): void
480+
{
481+
$client = new Client('ws://localhost:8000/my/mock/path');
482+
$this->assertFalse($client->isConnected());
483+
$client->setTimeout(30);
484+
$client->close();
485+
$this->assertFalse($client->isConnected());
486+
$this->assertNull($client->getName());
487+
$this->assertNull($client->getPeer());
488+
$this->assertNull($client->getCloseStatus());
489+
}
478490
}

tests/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ make test
1414

1515
## Continuous integration
1616

17-
[Travis](https://travis-ci.org/Textalk/websocket-php) is run on PHP versions
18-
`5.4`, `5.5`, `5.6`, `7.0`, `7.1`, `7.2`, `7.3` and `7.4`.
17+
GitHub Actions are run on PHP versions 7.3, 7.4, 8.0 and 8.1.
1918

2019
Code coverage by [Coveralls](https://coveralls.io/github/Textalk/websocket-php).
2120

tests/ServerTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,18 @@ public function testConvenicanceMethods(): void
445445
$this->assertEquals('WebSocket\Server(127.0.0.1:12345)', "{$server}");
446446
$this->assertTrue(MockSocket::isEmpty());
447447
}
448+
449+
public function testUnconnectedServer(): void
450+
{
451+
MockSocket::initialize('server.construct', $this);
452+
$server = new Server();
453+
$this->assertFalse($server->isConnected());
454+
$server->setTimeout(30);
455+
$server->close();
456+
$this->assertFalse($server->isConnected());
457+
$this->assertNull($server->getName());
458+
$this->assertNull($server->getPeer());
459+
$this->assertNull($server->getCloseStatus());
460+
$this->assertTrue(MockSocket::isEmpty());
461+
}
448462
}

0 commit comments

Comments
 (0)