-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSocketTest.php
105 lines (82 loc) · 3.63 KB
/
SocketTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Navarr\Socket\Test;
use Navarr\Socket\Socket;
use Navarr\Socket\Exception\SocketException;
use PHPUnit\Framework\TestCase;
use Socket as SocketResource;
class SocketTest extends TestCase
{
/**
* Verifies that {@see Socket::create}} works as expected. (happy-path).
*/
public function testSocketCreate()
{
// Create the Socket
$domain = AF_INET;
$type = SOCK_STREAM;
$protocol = SOL_TCP;
$socket = Socket::create($domain, $type, $protocol);
// Create function should obviously return an instance of the class
$this->assertTrue($socket instanceof Socket);
// Lets make sure that the resource is created properly
$reflectionProperty = new \ReflectionProperty($socket, 'resource');
$reflectionProperty->setAccessible(true);
$this->assertTrue($reflectionProperty->getValue($socket) instanceof SocketResource);
$propertyDomain = new \ReflectionProperty($socket, 'domain');
$propertyDomain->setAccessible(true);
$this->assertEquals($domain, $propertyDomain->getValue($socket));
$propertyType = new \ReflectionProperty($socket, 'type');
$propertyType->setAccessible(true);
$this->assertEquals($type, $propertyType->getValue($socket));
$propertyProtocol = new \ReflectionProperty($socket, 'protocol');
$propertyProtocol->setAccessible(true);
$this->assertEquals($protocol, $propertyProtocol->getValue($socket));
}
public function testSocketWithResourceThrowsSocketException()
{
$this->expectException(SocketException::class);
$socket = Socket::create(AF_INET, SOCK_STREAM, SOL_TCP);
$socket->write('test', 4);
}
/**
* Verifies that {@see Socket::constructFromResources} works as expected.
*/
public function testSocketCanBeConstructedFromResources()
{
$resourceA = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$resourceB = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$reflectionMethod = new \ReflectionMethod(Socket::class, 'constructFromResources');
$reflectionMethod->setAccessible(true);
$func = $reflectionMethod->getClosure();
/** @var Socket[] $result */
$result = $func([$resourceA, $resourceB]);
$this->assertTrue(is_array($result));
$this->assertEquals(2, count($result));
foreach ($result as $singleResult) {
$this->assertInstanceOf(Socket::class, $singleResult);
}
$resultA = $result[0];
$resultB = $result[1];
$reflectionProperty = new \ReflectionProperty($resultA, 'resource');
$reflectionProperty->setAccessible(true);
$this->assertEquals($resourceA, $reflectionProperty->getValue($resultA));
$this->assertEquals($resourceB, $reflectionProperty->getValue($resultB));
}
/**
* Verifies that {@see Socket::mapClassToRawSocket} works as expected.
*/
public function testMapClassToRawSocket()
{
$resourceA = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$reflectionMethod = new \ReflectionMethod(Socket::class, 'constructFromResources');
$reflectionMethod->setAccessible(true);
$func = $reflectionMethod->getClosure();
$result = $func([$resourceA]);
$staticReflectionMethod = new \ReflectionMethod(Socket::class, 'mapClassToRawSocket');
$staticReflectionMethod->setAccessible(true);
$func = $staticReflectionMethod->getClosure();
$staticResult = $func($result);
$this->assertTrue(is_array($staticResult));
$this->assertEquals($resourceA, $staticResult[0]);
}
}