Skip to content

Commit f6b298a

Browse files
author
Alex
committed
Covered common functionality
1 parent 8daff8a commit f6b298a

File tree

2 files changed

+81
-36
lines changed

2 files changed

+81
-36
lines changed

tests/Handler.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
use Generator;
44
use Ollyxar\WebSockets\{
5+
Dispatcher,
56
Frame,
67
Handler as BaseHandler,
7-
Dispatcher,
88
Logger
99
};
1010

1111
class Handler extends BaseHandler
1212
{
13-
13+
/**
14+
* Handler constructor.
15+
* @param $server
16+
* @param $master
17+
*/
1418
public function __construct($server, $master)
1519
{
1620
parent::__construct($server, $master);
@@ -29,6 +33,10 @@ protected function onConnect($client): Generator
2933
]))));
3034
}
3135

36+
/**
37+
* @param $clientNumber
38+
* @return Generator
39+
*/
3240
protected function onClose($clientNumber): Generator
3341
{
3442
yield Dispatcher::async($this->broadcast(Frame::encode(json_encode([

tests/ServerTest.php

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
use PHPUnit\Framework\TestCase;
44
use Ollyxar\WebSockets\{
5+
Exceptions\ForkException,
6+
Exceptions\SocketException,
57
Frame,
68
Server,
7-
Exceptions\ForkException,
8-
Logger,
99
Ssl
1010
};
1111

@@ -25,7 +25,8 @@ class ServerTest extends TestCase
2525
'path' => '/tmp/cert.pem',
2626
'passPhrase' => 'qwerty123'
2727
];
28-
private $client;
28+
29+
public static $serverStarted = false;
2930

3031
/**
3132
* Starts Server
@@ -34,26 +35,13 @@ class ServerTest extends TestCase
3435
*/
3536
private function startServer(): void
3637
{
37-
Logger::enable();
38-
Server::$connector = '/tmp/ws.sock';
39-
4038
(new Server('0.0.0.0', static::PORT, 2, true))
4139
->setHandler(Handler::class)
4240
->setCert(static::CERT['path'])
4341
->setPassPhrase(static::CERT['passPhrase'])
4442
->run();
4543
}
4644

47-
/**
48-
* Stops server
49-
*
50-
* @return void
51-
*/
52-
private function stopServer(): void
53-
{
54-
posix_kill($this->serverPid, SIGINT);
55-
}
56-
5745
/**
5846
* Forking process to get properly working Server and separated test
5947
*
@@ -62,23 +50,30 @@ private function stopServer(): void
6250
*/
6351
private function forkProcess(): void
6452
{
53+
if (static::$serverStarted) {
54+
return;
55+
}
56+
6557
$pid = pcntl_fork();
6658

6759
if ($pid == -1) {
6860
throw new ForkException('Cannot fork process');
6961
} elseif ($pid) {
7062
$this->serverPid = $pid;
63+
sleep(3);
7164
} else {
7265
$this->startServer();
7366
}
67+
68+
static::$serverStarted = true;
7469
}
7570

7671
/**
7772
* Making lightweight WebSocket client
7873
*
79-
* @return void
74+
* @return resource
8075
*/
81-
private function makeClient(): void
76+
private function makeClient()
8277
{
8378
$context = stream_context_create([
8479
'ssl' => [
@@ -91,7 +86,9 @@ private function makeClient(): void
9186
]
9287
]);
9388

94-
$this->client = stream_socket_client('ssl://localhost:' . static::PORT, $errorNumber, $errorString, null, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $context);
89+
sleep(3); // prevent receiving previous messages from queue
90+
91+
return stream_socket_client('ssl://localhost:' . static::PORT, $errorNumber, $errorString, null, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $context);
9592
}
9693

9794
/**
@@ -136,41 +133,81 @@ private function generateHandshakeRequest(): string
136133
}
137134

138135
/**
136+
* ServerTest constructor.
137+
*
138+
* @param null $name
139+
* @param array $data
140+
* @param string $dataName
139141
* @throws ForkException
140142
*/
141-
public function setUp()
143+
public function __construct($name = null, array $data = [], $dataName = '')
142144
{
145+
Server::$connector = '/tmp/ws.sock';
143146
Ssl::generateCert(static::CERT['path'], static::CERT['passPhrase']);
144147
$this->forkProcess();
145-
sleep(3);
146-
$this->makeClient();
148+
149+
parent::__construct($name, $data, $dataName);
147150
}
148151

149152
/**
150-
* Free resources
153+
* Simple messaging
154+
*
155+
* @throws SocketException
151156
*/
152-
public function __destruct()
157+
public function testBasicMessaging()
153158
{
154-
//$this->stopServer(); // doesn't work on TravisCI
155-
@unlink(static::CERT['path']);
159+
$client = $this->makeClient();
160+
161+
if (!$client) {
162+
throw new SocketException('Client socket does not created properly');
163+
}
164+
165+
fwrite($client, $this->generateHandshakeRequest());
166+
$response = fread($client, 4096);
167+
$this->assertContains('101 Web Socket Protocol Handshake', $response);
168+
169+
$data = Frame::decode($client);
170+
$this->assertArrayHasKey('payload', $data);
171+
$this->assertContains('connected.', $data['payload']);
172+
173+
fwrite($client, Frame::encode('client:hello'));
174+
$data = Frame::decode($client);
175+
$this->assertArrayHasKey('payload', $data);
176+
$this->assertEquals('client:hello', $data['payload']);
177+
178+
fclose($client);
156179
}
157180

158181
/**
159-
* Simple messaging
182+
* Base connector test
183+
*
184+
* @throws SocketException
160185
*/
161-
public function testBasicMessaging()
186+
public function testConnector()
162187
{
163-
fwrite($this->client, $this->generateHandshakeRequest());
164-
$response = fread($this->client, 4096);
188+
$client = $this->makeClient();
189+
190+
if (!$client) {
191+
throw new SocketException('Client socket does not created properly');
192+
}
193+
194+
fwrite($client, $this->generateHandshakeRequest());
195+
$response = fread($client, 4096);
165196
$this->assertContains('101 Web Socket Protocol Handshake', $response);
166197

167-
$data = Frame::decode($this->client);
198+
$data = Frame::decode($client);
168199
$this->assertArrayHasKey('payload', $data);
169200
$this->assertContains('connected.', $data['payload']);
170201

171-
fwrite($this->client, Frame::encode('Hello message'));
172-
$data = Frame::decode($this->client);
202+
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
203+
socket_connect($socket, Server::$connector);
204+
socket_write($socket, Frame::encode('connector:hello'));
205+
socket_close($socket);
206+
207+
$data = Frame::decode($client);
173208
$this->assertArrayHasKey('payload', $data);
174-
$this->assertEquals('Hello message', $data['payload']);
209+
$this->assertEquals('connector:hello', $data['payload']);
210+
211+
fclose($client);
175212
}
176213
}

0 commit comments

Comments
 (0)