|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpOpcua\SessionManager\Tests\Helpers; |
| 6 | + |
| 7 | +use PhpOpcua\SessionManager\Client\ManagedClient; |
| 8 | +use ReflectionProperty; |
| 9 | +use RuntimeException; |
| 10 | + |
| 11 | +/** |
| 12 | + * Cross-OS fake daemon over TCP loopback for unit testing `ManagedClient`. |
| 13 | + * |
| 14 | + * Uses `proc_open()` + `tcp://127.0.0.1:0` instead of `pcntl_fork()` + `unix://`, |
| 15 | + * so it runs on Linux, macOS, and Windows. |
| 16 | + */ |
| 17 | +final class FakeTcpDaemon |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @param array<int, array<string, mixed>> $responses JSON objects emitted in order, one per inbound frame. |
| 21 | + * @return array{endpoint: string, process: resource, pipes: array, scriptFile: string} |
| 22 | + */ |
| 23 | + public static function start(array $responses): array |
| 24 | + { |
| 25 | + $listener = @stream_socket_server('tcp://127.0.0.1:0', $errno, $errstr); |
| 26 | + if ($listener === false) { |
| 27 | + throw new RuntimeException("Cannot open listener: [{$errno}] {$errstr}"); |
| 28 | + } |
| 29 | + $name = stream_socket_get_name($listener, false); |
| 30 | + fclose($listener); |
| 31 | + if ($name === false) { |
| 32 | + throw new RuntimeException('Cannot resolve listener endpoint'); |
| 33 | + } |
| 34 | + [$host, $port] = explode(':', $name); |
| 35 | + |
| 36 | + $responsesArg = base64_encode(serialize($responses)); |
| 37 | + |
| 38 | + $script = <<<'PHP' |
| 39 | +<?php |
| 40 | +$responses = unserialize(base64_decode($argv[1])); |
| 41 | +$host = $argv[2]; |
| 42 | +$port = (int) $argv[3]; |
| 43 | +
|
| 44 | +$server = stream_socket_server("tcp://{$host}:{$port}"); |
| 45 | +if ($server === false) { |
| 46 | + exit(1); |
| 47 | +} |
| 48 | +
|
| 49 | +// Ready-probe: accept the parent readiness connection and close it without |
| 50 | +// consuming an entry from $responses. |
| 51 | +$probe = @stream_socket_accept($server, 10); |
| 52 | +if ($probe !== false) { |
| 53 | + fclose($probe); |
| 54 | +} |
| 55 | +
|
| 56 | +foreach ($responses as $responseData) { |
| 57 | + $conn = @stream_socket_accept($server, 10); |
| 58 | + if ($conn === false) { |
| 59 | + break; |
| 60 | + } |
| 61 | +
|
| 62 | + $data = ''; |
| 63 | + while (!str_contains($data, "\n")) { |
| 64 | + $chunk = fread($conn, 65536); |
| 65 | + if ($chunk === false || $chunk === '') { |
| 66 | + break; |
| 67 | + } |
| 68 | + $data .= $chunk; |
| 69 | + } |
| 70 | +
|
| 71 | + fwrite($conn, json_encode($responseData) . "\n"); |
| 72 | + fclose($conn); |
| 73 | +} |
| 74 | +
|
| 75 | +fclose($server); |
| 76 | +exit(0); |
| 77 | +PHP; |
| 78 | + |
| 79 | + $scriptFile = tempnam(sys_get_temp_dir(), 'opcua_fake_tcp_') . '.php'; |
| 80 | + file_put_contents($scriptFile, $script); |
| 81 | + |
| 82 | + $descriptors = [ |
| 83 | + 0 => ['pipe', 'r'], |
| 84 | + 1 => ['pipe', 'w'], |
| 85 | + 2 => ['pipe', 'w'], |
| 86 | + ]; |
| 87 | + $process = proc_open( |
| 88 | + [PHP_BINARY, $scriptFile, $responsesArg, $host, $port], |
| 89 | + $descriptors, |
| 90 | + $pipes, |
| 91 | + ); |
| 92 | + if (! is_resource($process)) { |
| 93 | + @unlink($scriptFile); |
| 94 | + |
| 95 | + throw new RuntimeException('Cannot spawn fake daemon subprocess'); |
| 96 | + } |
| 97 | + |
| 98 | + set_error_handler(static fn (): bool => true); |
| 99 | + try { |
| 100 | + $probeConnected = false; |
| 101 | + $deadline = microtime(true) + 3.0; |
| 102 | + while (microtime(true) < $deadline) { |
| 103 | + $probe = @stream_socket_client("tcp://{$host}:{$port}", $err, $errstr, 0.2); |
| 104 | + if ($probe !== false) { |
| 105 | + fclose($probe); |
| 106 | + $probeConnected = true; |
| 107 | + break; |
| 108 | + } |
| 109 | + usleep(50_000); |
| 110 | + } |
| 111 | + } finally { |
| 112 | + restore_error_handler(); |
| 113 | + } |
| 114 | + |
| 115 | + if (! $probeConnected) { |
| 116 | + @unlink($scriptFile); |
| 117 | + proc_terminate($process); |
| 118 | + proc_close($process); |
| 119 | + |
| 120 | + throw new RuntimeException("Fake daemon did not bind tcp://{$host}:{$port} within 3s"); |
| 121 | + } |
| 122 | + |
| 123 | + return [ |
| 124 | + 'endpoint' => "tcp://{$host}:{$port}", |
| 125 | + 'process' => $process, |
| 126 | + 'pipes' => $pipes, |
| 127 | + 'scriptFile' => $scriptFile, |
| 128 | + ]; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @param array{process: resource, pipes: array, scriptFile: string} $daemon |
| 133 | + * @return void |
| 134 | + */ |
| 135 | + public static function stop(array $daemon): void |
| 136 | + { |
| 137 | + foreach ($daemon['pipes'] as $pipe) { |
| 138 | + if (is_resource($pipe)) { |
| 139 | + @fclose($pipe); |
| 140 | + } |
| 141 | + } |
| 142 | + if (is_resource($daemon['process'])) { |
| 143 | + $status = proc_get_status($daemon['process']); |
| 144 | + if ($status['running']) { |
| 145 | + proc_terminate($daemon['process']); |
| 146 | + usleep(200_000); |
| 147 | + $status = proc_get_status($daemon['process']); |
| 148 | + if ($status['running']) { |
| 149 | + proc_terminate($daemon['process'], 9); |
| 150 | + } |
| 151 | + } |
| 152 | + proc_close($daemon['process']); |
| 153 | + } |
| 154 | + if (isset($daemon['scriptFile']) && file_exists($daemon['scriptFile'])) { |
| 155 | + @unlink($daemon['scriptFile']); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @param string $endpoint |
| 161 | + * @param string $sessionId |
| 162 | + * @param float $timeout |
| 163 | + * @return ManagedClient |
| 164 | + */ |
| 165 | + public static function connectClient(string $endpoint, string $sessionId = 'fake-session-id', float $timeout = 2.0): ManagedClient |
| 166 | + { |
| 167 | + $client = new ManagedClient($endpoint, timeout: $timeout); |
| 168 | + |
| 169 | + $ref = new ReflectionProperty(ManagedClient::class, 'sessionId'); |
| 170 | + $ref->setValue($client, $sessionId); |
| 171 | + |
| 172 | + return $client; |
| 173 | + } |
| 174 | +} |
0 commit comments