| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
namespace Ratchet\Server;
|
|
|
3 |
use Ratchet\Server\IoServer;
|
|
|
4 |
use React\EventLoop\StreamSelectLoop;
|
|
|
5 |
use React\EventLoop\LoopInterface;
|
|
|
6 |
use React\Socket\Server;
|
|
|
7 |
|
|
|
8 |
/**
|
|
|
9 |
* @covers Ratchet\Server\IoServer
|
|
|
10 |
*/
|
|
|
11 |
class IoServerTest extends \PHPUnit_Framework_TestCase {
|
|
|
12 |
protected $server;
|
|
|
13 |
|
|
|
14 |
protected $app;
|
|
|
15 |
|
|
|
16 |
protected $port;
|
|
|
17 |
|
|
|
18 |
protected $reactor;
|
|
|
19 |
|
|
|
20 |
protected function tickLoop(LoopInterface $loop) {
|
|
|
21 |
$loop->futureTick(function () use ($loop) {
|
|
|
22 |
$loop->stop();
|
|
|
23 |
});
|
|
|
24 |
|
|
|
25 |
$loop->run();
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
public function setUp() {
|
|
|
29 |
$this->app = $this->getMock('\\Ratchet\\MessageComponentInterface');
|
|
|
30 |
|
|
|
31 |
$loop = new StreamSelectLoop;
|
|
|
32 |
$this->reactor = new Server(0, $loop);
|
|
|
33 |
|
|
|
34 |
$uri = $this->reactor->getAddress();
|
|
|
35 |
$this->port = parse_url((strpos($uri, '://') === false ? 'tcp://' : '') . $uri, PHP_URL_PORT);
|
|
|
36 |
$this->server = new IoServer($this->app, $this->reactor, $loop);
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
public function testOnOpen() {
|
|
|
40 |
$this->app->expects($this->once())->method('onOpen')->with($this->isInstanceOf('\\Ratchet\\ConnectionInterface'));
|
|
|
41 |
|
|
|
42 |
$client = stream_socket_client("tcp://localhost:{$this->port}");
|
|
|
43 |
|
|
|
44 |
$this->tickLoop($this->server->loop);
|
|
|
45 |
|
|
|
46 |
//$this->assertTrue(is_string($this->app->last['onOpen'][0]->remoteAddress));
|
|
|
47 |
//$this->assertTrue(is_int($this->app->last['onOpen'][0]->resourceId));
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
public function testOnData() {
|
|
|
51 |
$msg = 'Hello World!';
|
|
|
52 |
|
|
|
53 |
$this->app->expects($this->once())->method('onMessage')->with(
|
|
|
54 |
$this->isInstanceOf('\\Ratchet\\ConnectionInterface')
|
|
|
55 |
, $msg
|
|
|
56 |
);
|
|
|
57 |
|
|
|
58 |
$client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
|
|
59 |
socket_set_option($client, SOL_SOCKET, SO_REUSEADDR, 1);
|
|
|
60 |
socket_set_option($client, SOL_SOCKET, SO_SNDBUF, 4096);
|
|
|
61 |
socket_set_block($client);
|
|
|
62 |
socket_connect($client, 'localhost', $this->port);
|
|
|
63 |
|
|
|
64 |
$this->tickLoop($this->server->loop);
|
|
|
65 |
|
|
|
66 |
socket_write($client, $msg);
|
|
|
67 |
$this->tickLoop($this->server->loop);
|
|
|
68 |
|
|
|
69 |
socket_shutdown($client, 1);
|
|
|
70 |
socket_shutdown($client, 0);
|
|
|
71 |
socket_close($client);
|
|
|
72 |
|
|
|
73 |
$this->tickLoop($this->server->loop);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public function testOnClose() {
|
|
|
77 |
$this->app->expects($this->once())->method('onClose')->with($this->isInstanceOf('\\Ratchet\\ConnectionInterface'));
|
|
|
78 |
|
|
|
79 |
$client = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
|
|
80 |
socket_set_option($client, SOL_SOCKET, SO_REUSEADDR, 1);
|
|
|
81 |
socket_set_option($client, SOL_SOCKET, SO_SNDBUF, 4096);
|
|
|
82 |
socket_set_block($client);
|
|
|
83 |
socket_connect($client, 'localhost', $this->port);
|
|
|
84 |
|
|
|
85 |
$this->tickLoop($this->server->loop);
|
|
|
86 |
|
|
|
87 |
socket_shutdown($client, 1);
|
|
|
88 |
socket_shutdown($client, 0);
|
|
|
89 |
socket_close($client);
|
|
|
90 |
|
|
|
91 |
$this->tickLoop($this->server->loop);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
public function testFactory() {
|
|
|
95 |
$this->assertInstanceOf('\\Ratchet\\Server\\IoServer', IoServer::factory($this->app, 0));
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
public function testNoLoopProvidedError() {
|
|
|
99 |
$this->setExpectedException('RuntimeException');
|
|
|
100 |
|
|
|
101 |
$io = new IoServer($this->app, $this->reactor);
|
|
|
102 |
$io->run();
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
public function testOnErrorPassesException() {
|
|
|
106 |
$conn = $this->getMock('\\React\\Socket\\ConnectionInterface');
|
|
|
107 |
$conn->decor = $this->getMock('\\Ratchet\\ConnectionInterface');
|
|
|
108 |
$err = new \Exception("Nope");
|
|
|
109 |
|
|
|
110 |
$this->app->expects($this->once())->method('onError')->with($conn->decor, $err);
|
|
|
111 |
|
|
|
112 |
$this->server->handleError($err, $conn);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
public function onErrorCalledWhenExceptionThrown() {
|
|
|
116 |
$this->markTestIncomplete("Need to learn how to throw an exception from a mock");
|
|
|
117 |
|
|
|
118 |
$conn = $this->getMock('\\React\\Socket\\ConnectionInterface');
|
|
|
119 |
$this->server->handleConnect($conn);
|
|
|
120 |
|
|
|
121 |
$e = new \Exception;
|
|
|
122 |
$this->app->expects($this->once())->method('onMessage')->with($this->isInstanceOf('\\Ratchet\\ConnectionInterface'), 'f')->will($e);
|
|
|
123 |
$this->app->expects($this->once())->method('onError')->with($this->instanceOf('\\Ratchet\\ConnectionInterface', $e));
|
|
|
124 |
|
|
|
125 |
$this->server->handleData('f', $conn);
|
|
|
126 |
}
|
|
|
127 |
}
|