Skip to content

Commit 2719dc0

Browse files
committed
#32: * fix Mysqli tests
1 parent 1ca2aa2 commit 2719dc0

11 files changed

+132
-110
lines changed

tests/mysqli/001-mysqli_connect_async.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
--TEST--
22
MySQLi: Basic async connection test
33
--EXTENSIONS--
4-
async
54
mysqli
65
--SKIPIF--
76
<?php

tests/mysqli/002-mysqli_query_async.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
--TEST--
22
MySQLi: Async query execution
33
--EXTENSIONS--
4-
async
54
mysqli
65
--SKIPIF--
76
<?php

tests/mysqli/003-mysqli_concurrent_connections.phpt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
--TEST--
22
MySQLi: Concurrent connections in separate coroutines
33
--EXTENSIONS--
4-
async
54
mysqli
65
--SKIPIF--
76
<?php
@@ -26,30 +25,27 @@ $coroutines = [
2625
$mysqli = AsyncMySQLiTest::factory();
2726
$result = $mysqli->query("SELECT 'coroutine1' as source, CONNECTION_ID() as conn_id");
2827
$row = $result->fetch_assoc();
29-
echo "from {$row['source']} conn_id: {$row['conn_id']}\n";
3028
$result->free();
3129
$mysqli->close();
32-
return $row['conn_id'];
30+
return ['source' => $row['source'], 'conn_id' => $row['conn_id']];
3331
}),
3432

3533
spawn(function() {
3634
$mysqli = AsyncMySQLiTest::factory();
3735
$result = $mysqli->query("SELECT 'coroutine2' as source, CONNECTION_ID() as conn_id");
3836
$row = $result->fetch_assoc();
39-
echo "from {$row['source']} conn_id: {$row['conn_id']}\n";
4037
$result->free();
4138
$mysqli->close();
42-
return $row['conn_id'];
39+
return ['source' => $row['source'], 'conn_id' => $row['conn_id']];
4340
}),
4441

4542
spawn(function() {
4643
$mysqli = AsyncMySQLiTest::factory();
4744
$result = $mysqli->query("SELECT 'coroutine3' as source, CONNECTION_ID() as conn_id");
4845
$row = $result->fetch_assoc();
49-
echo "from {$row['source']} conn_id: {$row['conn_id']}\n";
5046
$result->free();
5147
$mysqli->close();
52-
return $row['conn_id'];
48+
return ['source' => $row['source'], 'conn_id' => $row['conn_id']];
5349
}),
5450

5551
spawn(function() {
@@ -59,16 +55,29 @@ $coroutines = [
5955
$mysqli->query("INSERT INTO temp_work VALUES (1, 'data1'), (2, 'data2')");
6056
$result = $mysqli->query("SELECT COUNT(*) as count, CONNECTION_ID() as conn_id FROM temp_work");
6157
$row = $result->fetch_assoc();
62-
echo "from coroutine4 (with work) conn_id: {$row['conn_id']}, count: {$row['count']}\n";
6358
$result->free();
6459
$mysqli->close();
65-
return $row['conn_id'];
60+
return ['source' => 'coroutine4', 'conn_id' => $row['conn_id'], 'count' => $row['count']];
6661
})
6762
];
6863

69-
$connectionIds = awaitAllOrFail($coroutines);
64+
$results = awaitAllOrFail($coroutines);
65+
66+
// Display results in deterministic order
67+
usort($results, function($a, $b) {
68+
return strcmp($a['source'], $b['source']);
69+
});
70+
71+
foreach ($results as $result) {
72+
if (isset($result['count'])) {
73+
echo "from {$result['source']} (with work) conn_id: {$result['conn_id']}, count: {$result['count']}\n";
74+
} else {
75+
echo "from {$result['source']} conn_id: {$result['conn_id']}\n";
76+
}
77+
}
7078

