new hook-based system for inserting callbacks to run before and after a query

debugMode() now works through the new system
This commit is contained in:
Sergey Tsalkov
2021-06-22 06:01:28 +00:00
parent ac626b0795
commit 6487873c68
4 changed files with 232 additions and 158 deletions

View File

@@ -1,48 +1,52 @@
<?php
function new_error_callback($params) {
function my_error_handler($params) {
global $error_callback_worked;
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $error_callback_worked = 1;
return false;
}
function my_debug_handler($params) {
function my_success_handler($params) {
global $debug_callback_worked;
if (substr_count($params['query'], 'SELECT')) $debug_callback_worked = 1;
return false;
}
class ErrorTest extends SimpleTest {
static function static_error_callback($params) {
global $static_error_callback_worked;
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $static_error_callback_worked = 1;
return false;
}
function nonstatic_error_callback($params) {
global $nonstatic_error_callback_worked;
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $nonstatic_error_callback_worked = 1;
return false;
}
function test_1_error_handler() {
global $error_callback_worked, $static_error_callback_worked, $nonstatic_error_callback_worked;
DB::$error_handler = 'new_error_callback';
DB::addHook('run_failed', 'my_error_handler');
DB::query("SELET * FROM accounts");
$this->assert($error_callback_worked === 1);
DB::$error_handler = array('ErrorTest', 'static_error_callback');
DB::removeHooks('run_failed');
DB::addHook('run_failed', array('ErrorTest', 'static_error_callback'));
DB::query("SELET * FROM accounts");
$this->assert($static_error_callback_worked === 1);
DB::$error_handler = array($this, 'nonstatic_error_callback');
DB::removeHooks('run_failed');
DB::addHook('run_failed', array($this, 'nonstatic_error_callback'));
DB::query("SELET * FROM accounts");
$this->assert($nonstatic_error_callback_worked === 1);
}
public static function static_error_callback($params) {
global $static_error_callback_worked;
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $static_error_callback_worked = 1;
}
public function nonstatic_error_callback($params) {
global $nonstatic_error_callback_worked;
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $nonstatic_error_callback_worked = 1;
DB::removeHooks('run_failed');
}
function test_2_exception_catch() {
$dbname = DB::$dbName;
DB::$error_handler = '';
DB::$throw_exception_on_error = true;
try {
DB::query("SELET * FROM accounts");
} catch(MeekroDBException $e) {
@@ -67,15 +71,29 @@ class ErrorTest extends SimpleTest {
$this->assert($exception_was_caught === 2);
}
function test_3_debugmode_handler() {
function test_3_success_handler() {
global $debug_callback_worked;
DB::debugMode('my_debug_handler');
DB::addHook('run_success', 'my_success_handler');
DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
$this->assert($debug_callback_worked === 1);
DB::removeHooks('run_success');
}
function test_4_error_handler() {
global $anonymous_error_callback_worked;
DB::debugMode(false);
$error_handler = function($params) {
global $anonymous_error_callback_worked;
if (substr_count($params['error'], 'You have an error in your SQL syntax')) {
$anonymous_error_callback_worked = 1;
}
return false;
};
DB::addHook('run_failed', $error_handler);
DB::query("SELET * FROM accounts");
$this->assert($anonymous_error_callback_worked === 1);
DB::removeHooks('run_failed');
}
}

View File

@@ -1,19 +0,0 @@
<?php
class ErrorTest_53 extends SimpleTest {
function test_1_error_handler() {
global $anonymous_error_callback_worked;
DB::$throw_exception_on_error = false;
DB::$error_handler = function($params) {
global $anonymous_error_callback_worked;
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $anonymous_error_callback_worked = 1;
};
DB::query("SELET * FROM accounts");
$this->assert($anonymous_error_callback_worked === 1);
}
}
?>

View File

@@ -35,7 +35,6 @@ require_once __DIR__ . '/CallTest.php';
require_once __DIR__ . '/ObjectTest.php';
require_once __DIR__ . '/WhereClauseTest.php';
require_once __DIR__ . '/ErrorTest.php';
require_once __DIR__ . '/ErrorTest_53.php';
require_once __DIR__ . '/TransactionTest.php';
require_once __DIR__ . '/HelperTest.php';
@@ -45,7 +44,6 @@ $classes_to_test = array(
'WhereClauseTest',
'ObjectTest',
'ErrorTest',
'ErrorTest_53',
'TransactionTest',
'HelperTest',
);