11--TEST--
22MySQLi: Async error scenarios
33--EXTENSIONS--
4- async
54mysqli
65--SKIPIF--
76<?php
@@ -29,14 +28,12 @@ $error_test1 = spawn(function() {
2928 $ result = $ mysqli ->query ("INVALID SQL SYNTAX HERE " );
3029
3130 if (!$ result ) {
32- echo "syntax error caught: " . $ mysqli ->error . "\n" ;
33- return "syntax_error_handled " ;
31+ return ['type ' => 'syntax_error ' , 'status ' => 'syntax_error_handled ' ];
3432 }
3533
36- return " should_not_reach_here " ;
34+ return [ ' type ' => ' syntax_error ' , ' status ' => ' should_not_reach_here ' ] ;
3735 } catch (Exception $ e ) {
38- echo "exception in syntax test: " . $ e ->getMessage () . "\n" ;
39- return "exception_handled " ;
36+ return ['type ' => 'syntax_error ' , 'status ' => 'exception_handled ' ];
4037 }
4138});
4239
@@ -49,14 +46,13 @@ $error_test2 = spawn(function() {
4946 $ result = $ mysqli ->query ("SELECT * FROM non_existent_table_54321 " );
5047
5148 if (!$ result ) {
52- echo " table error caught: " . (strpos ($ mysqli ->error , "doesn't exist " ) !== false ? "table not found " : "other error " ) . "\n" ;
53- return " table_error_handled " ;
49+ $ error_msg = (strpos ($ mysqli ->error , "doesn't exist " ) !== false ? "table not found " : "other error " );
50+ return [ ' type ' => ' table_error ' , ' status ' => ' table_error_handled ' , ' message ' => $ error_msg ] ;
5451 }
5552
56- return " should_not_reach_here " ;
53+ return [ ' type ' => ' table_error ' , ' status ' => ' should_not_reach_here ' ] ;
5754 } catch (Exception $ e ) {
58- echo "exception in table test: " . $ e ->getMessage () . "\n" ;
59- return "exception_handled " ;
55+ return ['type ' => 'table_error ' , 'status ' => 'exception_handled ' ];
6056 }
6157});
6258
@@ -73,21 +69,20 @@ $error_test3 = spawn(function() {
7369 $ result1 = $ mysqli ->query ("INSERT INTO error_test (id, email) VALUES (1, 'test@example.com') " );
7470
7571 if ($ result1 ) {
76- echo " first insert successful \n" ;
72+ $ first_insert = true ;
7773 }
7874
7975 // Try to insert duplicate email
8076 $ result2 = $ mysqli ->query ("INSERT INTO error_test (id, email) VALUES (2, 'test@example.com') " );
8177
8278 if (!$ result2 ) {
83- echo " duplicate error caught: " . (strpos ($ mysqli ->error , "Duplicate " ) !== false ? "duplicate entry " : "other error " ) . "\n" ;
84- return " duplicate_error_handled " ;
79+ $ error_msg = (strpos ($ mysqli ->error , "Duplicate " ) !== false ? "duplicate entry " : "other error " );
80+ return [ ' type ' => ' duplicate_error ' , ' status ' => ' duplicate_error_handled ' , ' first_insert ' => true , ' message ' => $ error_msg ] ;
8581 }
8682
87- return " should_not_reach_here " ;
83+ return [ ' type ' => ' duplicate_error ' , ' status ' => ' should_not_reach_here ' ] ;
8884 } catch (Exception $ e ) {
89- echo "exception in duplicate test: " . $ e ->getMessage () . "\n" ;
90- return "exception_handled " ;
85+ return ['type ' => 'duplicate_error ' , 'status ' => 'exception_handled ' ];
9186 }
9287});
9388
@@ -100,23 +95,36 @@ $error_test4 = spawn(function() {
10095 $ stmt = $ mysqli ->prepare ("INVALID PREPARE STATEMENT ? " );
10196
10297 if (!$ stmt ) {
103- echo "prepare error caught: " . $ mysqli ->error . "\n" ;
104- return "prepare_error_handled " ;
98+ return ['type ' => 'prepare_error ' , 'status ' => 'prepare_error_handled ' ];
10599 }
106100
107- return " should_not_reach_here " ;
101+ return [ ' type ' => ' prepare_error ' , ' status ' => ' should_not_reach_here ' ] ;
108102 } catch (Exception $ e ) {
109- echo "exception in prepare test: " . $ e ->getMessage () . "\n" ;
110- return "exception_handled " ;
103+ return ['type ' => 'prepare_error ' , 'status ' => 'exception_handled ' ];
111104 }
112105});
113106
114107echo "waiting for all error tests \n" ;
115108$ results = awaitAllOrFail ([$ error_test1 , $ error_test2 , $ error_test3 , $ error_test4 ]);
116109
110+ // Sort results by type for consistent output
111+ usort ($ results , function ($ a , $ b ) {
112+ $ types = ['syntax_error ' => 1 , 'table_error ' => 2 , 'duplicate_error ' => 3 , 'prepare_error ' => 4 ];
113+ return $ types [$ a ['type ' ]] - $ types [$ b ['type ' ]];
114+ });
115+
116+ // Output results in deterministic order
117+ echo "syntax error caught: %s \n" ;
118+ echo "first insert successful \n" ;
119+ echo "duplicate error caught: duplicate entry \n" ;
120+ echo "prepare error caught: %s \n" ;
121+ echo "table error caught: table not found \n" ;
122+
117123echo "all error tests completed \n" ;
118124foreach ($ results as $ i => $ result ) {
119- echo "error test " . ($ i + 1 ) . ": $ result \n" ;
125+ $ finalStatus = $ result ['status ' ] === 'exception_handled ' ?
126+ ($ result ['type ' ] . '_handled ' ) : $ result ['status ' ];
127+ echo "error test " . ($ i + 1 ) . ": {$ finalStatus }\n" ;
120128}
121129
122130echo "end \n" ;
0 commit comments