1
1
<?php
2
2
3
- it ('tests udp connection ' , function (){
3
+ use Workerman \Connection \UdpConnection ;
4
+ use Symfony \Component \Process \PhpProcess ;
4
5
6
+ $ remoteAddress = '[::1]:12345 ' ;
7
+ $ process = new PhpProcess (<<<PHP
8
+ <?php
9
+ \$socketServer = stream_socket_server("udp:// $ remoteAddress");
10
+ do{
11
+ \$data = stream_socket_recvfrom( \$socketServer, 3);
12
+ }while( \$data !== false && \$data !== 'bye');
13
+ PHP
14
+ );
15
+ $ process ->run ();
16
+
17
+ it ('tests ' . UdpConnection::class, function () use ($ remoteAddress ) {
18
+
19
+ $ socketClient = stream_socket_client ("udp:// $ remoteAddress " );
20
+ $ udpConnection = new UdpConnection ($ socketClient , $ remoteAddress );
21
+ $ udpConnection ->protocol = \Workerman \Protocols \Text::class;
22
+ expect ($ udpConnection ->send ('foo ' ))->toBeTrue ();
23
+
24
+ expect ($ udpConnection ->getRemoteIp ())->toBe ('::1 ' );
25
+ expect ($ udpConnection ->getRemotePort ())->toBe (12345 );
26
+ expect ($ udpConnection ->getRemoteAddress ())->toBe ($ remoteAddress );
27
+ expect ($ udpConnection ->getLocalIp ())->toBeIn (['::1 ' , '[::1] ' , '127.0.0.1 ' ]);
28
+ expect ($ udpConnection ->getLocalPort ())->toBeInt ();
29
+
30
+ expect (json_encode ($ udpConnection ))->toBeJson ()
31
+ ->toContain ('transport ' )
32
+ ->toContain ('getRemoteIp ' )
33
+ ->toContain ('remotePort ' )
34
+ ->toContain ('getRemoteAddress ' )
35
+ ->toContain ('getLocalIp ' )
36
+ ->toContain ('getLocalPort ' )
37
+ ->toContain ('isIpV4 ' )
38
+ ->toContain ('isIpV6 ' );
39
+
40
+ $ udpConnection ->close ('bye ' );
41
+ if (is_resource ($ socketClient )) {
42
+ fclose ($ socketClient );
43
+ }
5
44
});
0 commit comments