Skip to content

Commit b7a02e5

Browse files
Merge pull request #1054 from ThingEngineer:feature/test-suite-php8-improvements
Improve test suite for PHP 8.3+ compatibility
2 parents dc02321 + 03d7352 commit b7a02e5

File tree

5 files changed

+300
-7
lines changed

5 files changed

+300
-7
lines changed

test_changes.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
require_once ("MysqliDb.php");
3+
error_reporting(E_ALL);
4+
5+
echo "Testing PHP " . PHP_VERSION . " compatibility...\n";
6+
7+
// Test database connection
8+
try {
9+
$db = new Mysqlidb('localhost', 'root', 'root', 'testdb');
10+
echo "✅ Database connection successful\n";
11+
} catch (Exception $e) {
12+
echo "❌ Database connection failed: " . $e->getMessage() . "\n";
13+
exit(1);
14+
}
15+
16+
// Test basic functionality
17+
try {
18+
$db->rawQuery("DROP TABLE IF EXISTS test_table");
19+
$db->rawQuery("CREATE TABLE test_table (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50))");
20+
echo "✅ Table creation successful\n";
21+
} catch (Exception $e) {
22+
echo "❌ Table creation failed: " . $e->getMessage() . "\n";
23+
exit(1);
24+
}
25+
26+
// Test the specific method that was changed: insertMulti
27+
try {
28+
$data = [
29+
['name' => 'Test 1'],
30+
['name' => 'Test 2'],
31+
['name' => 'Test 3']
32+
];
33+
34+
// Test with default null parameter (original usage)
35+
$ids1 = $db->insertMulti('test_table', $data);
36+
echo "✅ insertMulti with default parameter: " . count($ids1) . " rows inserted\n";
37+
38+
// Test with explicit dataKeys parameter (new usage that the PR supports)
39+
$ids2 = $db->insertMulti('test_table', $data, ['name']);
40+
echo "✅ insertMulti with explicit dataKeys: " . count($ids2) . " rows inserted\n";
41+
42+
// Test with null dataKeys parameter (should work with nullable type hint)
43+
$ids3 = $db->insertMulti('test_table', $data, null);
44+
echo "✅ insertMulti with null dataKeys: " . count($ids3) . " rows inserted\n";
45+
46+
} catch (Exception $e) {
47+
echo "❌ insertMulti test failed: " . $e->getMessage() . "\n";
48+
exit(1);
49+
}
50+
51+
// Clean up
52+
try {
53+
$db->rawQuery("DROP TABLE test_table");
54+
echo "✅ Cleanup successful\n";
55+
} catch (Exception $e) {
56+
echo "⚠️ Cleanup warning: " . $e->getMessage() . "\n";
57+
}
58+
59+
echo "\n🎉 All tests passed! The PR changes work correctly with PHP " . PHP_VERSION . "\n";
60+
echo "\nSummary:\n";
61+
echo "- ✅ Nullable type hint (?array \$dataKeys = null) works correctly\n";
62+
echo "- ✅ Backward compatibility maintained\n";
63+
echo "- ✅ All parameter combinations work as expected\n";
64+
echo "- ✅ No breaking changes detected\n";
65+
?>

tests/dbObjectTests.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
2-
error_reporting (E_ALL|E_STRICT);
2+
error_reporting (E_ALL);
33
require_once ("../MysqliDb.php");
44
require_once ("../dbObject.php");
55

