1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the php-gelf package. |
6
|
|
|
* |
7
|
|
|
* (c) Benjamin Zikarsky <http://benjamin-zikarsky.de> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Gelf\Test\Transport; |
14
|
|
|
|
15
|
|
|
use Gelf\Transport\StreamSocketClient; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use RuntimeException; |
18
|
|
|
|
19
|
|
|
class StreamSocketClientUdpTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
private StreamSocketClient $socketClient; |
22
|
|
|
private mixed $serverSocket; |
23
|
|
|
|
24
|
|
|
public function setUp(): void |
25
|
|
|
{ |
26
|
|
|
$host = "127.0.0.1"; |
27
|
|
|
$this->serverSocket = stream_socket_server( |
28
|
|
|
"udp://$host:0", |
29
|
|
|
$errNo, |
30
|
|
|
$errMsg, |
31
|
|
|
flags: STREAM_SERVER_BIND |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
if (!$this->serverSocket) { |
35
|
|
|
throw new \RuntimeException("Failed to create test-server-socket"); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// get random port |
39
|
|
|
$socketName = stream_socket_get_name( |
40
|
|
|
$this->serverSocket, |
41
|
|
|
remote: false |
42
|
|
|
); |
43
|
|
|
[, $port] = explode(":", $socketName); |
44
|
|
|
|
45
|
|
|
$this->socketClient = new StreamSocketClient('udp', $host, (int)$port); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function tearDown(): void |
49
|
|
|
{ |
50
|
|
|
unset($this->socketClient); |
51
|
|
|
fclose($this->serverSocket); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testInvalidConstructorArguments(): void |
55
|
|
|
{ |
56
|
|
|
self::expectException(RuntimeException::class); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$client = new StreamSocketClient("not-a-scheme", "not-a-host", -1); |
59
|
|
|
$client->getSocket(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testGetSocket(): void |
63
|
|
|
{ |
64
|
|
|
self::assertIsResource($this->socketClient->getSocket()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testWrite(): void |
68
|
|
|
{ |
69
|
|
|
$testData = "Hello World!"; |
70
|
|
|
$numBytes = $this->socketClient->write($testData); |
71
|
|
|
|
72
|
|
|
self::assertEquals(strlen($testData), $numBytes); |
73
|
|
|
|
74
|
|
|
// check that message is sent to server |
75
|
|
|
$readData = fread($this->serverSocket, $numBytes); |
76
|
|
|
|
77
|
|
|
self::assertEquals($testData, $readData); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|