Skip to content

Commit 9203329

Browse files
finished protocol V1 test. Updated Socket disconnect.
1 parent 31e492a commit 9203329

File tree

5 files changed

+227
-66
lines changed

5 files changed

+227
-66
lines changed

Socket.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,10 @@ private function printHex(string $str, bool $write = true)
166166
*/
167167
public function disconnect()
168168
{
169-
socket_shutdown($this->socket);
170-
@socket_close($this->socket);
169+
if (is_resource($this->socket)) {
170+
@socket_shutdown($this->socket);
171+
@socket_close($this->socket);
172+
}
171173
}
172174

173175
}

tests/ATest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Bolt\tests;
4+
5+
use \PHPUnit\Framework\TestCase;
6+
7+
/**
8+
* Class TestBase
9+
* @author Michal Stefanak
10+
* @link https://github.com/stefanak-michal/Bolt
11+
* @package Bolt\tests
12+
*/
13+
abstract class ATest extends TestCase
14+
{
15+
16+
/**
17+
* @var bool
18+
*/
19+
private static $bypass = false;
20+
21+
/**
22+
* Bypass Socket final keyword before autoload
23+
*/
24+
public static function setUpBeforeClass(): void
25+
{
26+
if (!self::$bypass) {
27+
$path = __DIR__;
28+
while (!file_exists($path . DS . 'Socket.php')) {
29+
$path = dirname($path);
30+
}
31+
32+
$content = file_get_contents($path . DS . 'Socket.php');
33+
if (strpos($content, "final class Socket") !== false) {
34+
file_put_contents($path . DS . 'Socket.php', str_replace('final class Socket', 'class Socket', $content));
35+
$socket = new \Bolt\Socket('localhost', 0, 0);
36+
file_put_contents($path . DS . 'Socket.php', $content);
37+
}
38+
}
39+
40+
self::$bypass = true;
41+
}
42+
43+
}

tests/BoltTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Bolt\tests;
44

5-
use PHPUnit\Framework\TestCase;
65
use Bolt\Bolt;
76

87
/**
@@ -19,13 +18,13 @@
1918
* @requires extension sockets
2019
* @requires extension mbstring
2120
*/
22-
class BoltTest extends TestCase
21+
class BoltTest extends \Bolt\tests\ATest
2322
{
2423

2524
/**
2625
* @return Bolt|null
2726
*/
28-
public function testHello(): Bolt
27+
public function testHello(): ?Bolt
2928
{
3029
try {
3130
$bolt = new Bolt($GLOBALS['NEO_HOST'] ?? '127.0.0.1', $GLOBALS['NEO_PORT'] ?? 7687);

tests/PackStream/v1/PackerTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Bolt\tests\PackStream\v1;
44

55
use Bolt\PackStream\v1\Packer;
6-
use PHPUnit\Framework\TestCase;
6+
use Exception;
77

88
/**
99
* Class PackerTest
@@ -18,7 +18,7 @@
1818
* @requires extension mbstring
1919
* @requires extension json
2020
*/
21-
class PackerTest extends TestCase
21+
class PackerTest extends \Bolt\tests\ATest
2222
{
2323

2424
/**
@@ -37,7 +37,7 @@ public function test__construct(): Packer
3737
* @param string $bin
3838
* @param array $args
3939
* @param Packer $packer
40-
* @throws \Exception
40+
* @throws Exception
4141
*/
4242
public function testPack(string $bin, array $args, Packer $packer)
4343
{
@@ -62,7 +62,7 @@ public function packProvider(): array
6262
* @param string $bin
6363
* @param int $number
6464
* @param Packer $packer
65-
* @throws \Exception
65+
* @throws Exception
6666
*/
6767
public function testPackInteger(string $bin, int $number, Packer $packer)
6868
{
@@ -83,7 +83,7 @@ public function integerProvider(): array
8383
/**
8484
* @depends test__construct
8585
* @param Packer $packer
86-
* @throws \Exception
86+
* @throws Exception
8787
*/
8888
public function testPackFloat(Packer $packer)
8989
{
@@ -93,7 +93,7 @@ public function testPackFloat(Packer $packer)
9393
/**
9494
* @depends test__construct
9595
* @param Packer $packer
96-
* @throws \Exception
96+
* @throws Exception
9797
*/
9898
public function testPackNull(Packer $packer)
9999
{
@@ -103,7 +103,7 @@ public function testPackNull(Packer $packer)
103103
/**
104104
* @depends test__construct
105105
* @param Packer $packer
106-
* @throws \Exception
106+
* @throws Exception
107107
*/
108108
public function testPackBool(Packer $packer)
109109
{
@@ -117,7 +117,7 @@ public function testPackBool(Packer $packer)
117117
* @param string $bin
118118
* @param string $str
119119
* @param Packer $packer
120-
* @throws \Exception
120+
* @throws Exception
121121
*/
122122
public function testPackString(string $bin, string $str, Packer $packer)
123123
{
@@ -138,7 +138,7 @@ public function stringProvider(): array
138138
* @param string $bin
139139
* @param array $arr
140140
* @param Packer $packer
141-
* @throws \Exception
141+
* @throws Exception
142142
*/
143143
public function testPackArray(string $bin, array $arr, Packer $packer)
144144
{
@@ -162,9 +162,9 @@ public function arrayProvider(): array
162162
* @param string $bin
163163
* @param object $obj
164164
* @param Packer $packer
165-
* @throws \Exception
165+
* @throws Exception
166166
*/
167-
public function testPackMap(string $bin, object $obj, Packer $packer)
167+
public function testPackMap(string $bin, $obj, Packer $packer)
168168
{
169169
$this->assertEquals($bin, $this->getMethod($packer)->invoke($packer, $obj));
170170
}
@@ -184,12 +184,12 @@ public function mapProvider(): array
184184
* Test it on data type resource, which is not implemented
185185
* @depends test__construct
186186
* @param Packer $packer
187-
* @throws \Exception
187+
* @throws Exception
188188
*/
189189
public function testException(Packer $packer)
190190
{
191191
$f = fopen(__FILE__, 'r');
192-
$this->expectException(\Exception::class);
192+
$this->expectException(Exception::class);
193193
$this->getMethod($packer)->invoke($packer, $f);
194194
fclose($f);
195195
}

0 commit comments

Comments
 (0)