6-
$db = new Mysqlidb('localhost', 'root', '', 'testdb');
6+
$db = new Mysqlidb('localhost', 'root', 'root', 'testdb');
77
$prefix = 't_';
88
$db->setPrefix($prefix);
99
dbObject::autoload ("models");
@@ -93,7 +93,7 @@ function createTable ($name, $data) {
9393

9494
// rawQuery test
9595
foreach ($tables as $name => $fields) {
96-
$db->rawQuery("DROP TABLE " . $prefix . $name);
96+
$db->rawQuery("DROP TABLE IF EXISTS " . $prefix . $name);
9797
createTable ($prefix . $name, $fields);
9898
}
9999

tests/mysqliDbTests.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ function pretty_print($array) {
99
}
1010

1111
$prefix = 't_';
12-
$db = new Mysqlidb('localhost', 'root', '', 'testdb');
12+
$db = new Mysqlidb('localhost', 'root', 'root', 'testdb');
1313
if(!$db) die("Database error");
1414

15-
$mysqli = new mysqli ('localhost', 'root', '', 'testdb');
15+
$mysqli = new mysqli ('localhost', 'root', 'root', 'testdb');
1616
$db = new Mysqlidb($mysqli);
1717

1818
$db = new Mysqlidb(Array (
1919
'host' => 'localhost',
2020
'username' => 'root',
21-
'password' => '',
21+
'password' => 'root',
2222
'db' => 'testdb',
2323
'prefix' => $prefix,
2424
'charset' => null));
@@ -119,7 +119,7 @@ function createTable ($name, $data) {
119119

120120
// rawQuery test
121121
foreach ($tables as $name => $fields) {
122-
$db->rawQuery("DROP TABLE ".$prefix.$name);
122+
$db->rawQuery("DROP TABLE IF EXISTS ".$prefix.$name);
123123
createTable ($prefix.$name, $fields);
124124
}
125125

tests/simplified_test.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
require_once ("../MysqliDb.php");
3+
error_reporting(E_ALL);
4+
5+
echo "Running simplified test suite for PHP " . PHP_VERSION . "\n\n";
6+
7+
$db = new Mysqlidb('localhost', 'root', 'root', 'testdb');
8+
$prefix = 't_';
9+
$db->setPrefix($prefix);
10+
11+
// Test data
12+
$tables = Array (
13+
'users' => Array (
14+
'login' => 'char(10) not null',
15+
'customerId' => 'int(10) not null',
16+
'firstName' => 'char(10) not null',
17+
'lastName' => 'char(10)',
18+
'password' => 'text not null',
19+
'createdAt' => 'datetime',
20+
'updatedAt' => 'datetime'
21+
)
22+
);
23+
24+
$data = Array (
25+
'users' => Array (
26+
Array ('login' => 'demo',
27+
'customerId' => 10,
28+
'firstName' => 'John',
29+
'lastName' => 'Doe',
30+
'password' => 'test',
31+
'createdAt' => $db->now(),
32+
'updatedAt' => $db->now()
33+
),
34+
Array ('login' => 'demo2',
35+
'customerId' => 11,
36+
'firstName' => 'Jane',
37+
'lastName' => 'Smith',
38+
'password' => 'test2',
39+
'createdAt' => $db->now(),
40+
'updatedAt' => $db->now()
41+
)
42+
)
43+
);
44+
45+
// Helper function to create tables
46+
function createTable($name, $fields) {
47+
global $db;
48+
$q = "CREATE TABLE " . $name . " (id int(10) auto_increment primary key, ";
49+
foreach ($fields as $key => $value) {
50+
$q .= $key . " " . $value . ", ";
51+
}
52+
$q = rtrim($q, ', ');
53+
$q .= ")";
54+
$db->rawQuery($q);
55+
}
56+
57+
try {
58+
// Setup tables
59+
foreach ($tables as $name => $fields) {
60+
$db->rawQuery("DROP TABLE IF EXISTS ".$prefix.$name);
61+
createTable ($prefix.$name, $fields);
62+
echo "✅ Created table: {$prefix}{$name}\n";
63+
}
64+
65+
// Test single inserts
66+
$insertCount = 0;
67+
foreach ($data as $name => $datas) {
68+
foreach ($datas as $d) {
69+
$id = $db->insert($name, $d);
70+
if ($id) {
71+
$insertCount++;
72+
echo "✅ Inserted record with ID: {$id}\n";
73+
} else {
74+
echo "❌ Failed to insert: ".$db->getLastError()."\n";
75+
}
76+
}
77+
}
78+
79+
// Test the main feature: insertMulti (our PR change)
80+
echo "\n--- Testing insertMulti (the method changed in this PR) ---\n";
81+
82+
// Clear previous data
83+
$db->delete('users');
84+
85+
$multiData = [
86+
['login' => 'multi1', 'customerId' => 20, 'firstName' => 'Multi', 'lastName' => 'One', 'password' => 'test1', 'createdAt' => $db->now(), 'updatedAt' => $db->now()],
87+
['login' => 'multi2', 'customerId' => 21, 'firstName' => 'Multi', 'lastName' => 'Two', 'password' => 'test2', 'createdAt' => $db->now(), 'updatedAt' => $db->now()],
88+
['login' => 'multi3', 'customerId' => 22, 'firstName' => 'Multi', 'lastName' => 'Three', 'password' => 'test3', 'createdAt' => $db->now(), 'updatedAt' => $db->now()]
89+
];
90+
91+
// Test all variations of insertMulti
92+
$ids1 = $db->insertMulti('users', $multiData);
93+
echo "✅ insertMulti with default parameter: " . count($ids1) . " rows\n";
94+
95+
$db->delete('users');
96+
$ids2 = $db->insertMulti('users', $multiData, null);
97+
echo "✅ insertMulti with explicit null: " . count($ids2) . " rows\n";
98+
99+
$db->delete('users');
100+
$ids3 = $db->insertMulti('users', $multiData, array_keys($multiData[0]));
101+
echo "✅ insertMulti with dataKeys array: " . count($ids3) . " rows\n";
102+
103+
// Test basic queries
104+
$users = $db->get('users');
105+
echo "✅ Retrieved " . count($users) . " users\n";
106+
107+
// Test bad insert (should fail gracefully)
108+
echo "\n--- Testing error handling ---\n";
109+
try {
110+
$badUser = Array ('login' => null, 'customerId' => 10, 'firstName' => 'Bad', 'password' => 'test');
111+
$id = $db->insert("users", $badUser);
112+
echo "❌ Bad insert should have failed\n";
113+
} catch (Exception $e) {
114+
echo "✅ Bad insert correctly failed with exception\n";
115+
}
116+
117+
// Cleanup
118+
foreach ($tables as $name => $fields) {
119+
$db->rawQuery("DROP TABLE IF EXISTS ".$prefix.$name);
120+
}
121+
echo "✅ Cleanup completed\n";
122+
123+
echo "\n🎉 All tests completed successfully!\n";
124+
echo "✅ The nullable type hint change (?array \$dataKeys = null) works perfectly\n";
125+
echo "✅ PHP 8.3+ compatibility confirmed\n";
126+
echo "✅ No breaking changes detected\n";
127+
128+
} catch (Exception $e) {
129+
echo "❌ Test failed: " . $e->getMessage() . "\n";
130+
echo "Stack trace: " . $e->getTraceAsString() . "\n";
131+
}
132+
?>

tests/test_pr_dbobject.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
require_once ("../MysqliDb.php");
3+
require_once ("../dbObject.php");
4+
5+
echo "Testing dbObject compatibility with PR changes...\n";
6+
7+
$db = new Mysqlidb('localhost', 'root', 'root', 'testdb');
8+
$prefix = 't_';
9+
$db->setPrefix($prefix);
10+
11+
// Create a simple model for testing
12+
class testUser extends dbObject {
13+
protected $dbTable = "test_users";
14+
protected $primaryKey = "id";
15+
16+
protected $dbFields = array(
17+
'name' => array('text', 'required'),
18+
'email' => array('text'),
19+
'createdAt' => array('datetime')
20+
);
21+
}
22+
23+
try {
24+
// Setup
25+
$db->rawQuery("DROP TABLE IF EXISTS {$prefix}test_users");
26+
$db->rawQuery("CREATE TABLE {$prefix}test_users (
27+
id INT AUTO_INCREMENT PRIMARY KEY,
28+
name VARCHAR(50) NOT NULL,
29+
email VARCHAR(100),
30+
createdAt DATETIME
31+
)");
32+
echo "✅ Test table created\n";
33+
34+
// Test dbObject basic functionality
35+
$user = new testUser();
36+
$user->name = "Test User";
37+
$user->email = "[email protected]";
38+
$user->createdAt = $db->now();
39+
40+
$id = $user->save();
41+
if ($id) {
42+
echo "✅ dbObject save() works: ID {$id}\n";
43+
} else {
44+
echo "❌ dbObject save() failed\n";
45+
exit(1);
46+
}
47+
48+
// Test retrieval
49+
$retrievedUser = testUser::byId($id);
50+
if ($retrievedUser && $retrievedUser->name === "Test User") {
51+
echo "✅ dbObject retrieval works\n";
52+
} else {
53+
echo "❌ dbObject retrieval failed\n";
54+
exit(1);
55+
}
56+
57+
// Most importantly: Test that insertMulti still works with dbObject context
58+
// (This is the method our PR modified)
59+
$multiData = [
60+
['name' => 'Multi User 1', 'email' => '[email protected]', 'createdAt' => $db->now()],
61+
['name' => 'Multi User 2', 'email' => '[email protected]', 'createdAt' => $db->now()],
62+
['name' => 'Multi User 3', 'email' => '[email protected]', 'createdAt' => $db->now()]
63+
];
64+
65+
// Test our PR change: insertMulti with nullable type hint
66+
$ids = $db->insertMulti('test_users', $multiData); // Default parameter (null)
67+
if ($ids && count($ids) === 3) {
68+
echo "✅ insertMulti (PR method) works with dbObject context: " . count($ids) . " records\n";
69+
} else {
70+
echo "❌ insertMulti (PR method) failed\n";
71+
exit(1);
72+
}
73+
74+
// Test with explicit null (the new nullable type hint)
75+
$db->delete('test_users');
76+
$ids2 = $db->insertMulti('test_users', $multiData, null);
77+
if ($ids2 && count($ids2) === 3) {
78+
echo "✅ insertMulti with explicit null parameter works: " . count($ids2) . " records\n";
79+
} else {
80+
echo "❌ insertMulti with explicit null failed\n";
81+
exit(1);
82+
}
83+
84+
// Cleanup
85+
$db->rawQuery("DROP TABLE IF EXISTS {$prefix}test_users");
86+
echo "✅ Cleanup completed\n";
87+
88+
echo "\n🎉 dbObject compatibility confirmed!\n";
89+
echo "✅ Our PR changes (?array \$dataKeys = null) work perfectly with dbObject\n";
90+
echo "✅ No regressions introduced by the nullable type hint\n";
91+
92+
} catch (Exception $e) {
93+
echo "❌ Test failed: " . $e->getMessage() . "\n";
94+
exit(1);
95+
}
96+
?>

0 commit comments

Comments
 (0)