| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace React\Tests\Socket;
|
|
|
4 |
|
|
|
5 |
use React\Socket\Connection;
|
|
|
6 |
use React\Socket\Server;
|
|
|
7 |
use React\EventLoop\StreamSelectLoop;
|
|
|
8 |
|
|
|
9 |
class ConnectionTest extends TestCase
|
|
|
10 |
{
|
|
|
11 |
/**
|
|
|
12 |
* @covers React\Socket\Connection::getRemoteAddress
|
|
|
13 |
*/
|
|
|
14 |
public function testGetRemoteAddress()
|
|
|
15 |
{
|
|
|
16 |
$loop = new StreamSelectLoop();
|
|
|
17 |
$server = new Server($loop);
|
|
|
18 |
$server->listen(0);
|
|
|
19 |
|
|
|
20 |
$class = new \ReflectionClass('React\\Socket\\Server');
|
|
|
21 |
$master = $class->getProperty('master');
|
|
|
22 |
$master->setAccessible(true);
|
|
|
23 |
|
|
|
24 |
$client = stream_socket_client('tcp://localhost:' . $server->getPort());
|
|
|
25 |
|
|
|
26 |
$class = new \ReflectionClass('React\\Socket\\Connection');
|
|
|
27 |
$method = $class->getMethod('parseAddress');
|
|
|
28 |
$method->setAccessible(true);
|
|
|
29 |
|
|
|
30 |
$servConn = new Connection($server->master, $loop);
|
|
|
31 |
|
|
|
32 |
$mock = $this->createCallableMock();
|
|
|
33 |
$mock
|
|
|
34 |
->expects($this->once())
|
|
|
35 |
->method('__invoke')
|
|
|
36 |
->with($method->invokeArgs($servConn, array(stream_socket_get_name($master->getValue($server), false))))
|
|
|
37 |
;
|
|
|
38 |
|
|
|
39 |
$server->on('connection', function ($conn) use ($mock) {
|
|
|
40 |
$mock($conn->getRemoteAddress());
|
|
|
41 |
});
|
|
|
42 |
$loop->tick();
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public function remoteAddressProvider()
|
|
|
46 |
{
|
|
|
47 |
return array(
|
|
|
48 |
array('192.168.1.120', '192.168.1.120:12345')
|
|
|
49 |
, array('9999:0000:aaaa:bbbb:cccc:dddd:eeee:ffff', '[9999:0000:aaaa:bbbb:cccc:dddd:eeee:ffff]:12345')
|
|
|
50 |
, array('10.0.0.1', '10.0.0.1:80')
|
|
|
51 |
);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* @dataProvider remoteAddressProvider
|
|
|
56 |
* @covers React\Socket\Connection::parseAddress
|
|
|
57 |
*/
|
|
|
58 |
public function testParseAddress($expected, $given)
|
|
|
59 |
{
|
|
|
60 |
$class = new \ReflectionClass('React\\Socket\\Connection');
|
|
|
61 |
$method = $class->getMethod('parseAddress');
|
|
|
62 |
$method->setAccessible(true);
|
|
|
63 |
|
|
|
64 |
$socket = fopen('php://temp', 'r');
|
|
|
65 |
$loop = $this->createLoopMock();
|
|
|
66 |
|
|
|
67 |
$conn = new Connection($socket, $loop);
|
|
|
68 |
$result = $method->invokeArgs($conn, array($given));
|
|
|
69 |
|
|
|
70 |
$this->assertEquals($expected, $result);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
private function createLoopMock()
|
|
|
74 |
{
|
|
|
75 |
return $this->getMock('React\EventLoop\LoopInterface');
|
|
|
76 |
}
|
|
|
77 |
}
|