From 94e10f849292c55b67f1022e734128f063485290 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Thu, 18 Apr 2024 17:19:38 +0400 Subject: [PATCH] do not catch PDOException when phpunit is running --- src/core/Database.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/core/Database.php b/src/core/Database.php index b741b05..410fe56 100644 --- a/src/core/Database.php +++ b/src/core/Database.php @@ -18,17 +18,19 @@ trait Database private static function connect(): PDO { $string = "mysql:hostname=" . DB_HOST . ";dbname=" . DB_NAME; - $pdo_object = new PDO($string, DB_USERNAME, DB_PASSWORD); - try { + return new PDO($string, DB_USERNAME, DB_PASSWORD); } catch (PDOException $e) { - // TODO: Create a page to display the error - Utility::show( - $e - ); - die(); + // if PHPUnit is not running, handle the exception + if (!defined('PHPUNIT_STEAMY_TESTSUITE')) { + // TODO: Display a user-friendly error message + Utility::show("Sorry, we're unable to process your request at the moment. Please try again later."); + die(); + } else { + // if PHPUnit is running, re-throw the exception to allow it to propagate + throw $e; + } } - return $pdo_object; } /**