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
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace Ratchet\Server;
use Ratchet\Server\IoServer;
use React\EventLoop\StreamSelectLoop;
use React\Socket\Server;
/**
* @covers Ratchet\Server\IoServer
*/
class IoServerTest extends \PHPUnit_Framework_TestCase {
protected $server;
protected $app;
protected $port;
protected $reactor;
public function setUp() {
$this->app = $this->getMock('\\Ratchet\\MessageComponentInterface');
$loop = new StreamSelectLoop;
$this->reactor = new Server(0, $loop);
$uri = $this->reactor->getAddress();
$this->port = parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri, PHP_URL_PORT);
$this->server = new IoServer($this->app, $this->reactor, $loop);
}
public function testOnOpen() {
$this->app->expects($this->once())->method('onOpen')->with($this->isInstanceOf('\\Ratchet\\ConnectionInterface'));
$client = stream_socket_client("tcp://localhost:{$this->port}");
$this->server->loop->tick();
//$this->assertTrue(is_string($this->app->last['onOpen'][0]->remoteAddress));
//$this->assertTrue(is_int($this->app->last['onOpen'][0]->resourceId));
}
public function testOnData() {
$msg = 'Hello World!';
$this->app->expects($this->once())->method('onMessage')->with(
$this->isInstanceOf('\\Ratchet\\ConnectionInterface')
, $msg
);
$client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($client, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_option($client, SOL_SOCKET, SO_SNDBUF, 4096);
socket_set_block($client);
socket_connect($client, 'localhost', $this->port);
$this->server->loop->tick();
socket_write($client, $msg);
$this->server->loop->tick();
socket_shutdown($client, 1);
socket_shutdown($client, 0);
socket_close($client);
$this->server->loop->tick();
}
public function testOnClose() {
$this->app->expects($this->once())->method('onClose')->with($this->isInstanceOf('\\Ratchet\\ConnectionInterface'));
$client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($client, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_option($client, SOL_SOCKET, SO_SNDBUF, 4096);
socket_set_block($client);
socket_connect($client, 'localhost', $this->port);
$this->server->loop->tick();
socket_shutdown($client, 1);
socket_shutdown($client, 0);
socket_close($client);
$this->server->loop->tick();
}
public function testFactory() {
$this->assertInstanceOf('\\Ratchet\\Server\\IoServer', IoServer::factory($this->app, 0));
}
public function testNoLoopProvidedError() {
$this->setExpectedException('RuntimeException');
$io = new IoServer($this->app, $this->reactor);
$io->run();
}
public function testOnErrorPassesException() {
$conn = $this->getMock('\\React\\Socket\\ConnectionInterface');
$conn->decor = $this->getMock('\\Ratchet\\ConnectionInterface');
$err = new \Exception("Nope");
$this->app->expects($this->once())->method('onError')->with($conn->decor, $err);
$this->server->handleError($err, $conn);
}
public function onErrorCalledWhenExceptionThrown() {
$this->markTestIncomplete("Need to learn how to throw an exception from a mock");
$conn = $this->getMock('\\React\\Socket\\ConnectionInterface');
$this->server->handleConnect($conn);
$e = new \Exception;
$this->app->expects($this->once())->method('onMessage')->with($this->isInstanceOf('\\Ratchet\\ConnectionInterface'), 'f')->will($e);
$this->app->expects($this->once())->method('onError')->with($this->instanceOf('\\Ratchet\\ConnectionInterface', $e));
$this->server->handleData('f', $conn);
}
}