@@ -615,19 +615,30 @@ class my_mysqli_fake_server_conn
615615
616616class my_mysqli_fake_server_process
617617{
618+ private int $ port ;
619+
618620 public function __construct (private $ process , private array $ pipes ) {}
619621
620- public function terminate (bool $ wait = false )
622+ public function terminate (bool $ wait = false ): void
621623 {
622624 if ($ wait ) {
623625 $ this ->wait ();
624626 }
625627 proc_terminate ($ this ->process );
626628 }
627629
628- public function wait ()
630+ public function wait (): void
629631 {
630- echo fgets ($ this ->pipes [1 ]);
632+ $ line = fgets ($ this ->pipes [1 ]);
633+ if (preg_match ('/\[\*\] Server started on \d+\.\d+\.\d+\.\d+:(\d+)/ ' , $ line , $ matches )) {
634+ $ this ->port = (int )$ matches [1 ];
635+ }
636+ echo $ line ;
637+ }
638+
639+ public function getPort (): int
640+ {
641+ return $ this ->port ?? throw new RuntimeException ("Port not set " );
631642 }
632643}
633644
@@ -807,15 +818,20 @@ function my_mysqli_test_query_response_row_read_two_fields(my_mysqli_fake_server
807818 }
808819}
809820
810- function run_fake_server (string $ test_function , $ port = 33305 ): void
821+ function run_fake_server (string $ test_function , int | string $ port = 0 ): int
811822{
812- $ address = '127.0.0.1 ' ;
823+ $ host = '127.0.0.1 ' ;
813824
814- $ socket = @stream_socket_server ("tcp:// $ address : $ port " , $ errno , $ errstr );
825+ $ socket = @stream_socket_server ("tcp:// $ host : $ port " , $ errno , $ errstr );
815826 if (!$ socket ) {
816827 die ("Failed to create socket: $ errstr ( $ errno) \n" );
817828 }
818- echo "[*] Server started \n" ;
829+ if (intval ($ port ) === 0 ) {
830+ $ address = stream_socket_get_name ($ socket , false );
831+ list ($ host , $ port ) = explode (": " , $ address );
832+ }
833+
834+ echo "[*] Server started on $ host: $ port \n" ;
819835
820836 try {
821837 $ conn = new my_mysqli_fake_server_conn ($ socket );
@@ -832,7 +848,7 @@ function run_fake_server(string $test_function, $port = 33305): void
832848}
833849
834850
835- function run_fake_server_in_background ($ test_function , $ port = 33305 ): my_mysqli_fake_server_process
851+ function run_fake_server_in_background ($ test_function , $ port = 0 ): my_mysqli_fake_server_process
836852{
837853 $ command = [PHP_BINARY , '-n ' , __FILE__ , 'mysqli_fake_server ' , $ test_function , $ port ];
838854
@@ -852,5 +868,5 @@ function run_fake_server_in_background($test_function, $port = 33305): my_mysqli
852868}
853869
854870if (isset ($ argv ) && $ argc > 2 && $ argv [1 ] == 'mysqli_fake_server ' ) {
855- run_fake_server ($ argv [2 ], $ argv [3 ] ?? ' 33305 ' );
871+ run_fake_server ($ argv [2 ], $ argv [3 ] ?? 0 );
856872}
0 commit comments