7179
// Verify all connections are different
80+
$connectionIds = array_map(function($r) { return $r['conn_id']; }, $results);
7281
$uniqueIds = array_unique($connectionIds);
7382
echo "unique connections: " . count($uniqueIds) . "\n";
7483
echo "total coroutines: " . count($connectionIds) . "\n";

tests/mysqli/004-mysqli_prepared_async.phpt

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
--TEST--
22
MySQLi: Async prepared statements
33
--EXTENSIONS--
4-
async
54
mysqli
65
--SKIPIF--
76
<?php
8-
if (!extension_loaded('mysqli')) die('skip mysqli not available');
9-
if (!getenv('MYSQL_TEST_HOST')) die('skip MYSQL_TEST_HOST not set');
7+
require_once __DIR__ . '/inc/async_mysqli_test.inc';
8+
AsyncMySQLiTest::skipIfNoAsync();
9+
AsyncMySQLiTest::skipIfNoMySQLi();
10+
AsyncMySQLiTest::skip();
1011
?>
1112
--FILE--
1213
<?php
14+
require_once __DIR__ . '/inc/async_mysqli_test.inc';
1315

1416
use function Async\spawn;
1517
use function Async\await;
1618

1719
echo "start\n";
1820

1921
$coroutine = spawn(function() {
20-
$host = getenv("MYSQL_TEST_HOST") ?: "127.0.0.1";
21-
$port = getenv("MYSQL_TEST_PORT") ?: 3306;
22-
$user = getenv("MYSQL_TEST_USER") ?: "root";
23-
$passwd = getenv("MYSQL_TEST_PASSWD") ?: "";
24-
$db = getenv("MYSQL_TEST_DB") ?: "test";
25-
2622
try {
27-
$mysqli = new mysqli($host, $user, $passwd, $db, $port);
28-
29-
if ($mysqli->connect_error) {
30-
throw new Exception("Connection failed: " . $mysqli->connect_error);
31-
}
23+
$mysqli = AsyncMySQLiTest::factory();
3224

3325
// Create test table
3426
$mysqli->query("DROP TEMPORARY TABLE IF EXISTS async_prepared_test");
@@ -58,7 +50,7 @@ $coroutine = spawn(function() {
5850
echo "inserted records with prepared statement\n";
5951

6052
// Test SELECT prepared statement
61-
$stmt = $mysqli->prepare("SELECT id, name, score FROM async_prepared_test WHERE score > ? AND active = ?");
53+
$stmt = $mysqli->prepare("SELECT id, name, score FROM async_prepared_test WHERE score > ? AND active = ? ORDER BY id");
6254
if (!$stmt) {
6355
throw new Exception("Prepare SELECT failed: " . $mysqli->error);
6456
}
@@ -118,7 +110,6 @@ table created
118110
inserted records with prepared statement
119111
records with score > 80 and active = 1:
120112
id: 1, name: user1, score: 85
121-
id: 2, name: user2, score: 92
122113
updated user1 with bonus 5 points
123114
affected rows: 1
124115
user1 new score: 90

tests/mysqli/005-mysqli_multi_query_async.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
--TEST--
22
MySQLi: Async multi-query execution
33
--EXTENSIONS--
4-
async
54
mysqli
65
--SKIPIF--
76
<?php

tests/mysqli/006-mysqli_transaction_async.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
--TEST--
22
MySQLi: Async transaction handling
33
--EXTENSIONS--
4-
async
54
mysqli
65
--SKIPIF--
76
<?php

tests/mysqli/007-mysqli_error_scenarios.phpt

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
--TEST--
22
MySQLi: Async error scenarios
33
--EXTENSIONS--
4-
async
54
mysqli
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, '[email protected]')");
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, '[email protected]')");
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

114107
echo "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+
117123
echo "all error tests completed\n";
118124
foreach ($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

122130
echo "end\n";

tests/mysqli/008-mysqli_result_async.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
--TEST--
22
MySQLi: Async result handling and fetch methods
33
--EXTENSIONS--
4-
async
54
mysqli
65
--SKIPIF--
76
<?php

0 commit comments

Comments
 (0)