From 50ed9675f3c1f86fe79f4ea6923ba28291ba0263 Mon Sep 17 00:00:00 2001 From: Sergey Tsalkov Date: Sat, 26 Jun 2021 00:52:37 +0000 Subject: [PATCH] fix pre_parse and pre_run hooks add testing for all hooks (roll into previous ErrorTest group) --- db.class.php | 28 +++--- simpletest/ErrorTest.php | 102 --------------------- simpletest/HookTest.php | 187 +++++++++++++++++++++++++++++++++++++++ simpletest/test.php | 4 +- 4 files changed, 203 insertions(+), 118 deletions(-) delete mode 100644 simpletest/ErrorTest.php create mode 100644 simpletest/HookTest.php diff --git a/db.class.php b/db.class.php index ed79742..cd063f8 100644 --- a/db.class.php +++ b/db.class.php @@ -220,18 +220,19 @@ class MeekroDB { foreach ($this->hooks[$type] as $hook) { $result = call_user_func($hook, array('query' => $query, 'args' => $args)); - if ($result) { - if (!is_array($result) || count($result) != 2) { - throw new MeekroDBException("pre_parse hook must return an array of 2 items"); - } - if (!is_string($result[0])) { - throw new MeekroDBException("pre_parse hook must return a string as its first item"); - } - if (!is_array($result[1])) { - throw new MeekroDBException("pre_parse hook must return an array as its second item"); - } + if (is_null($result)) { + $result = array($query, $args); } - + if (!is_array($result) || count($result) != 2) { + throw new MeekroDBException("pre_parse hook must return an array of 2 items"); + } + if (!is_string($result[0])) { + throw new MeekroDBException("pre_parse hook must return a string as its first item"); + } + if (!is_array($result[1])) { + throw new MeekroDBException("pre_parse hook must return an array as its second item"); + } + $query = $result[0]; $args = $result[1]; } @@ -243,9 +244,8 @@ class MeekroDB { foreach ($this->hooks[$type] as $hook) { $result = call_user_func($hook, array('query' => $query)); - if (!is_string($result)) { - throw new MeekroDBException("pre_run hook must return a string"); - } + if (is_null($result)) $result = $query; + if (!is_string($result)) throw new MeekroDBException("pre_run hook must return a string"); $query = $result; } diff --git a/simpletest/ErrorTest.php b/simpletest/ErrorTest.php deleted file mode 100644 index 45361af..0000000 --- a/simpletest/ErrorTest.php +++ /dev/null @@ -1,102 +0,0 @@ -assert($error_callback_worked === 1); - - 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::removeHooks('run_failed'); - DB::addHook('run_failed', array($this, 'nonstatic_error_callback')); - DB::query("SELET * FROM accounts"); - $this->assert($nonstatic_error_callback_worked === 1); - DB::removeHooks('run_failed'); - } - - function test_2_exception_catch() { - $dbname = DB::$dbName; - try { - DB::query("SELET * FROM accounts"); - } catch(MeekroDBException $e) { - $this->assert(substr_count($e->getMessage(), 'You have an error in your SQL syntax')); - $this->assert($e->getQuery() === 'SELET * FROM accounts'); - $exception_was_caught = 1; - } - $this->assert($exception_was_caught === 1); - $this->assert(DB::lastQuery() === 'SELET * FROM accounts'); - - try { - DB::insert("`$dbname`.`accounts`", array( - 'id' => 2, - 'username' => 'Another Dude\'s \'Mom"', - 'password' => 'asdfsdse', - 'age' => 35, - 'height' => 555.23 - )); - } catch(MeekroDBException $e) { - $this->assert(substr_count($e->getMessage(), 'Duplicate entry')); - $exception_was_caught = 2; - } - $this->assert($exception_was_caught === 2); - } - - function test_3_success_handler() { - global $debug_callback_worked; - - 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; - - $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'); - } - -} - -?> diff --git a/simpletest/HookTest.php b/simpletest/HookTest.php new file mode 100644 index 0000000..bff7af4 --- /dev/null +++ b/simpletest/HookTest.php @@ -0,0 +1,187 @@ +assert($error_callback_worked === 1); + DB::removeHooks('run_failed'); + + DB::addHook('run_failed', array('HookTest', 'static_error_callback')); + DB::query("SELET * FROM accounts"); + $this->assert($static_error_callback_worked === 1); + 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); + DB::removeHooks('run_failed'); + } + + function test_2_exception_catch() { + $dbname = DB::$dbName; + try { + DB::query("SELET * FROM accounts"); + } catch(MeekroDBException $e) { + $this->assert(substr_count($e->getMessage(), 'You have an error in your SQL syntax')); + $this->assert($e->getQuery() === 'SELET * FROM accounts'); + $exception_was_caught = 1; + } + $this->assert($exception_was_caught === 1); + $this->assert(DB::lastQuery() === 'SELET * FROM accounts'); + + try { + DB::insert("`$dbname`.`accounts`", array( + 'id' => 2, + 'username' => 'Another Dude\'s \'Mom"', + 'password' => 'asdfsdse', + 'age' => 35, + 'height' => 555.23 + )); + } catch(MeekroDBException $e) { + $this->assert(substr_count($e->getMessage(), 'Duplicate entry')); + $exception_was_caught = 2; + } + $this->assert($exception_was_caught === 2); + } + + function test_3_success_handler() { + global $debug_callback_worked; + + 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; + + $error_handler = function($hash) { + global $anonymous_error_callback_worked; + if (substr_count($hash['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'); + } + + function test_5_post_run_success() { + $callback_worked = false; + + $fn = function($hash) use (&$callback_worked) { + if (!isset($hash['error']) && !isset($hash['exception'])) { + $callback_worked = true; + } + }; + + DB::addHook('post_run', $fn); + DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend"); + $this->assert($callback_worked); + DB::removeHooks('post_run'); + } + + function test_6_post_run_failed() { + $callback_worked = false; + + $fn = function($hash) use (&$callback_worked) { + if ($hash['error'] && $hash['exception']) { + $expected_query = "SELEC * FROM accounts WHERE username!='Charlie\'s Friend'"; + $expected_error = "error in your SQL syntax"; + if ($hash['exception']->getQuery() == $expected_query && substr_count($hash['error'], $expected_error)) { + $callback_worked = true; + } + } + }; + + DB::addHook('post_run', $fn); + DB::addHook('run_failed', function() { return false; }); // disable exception throwing + DB::query("SELEC * FROM accounts WHERE username!=%s", "Charlie's Friend"); + $this->assert($callback_worked); + DB::removeHooks('post_run'); + DB::removeHooks('run_failed'); + } + + function test_7_pre_run() { + $callback_worked = false; + + $fn = function($args) { return str_replace('SLCT', 'SELET', $args['query']); }; + $fn2 = function($args) { return str_replace('SELET', 'SELECT', $args['query']); }; + $fn3 = function($args) use (&$callback_worked) { $callback_worked = true; }; + + DB::addHook('pre_run', $fn); + DB::addHook('pre_run', $fn2); + $last_hook = DB::addHook('pre_run', $fn3); + $results = DB::query("SLCT * FROM accounts WHERE username!=%s", "Charlie's Friend"); + $this->assert(count($results) == 4); + $this->assert($callback_worked); + + $callback_worked = false; + DB::removeHook('pre_run', $last_hook); + $results = DB::query("SLCT * FROM accounts WHERE username!=%s", "Charlie's Friend"); + $this->assert(count($results) == 4); + $this->assert(!$callback_worked); + + DB::removeHooks('pre_run'); + } + + function test_8_pre_parse() { + $callback_worked = false; + + $fn = function($args) { + $args['query'] = str_replace('SLCT', 'SELECT', $args['query']); + return array($args['query'], $args['args']); + }; + $fn2 = function($args) { + $args['args'][0] = '1ofmany'; + return array($args['query'], $args['args']); + }; + $fn3 = function() use (&$callback_worked) { + $callback_worked = true; + }; + + DB::addHook('pre_parse', $fn); + DB::addHook('pre_parse', $fn2); + DB::addHook('pre_parse', $fn3); + $row = DB::queryFirstRow("SLCT * FROM accounts WHERE username=%s", "asdf"); + $this->assert($row['password'] == 'something'); + $this->assert($callback_worked); + DB::removeHooks('pre_parse'); + } + + + +} + +?> diff --git a/simpletest/test.php b/simpletest/test.php index 34ec038..321ab4e 100755 --- a/simpletest/test.php +++ b/simpletest/test.php @@ -35,7 +35,7 @@ require_once __DIR__ . '/WalkTest.php'; require_once __DIR__ . '/CallTest.php'; require_once __DIR__ . '/ObjectTest.php'; require_once __DIR__ . '/WhereClauseTest.php'; -require_once __DIR__ . '/ErrorTest.php'; +require_once __DIR__ . '/HookTest.php'; require_once __DIR__ . '/TransactionTest.php'; require_once __DIR__ . '/HelperTest.php'; @@ -45,7 +45,7 @@ $classes_to_test = array( 'CallTest', 'WhereClauseTest', 'ObjectTest', - 'ErrorTest', + 'HookTest', 'TransactionTest', 'HelperTest', );