-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathServerTest.php
104 lines (88 loc) · 3.71 KB
/
ServerTest.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
<?php
namespace Navarr\Socket\Test;
use Navarr\Socket\Server;
use PHPUnit\Framework\TestCase;
class ServerTest extends TestCase
{
public function testAddingSingleHookWorksProperly()
{
$server = new Server('127.0.0.1', 9000);
$server->start();
$exampleReturn = '__NAVARR_SOCKET_TEST_EXAMPLE_RETURN__';
$server->addHook(
Server::HOOK_CONNECT,
function () {
return '__NAVARR_SOCKET_TEST_EXAMPLE_RETURN__';
}
);
$serverClass = new \ReflectionClass($server);
$hooksProperty = $serverClass->getProperty('hooks');
$hooksProperty->setAccessible(true);
$hooks = $hooksProperty->getValue($server);
// Hooks should be a protected property
$this->assertTrue($hooksProperty->isProtected());
// Hooks should be an array
$this->assertTrue(is_array($hooks));
// Hooks should have a single callable in $hooks[Server::HOOK_CONNECT]
$this->assertEquals(1, count($hooks[Server::HOOK_CONNECT]));
// Values in $hooks[Server::HOOK_CONNECT] should be callables
$this->assertTrue(is_callable($hooks[Server::HOOK_CONNECT][0]));
// Make sure the callable in $hooks[Server::HOOK_CONNECT] is the return value we expect
$this->assertEquals($exampleReturn, call_user_func($hooks[Server::HOOK_CONNECT][0]));
return $server;
}
/**
* @param Server $server
*
* @depends testAddingSingleHookWorksProperly
*
* @return Server
*/
public function testAddingMultipleHooksWorksProperly($server)
{
$server->addHook(
Server::HOOK_CONNECT,
function () {
return '__NAVARR_SOCKET_TEST_EXAMPLE_RETURN_2__';
}
);
$serverClass = new \ReflectionClass($server);
$hooksProperty = $serverClass->getProperty('hooks');
$hooksProperty->setAccessible(true);
$hooks = $hooksProperty->getValue($server);
// Hooks should have two callables in $hooks[Server::HOOK_CONNECT]
$this->assertEquals(2, count($hooks[Server::HOOK_CONNECT]));
// Values in $hooks[Server::HOOK_CONNECT] should be callables
foreach ($hooks[Server::HOOK_CONNECT] as $callable) {
$this->assertTrue(is_callable($callable));
}
// Make sure the callable in $hooks[Server::HOOK_CONNECT] is the return value we expect
$this->assertEquals('__NAVARR_SOCKET_TEST_EXAMPLE_RETURN_2__', call_user_func($hooks[Server::HOOK_CONNECT][1]));
return $server;
}
/**
* @param Server $server
*
* @depends testAddingMultipleHooksWorksProperly
*/
public function testAddingSameHookMultipleTimesWorksProperly($server)
{
$callable = function () {
return '__NAVARR_SOCKET_TEST_EXAMPLE_RETURN_3__';
};
$server->addHook(Server::HOOK_CONNECT, $callable);
$server->addHook(Server::HOOK_CONNECT, $callable);
$serverClass = new \ReflectionClass($server);
$hooksProperty = $serverClass->getProperty('hooks');
$hooksProperty->setAccessible(true);
$hooks = $hooksProperty->getValue($server);
// Hooks should have two callables in $hooks[Server::HOOK_CONNECT]
$this->assertEquals(3, count($hooks[Server::HOOK_CONNECT]));
// Values in $hooks[Server::HOOK_CONNECT] should be callables
foreach ($hooks[Server::HOOK_CONNECT] as $callable) {
$this->assertTrue(is_callable($callable));
}
// Make sure the callable in $hooks[Server::HOOK_CONNECT] is the return value we expect
$this->assertEquals('__NAVARR_SOCKET_TEST_EXAMPLE_RETURN_3__', call_user_func($hooks[Server::HOOK_CONNECT][2]));
}
}