Skip to content

Commit 02f98f4

Browse files
author
Alex
committed
Added tests for Frame and Server setup
1 parent a23f639 commit 02f98f4

File tree

3 files changed

+123
-4
lines changed

3 files changed

+123
-4
lines changed

phpunit.xml

+12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,19 @@
1414
<directory>./tests</directory>
1515
</testsuite>
1616
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory>src</directory>
20+
<exclude>
21+
<directory>src/Exceptions</directory>
22+
</exclude>
23+
</whitelist>
24+
</filter>
1725
<php>
1826
<env name="APP_ENV" value="test"/>
1927
</php>
28+
<logging>
29+
<!--<log type="coverage-clover" target="tests/clover.xml"/>-->
30+
<!--<log type="coverage-html" target="tests/coverage/html"/>-->
31+
</logging>
2032
</phpunit>

tests/FrameTest.php

+75-2
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,89 @@ class FrameTest extends TestCase
77
{
88
const TEXT_FILE = 'php://memory';
99

10-
public function testTranslateFunctions()
10+
/**
11+
* Encode/Decode helper
12+
*
13+
* @param string $text
14+
* @param int $type
15+
* @return array
16+
*/
17+
private function encodeDecode(string $text, int $type = Frame::TEXT): array
1118
{
12-
$encoded = Frame::encode('simple text');
19+
$encoded = Frame::encode($text, $type);
1320
$handle = fopen(self::TEXT_FILE, 'r+');
1421
fwrite($handle, $encoded);
1522
rewind($handle);
1623
$decoded = Frame::decode($handle);
1724
fclose($handle);
1825

26+
return $decoded;
27+
}
28+
/**
29+
* Simple text encode/decode
30+
*
31+
* @return void
32+
*/
33+
public function testSimpleText(): void
34+
{
35+
$decoded = $this->encodeDecode('simple text');
36+
1937
$this->assertTrue($decoded['opcode'] === Frame::TEXT);
2038
$this->assertTrue($decoded['payload'] === 'simple text');
2139
}
40+
41+
/**
42+
* Short text encode/decode
43+
*
44+
* @return void
45+
*/
46+
public function testShortText(): void
47+
{
48+
$decoded = $this->encodeDecode('');
49+
50+
$this->assertTrue($decoded['opcode'] === Frame::TEXT);
51+
$this->assertTrue($decoded['payload'] === '');
52+
}
53+
54+
/**
55+
* Long text encode/decode
56+
*
57+
* @return void
58+
*/
59+
public function testLongText(): void
60+
{
61+
$text = 'very very long string';
62+
63+
while (strlen($text) < 8192) {
64+
$text .= $text;
65+
}
66+
67+
$decoded = $this->encodeDecode($text);
68+
69+
$this->assertTrue($decoded['opcode'] === Frame::TEXT);
70+
$this->assertTrue($decoded['payload'] === $text);
71+
}
72+
73+
/**
74+
* False positive socket
75+
*
76+
* @return void
77+
*/
78+
public function testWrongSocket(): void {
79+
$decoded = Frame::decode(false);
80+
81+
$this->assertArrayHasKey('opcode', $decoded);
82+
$this->assertEquals(Frame::CLOSE, $decoded['opcode']);
83+
}
84+
85+
/**
86+
* Close message encode/decode
87+
*
88+
* @return void
89+
*/
90+
public function testCloseSignal(): void
91+
{
92+
$decoded = $this->encodeDecode('', Frame::CLOSE);
93+
$this->assertTrue($decoded['opcode'] === Frame::CLOSE);
94+
}
2295
}

tests/ServerTest.php

+36-2
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,45 @@ public function __construct($name = null, array $data = [], $dataName = '')
149149
parent::__construct($name, $data, $dataName);
150150
}
151151

152+
/**
153+
* Server setup/destroy
154+
*
155+
* @return void
156+
*/
157+
public function testServerSetupDestroy(): void
158+
{
159+
Server::$connector = '/tmp/temp1.sock';
160+
161+
$server = (new Server('0.0.0.0', 2930, 2, true))
162+
->setHandler(Handler::class)
163+
->setCert(static::CERT['path'])
164+
->setPassPhrase(static::CERT['passPhrase']);
165+
166+
unset($server);
167+
168+
$this->assertFalse(file_exists(Server::$connector));
169+
170+
Server::$connector = '/tmp/ws.sock';
171+
}
172+
173+
/**
174+
* Making ssl certificate
175+
*
176+
* @return void
177+
*/
178+
public function testMakeSslCert(): void
179+
{
180+
Ssl::generateCert(static::CERT['path'], static::CERT['passPhrase']);
181+
$this->assertTrue(file_exists(static::CERT['path']));
182+
}
183+
152184
/**
153185
* Simple messaging
154186
*
155187
* @throws SocketException
188+
* @return void
156189
*/
157-
public function testBasicMessaging()
190+
public function testBasicMessaging(): void
158191
{
159192
$client = $this->makeClient();
160193

@@ -182,8 +215,9 @@ public function testBasicMessaging()
182215
* Base connector test
183216
*
184217
* @throws SocketException
218+
* @return void
185219
*/
186-
public function testConnector()
220+
public function testConnector(): void
187221
{
188222
$client = $this->makeClient();
189223

0 commit comments

Comments
 (0)