From f09308a3f75432e396ccd56e7f54a4e10060ebef Mon Sep 17 00:00:00 2001 From: Sergey Tsalkov Date: Fri, 23 Sep 2011 15:51:35 -0700 Subject: [PATCH] re-organize test files, make it easy to test for both php 5.2 and 5.3 --- simpletest/BasicTest.php | 96 +------------------------------------ simpletest/ErrorTest.php | 83 ++++++++++++++++++++++++++++++++ simpletest/ErrorTest_53.php | 19 ++++++++ simpletest/test.php | 50 +++++++++---------- 4 files changed, 125 insertions(+), 123 deletions(-) create mode 100644 simpletest/ErrorTest.php create mode 100644 simpletest/ErrorTest_53.php diff --git a/simpletest/BasicTest.php b/simpletest/BasicTest.php index 3d30f29..201113f 100644 --- a/simpletest/BasicTest.php +++ b/simpletest/BasicTest.php @@ -1,28 +1,6 @@ assert($error_callback_worked === 1); - - DB::$error_handler = array('BasicTest', 'static_error_callback'); - DB::query("SELET * FROM accounts"); - $this->assert($static_error_callback_worked === 1); - - DB::$error_handler = array($this, 'nonstatic_error_callback'); - DB::query("SELET * FROM accounts"); - $this->assert($nonstatic_error_callback_worked === 1); - - 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); - - } - 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; - } - - function test_6_exception_catch() { - $dbname = DB::$dbName; - DB::$error_handler = ''; - DB::$throw_exception_on_error = true; - 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); - - 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_7_debugmode_handler() { - global $debug_callback_worked; - - DB::debugMode('my_debug_handler'); - DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend"); - - $this->assert($debug_callback_worked === 1); - - DB::debugMode(false); - } - - function test_8_insert_blobs() { + function test_5_insert_blobs() { DB::query("CREATE TABLE `storedata` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `picture` BLOB diff --git a/simpletest/ErrorTest.php b/simpletest/ErrorTest.php new file mode 100644 index 0000000..d95b0c9 --- /dev/null +++ b/simpletest/ErrorTest.php @@ -0,0 +1,83 @@ +assert($error_callback_worked === 1); + + DB::$error_handler = 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::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; + } + + 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) { + $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); + + 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_debugmode_handler() { + global $debug_callback_worked; + + DB::debugMode('my_debug_handler'); + DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend"); + + $this->assert($debug_callback_worked === 1); + + DB::debugMode(false); + } + +} + +?> diff --git a/simpletest/ErrorTest_53.php b/simpletest/ErrorTest_53.php new file mode 100644 index 0000000..1b1f18a --- /dev/null +++ b/simpletest/ErrorTest_53.php @@ -0,0 +1,19 @@ +assert($anonymous_error_callback_worked === 1); + + } + + +} + +?> diff --git a/simpletest/test.php b/simpletest/test.php index bb76abe..17dfd72 100755 --- a/simpletest/test.php +++ b/simpletest/test.php @@ -11,38 +11,32 @@ class SimpleTest { die; } - public static function __listfiles($dir, $regex, $type='files', $rec = false) { - $A = array(); - - if (! $dir_handler = @opendir($dir)) return $A; - - while (false !== ($filename = @readdir($dir_handler))) { - if ($filename == '.' || $filename == '..') continue; - if ($rec && is_dir("$dir/$filename")) $A = array_merge($A, File::listfiles("$dir/$filename", $regex, $type, true)); - - if (! preg_match($regex, $filename)) continue; - if ($type == 'files' && ! is_file("$dir/$filename")) continue; - if ($type == 'dirs' && ! is_dir("$dir/$filename")) continue; - if ($type == 'symlinks' && ! is_link("$dir/$filename")) continue; - - $A[] = $dir . DIRECTORY_SEPARATOR . $filename; - } - return $A; - } - - } -$files = SimpleTest::__listfiles(dirname(__FILE__), '/^.*php$/i'); +if (phpversion() >= '5.3') $is_php_53 = true; +else $is_php_53 = false; -$classes_to_test = array(); -foreach ($files as $fullpath) { - $filename = basename($fullpath); - if ($fullpath == __FILE__) continue; - if ($filename == 'test_setup.php') continue; - require_once($fullpath); - $classes_to_test[] = str_replace('.php', '', $filename); +error_reporting(E_ALL); +require_once '../db.class.php'; +DB::$user = 'meekrodb_test_us'; + +include 'test_setup.php'; //test config values go here +DB::$password = $set_password; +DB::$dbName = $set_db; +DB::$host = $set_host; + +require_once 'BasicTest.php'; +require_once 'ErrorTest.php'; + +$classes_to_test = array( + 'BasicTest', + 'ErrorTest', +); + +if ($is_php_53) { + require_once 'ErrorTest_53.php'; + $classes_to_test[] = 'ErrorTest_53'; } foreach ($classes_to_test as $class) {