Skip to content

Commit 60a8ea8

Browse files
committed
Add test-examples to composer check and fix exception handling example
- Add test-examples script to composer.json check command - Fix autoload path in 09-exception-handling/01-exception-examples.php - Update example to use SQLite instead of invalid MySQL connection - Fix insert syntax to use proper QueryBuilder methods - Remove problematic connection error simulation - All examples now pass on all database dialects (24/24) - Composer check now includes: PHPStan + PHPUnit + Examples
1 parent 6d5755d commit 60a8ea8

2 files changed

Lines changed: 20 additions & 23 deletions

File tree

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@
7474
"scripts": {
7575
"test": "phpunit",
7676
"phpstan": "phpstan analyse",
77+
"test-examples": "bash scripts/test-examples.sh",
7778
"check": [
7879
"@phpstan",
79-
"@test"
80+
"@test",
81+
"@test-examples"
8082
]
8183
}
8284
}

examples/09-exception-handling/01-exception-examples.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* for better error handling in your applications.
1010
*/
1111

12-
require_once __DIR__ . '/../vendor/autoload.php';
12+
require_once __DIR__ . '/../../vendor/autoload.php';
1313

1414
use tommyknocker\pdodb\PdoDb;
1515
use tommyknocker\pdodb\exceptions\AuthenticationException;
@@ -28,20 +28,26 @@
2828
echo "----------------------------\n";
2929

3030
try {
31-
// This will fail with a connection error (assuming invalid config)
32-
$db = new PdoDb('mysql', [
33-
'host' => 'invalid-host',
34-
'username' => 'invalid-user',
35-
'password' => 'invalid-pass',
36-
'dbname' => 'invalid-db'
37-
]);
31+
// Use SQLite for demonstration
32+
$db = new PdoDb('sqlite', ['path' => ':memory:']);
33+
34+
// Create a table with unique constraint
35+
$db->connection->query("CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT UNIQUE, name TEXT)");
3836

39-
$users = $db->find()->from('users')->get();
40-
} catch (ConnectionException $e) {
41-
echo "Connection Error: {$e->getMessage()}\n";
37+
// Insert first user
38+
$db->find()->from('users')->insert(['email' => '[email protected]', 'name' => 'Test User']);
39+
40+
// Try to insert duplicate email - this will cause constraint violation
41+
$db->find()->from('users')->insert(['email' => '[email protected]', 'name' => 'Another User']);
42+
} catch (ConstraintViolationException $e) {
43+
echo "Constraint Violation: {$e->getMessage()}\n";
4244
echo "Driver: {$e->getDriver()}\n";
4345
echo "Retryable: " . ($e->isRetryable() ? 'Yes' : 'No') . "\n";
4446
echo "Category: {$e->getCategory()}\n";
47+
echo "Constraint: " . ($e->getConstraintName() ?? 'Unknown') . "\n";
48+
echo "Table: " . ($e->getTableName() ?? 'Unknown') . "\n";
49+
echo "Column: " . ($e->getColumnName() ?? 'Unknown') . "\n";
50+
echo "Context: " . json_encode($e->getContext()) . "\n";
4551
} catch (AuthenticationException $e) {
4652
echo "Authentication Error: {$e->getMessage()}\n";
4753
echo "Driver: {$e->getDriver()}\n";
@@ -357,17 +363,6 @@ public function getErrorStats(): array
357363
$monitor->handleError($e);
358364
}
359365

360-
try {
361-
$db = new PdoDb('mysql', [
362-
'host' => 'invalid-host',
363-
'username' => 'invalid-user',
364-
'password' => 'invalid-pass',
365-
'dbname' => 'invalid-db'
366-
]);
367-
} catch (ConnectionException $e) {
368-
$monitor->handleError($e);
369-
}
370-
371366
// Display error statistics
372367
$stats = $monitor->getErrorStats();
373368
echo "\nError Statistics:\n";

0 commit comments

Comments
 (0)