diff --git a/db.class.php b/db.class.php index 6a90059..168f4bc 100644 --- a/db.class.php +++ b/db.class.php @@ -42,7 +42,6 @@ class DB public static function useDB() { return call_user_func_array('DB::setDB', func_get_args()); } public static function setDB($dbName, $limit=0) { - if (DB::$current_db == $dbName) return true; $db = DB::get(); DB::$old_db = DB::$current_db; if (! $db->select_db($dbName)) die("unable to set db to $dbName"); @@ -74,6 +73,7 @@ class DB } private static function formatTableName($table) { + $table = str_replace('`', '', $table); if (strpos($table, '.')) { list($table_db, $table_table) = explode('.', $table, 2); $table = "`$table_db`.`$table_table`"; diff --git a/simpletest/BasicTest.php b/simpletest/BasicTest.php new file mode 100644 index 0000000..1f9fd95 --- /dev/null +++ b/simpletest/BasicTest.php @@ -0,0 +1,89 @@ +assert($counter === strval(0)); + + $row = DB::queryFirstRow("SELECT * FROM accounts"); + $this->assert($row === null); + + $field = DB::queryFirstField("SELECT * FROM accounts"); + $this->assert($field === null); + + $field = DB::queryOneField('nothere', "SELECT * FROM accounts"); + $this->assert($field === null); + + $column = DB::queryFirstColumn("SELECT * FROM accounts"); + $this->assert(is_array($column) && count($column) === 0); + + $column = DB::queryOneColumn('nothere', "SELECT * FROM accounts"); //TODO: is this what we want? + $this->assert(is_array($column) && count($column) === 0); + } + + function test_2_insert_row() { + DB::insert('accounts', array( + 'username' => 'Abe', + 'password' => 'hello' + )); + + $this->assert(DB::affectedRows() === 1); + + $counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts"); + $this->assert($counter === strval(1)); + } + + function test_3_more_inserts() { + DB::insert('`accounts`', array( + 'username' => 'Bart', + 'password' => 'hello', + 'age' => 15, + 'height' => 10.371 + )); + + DB::insert('`libdb_test`.`accounts`', array( + 'username' => 'Charlie\'s Friend', + 'password' => 'goodbye', + 'age' => 30, + 'height' => 155.23 + )); + + $counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts"); + $this->assert($counter === strval(3)); + + $bart = DB::queryFirstRow("SELECT * FROM accounts WHERE age IN %li AND height IN %ld AND username IN %ls", + array(15, 25), array(10.371, 150.123), array('Bart', 'Barts')); + $this->assert($bart['username'] === 'Bart'); + + $charlie_password = DB::queryFirstField("SELECT password FROM accounts WHERE username IN %ls AND username = %s", + array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend'); + $this->assert($charlie_password === 'goodbye'); + + + + } + +} + + +?> diff --git a/simpletest/test.php b/simpletest/test.php new file mode 100755 index 0000000..5dd5d39 --- /dev/null +++ b/simpletest/test.php @@ -0,0 +1,60 @@ +#!/usr/bin/php +fail(); + } + + protected function fail($msg = '') { + echo "FAILURE! $msg\n"; + debug_print_backtrace(); + 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/$filename"; + } + return $A; + } + + + +} + +$files = SimpleTest::__listfiles(__DIR__, '/^.*php$/i'); + +$classes_to_test = array(); +foreach ($files as $fullpath) { + $filename = basename($fullpath); + if ($fullpath == __FILE__) continue; + + require_once($fullpath); + $classes_to_test[] = str_replace('.php', '', $filename); +} + +foreach ($classes_to_test as $class) { + $object = new $class(); + + foreach (get_class_methods($object) as $method) { + if (substr($method, 0, 2) == '__') continue; + echo "Running $class::$method..\n"; + $object->$method(); + } +} + + + +?>