12 Commits
v1.6 ... v1.7

Author SHA1 Message Date
Sergey Tsalkov
a1f01a4f87 few more fixes and tests for WhereClause 2011-09-23 22:03:15 -07:00
Sergey Tsalkov
80cc78edcd insertUpdate() can now handle a second array for the update component, or nothing, so it'll
re-use the insert array for the update component
2011-09-23 21:29:40 -07:00
Sergey Tsalkov
5050205e58 add insertUpdate() function for "insert ... on duplicate key update" behavior 2011-09-23 19:15:41 -07:00
Sergey Tsalkov
9f3aa571dc add some simple tests for the WhereClause system, which will now be official and documented 2011-09-23 18:44:34 -07:00
Sergey Tsalkov
a2800ef04b add insertIgnore() function -- pretty self explanatory 2011-09-23 16:11:10 -07:00
Sergey Tsalkov
164c7157a6 queryRaw() will now be officially supported, returns mysqli_result instead of array of assoc
arrays
2011-09-23 15:57:36 -07:00
Sergey Tsalkov
f09308a3f7 re-organize test files, make it easy to test for both php 5.2 and 5.3 2011-09-23 15:51:35 -07:00
Sergey Tsalkov
69bfe15ebb add test case regarding BLOBs 2011-08-25 23:54:52 -07:00
Sergey Tsalkov
7621b50a02 handle non-sql errors (such as failure to connect to MySQL server) in a user-defined way
adds DB::$nonsql_error_handler and DB::$throw_exception_on_nonsql_error
2011-08-25 23:34:37 -07:00
Sergey Tsalkov
4122fe52c1 collapse some redundant code into a function 2011-06-02 18:57:46 -04:00
Sergey Tsalkov
8072f6ab07 use static class variable, not static function variable, to hold mysql connection 2011-06-01 17:30:28 -04:00
Sergey Tsalkov
860da8d18a insert(), replace(), and update() now handle the value NULL correctly 2011-05-31 21:50:24 -04:00
8 changed files with 410 additions and 151 deletions

1
.gitignore vendored
View File

@@ -0,0 +1 @@
simpletest/test_setup.php

View File

@@ -19,6 +19,7 @@
class DB
{
public static $internal_mysql = null;
public static $insert_id = 0;
public static $num_rows = 0;
public static $affected_rows = 0;
@@ -33,24 +34,48 @@ class DB
public static $port = null;
public static $encoding = 'latin1';
public static $queryMode = 'queryAllRows';
public static $param_char = '%';
public static $success_handler = false;
public static $error_handler = true;
public static $throw_exception_on_error = false;
public static $param_char = '%';
public static $nonsql_error_handler = null;
public static $throw_exception_on_nonsql_error = false;
public static function get() {
static $mysql = null;
$mysql = DB::$internal_mysql;
if ($mysql == null) {
if (! DB::$port) DB::$port = ini_get('mysqli.default_port');
DB::$current_db = DB::$dbName;
$mysql = new mysqli(DB::$host, DB::$user, DB::$password, DB::$dbName, DB::$port);
if ($mysql->connect_error) {
DB::nonSQLError('Unable to connect to MySQL server! Error: ' . $mysql->connect_error);
}
$mysql->set_charset(DB::$encoding);
DB::$internal_mysql = $mysql;
}
return $mysql;
}
public static function nonSQLError($message) {
if (DB::$throw_exception_on_nonsql_error) {
$e = new MeekroDBException($message);
throw $e;
}
$error_handler = is_callable(DB::$nonsql_error_handler) ? DB::$nonsql_error_handler : 'meekrodb_error_handler';
call_user_func($error_handler, array(
'type' => 'nonsql',
'error' => $message
));
}
public static function debugMode($handler = true) {
DB::$success_handler = $handler;
}
@@ -64,7 +89,7 @@ class DB
public static function setDB($dbName) {
$db = DB::get();
DB::$old_db = DB::$current_db;
if (! $db->select_db($dbName)) die("unable to set db to $dbName");
if (! $db->select_db($dbName)) DB::nonSQLError("Unable to set database to $dbName");
DB::$current_db = $dbName;
}
@@ -86,6 +111,20 @@ class DB
return $db->real_escape_string($str);
}
public static function sanitize($value) {
if (is_object($value) && ($value instanceof MeekroDBEval)) {
$value = $value->text;
} else {
if (is_array($value) || is_object($value)) $value = serialize($value);
if (is_string($value)) $value = "'" . DB::escape($value) . "'";
else if (is_null($value)) $value = 'NULL';
else if (is_bool($value)) $value = ($value ? 1 : 0);
}
return $value;
}
private static function formatTableName($table) {
$table = str_replace('`', '', $table);
if (strpos($table, '.')) {
@@ -130,13 +169,7 @@ class DB
$buildquery = "UPDATE " . self::formatTableName($table) . " SET ";
$keyval = array();
foreach ($params as $key => $value) {
if (is_object($value) && ($value instanceof MeekroDBEval)) {
$value = $value->text;
} else {
if (is_array($value)) $value = serialize($value);
$value = (is_int($value) ? $value : "'" . DB::escape($value) . "'");
}
$value = DB::sanitize($value);
$keyval[] = "`" . $key . "`=" . $value;
}
@@ -145,7 +178,7 @@ class DB
call_user_func_array('DB::queryNull', $args);
}
public static function insertOrReplace($which, $table, $datas) {
public static function insertOrReplace($which, $table, $datas, $options=array()) {
$datas = unserialize(serialize($datas)); // break references within array
$keys = null;
@@ -165,14 +198,9 @@ class DB
$insert_values = array();
foreach ($keys as $key) {
if ($many && !isset($data[$key])) die("insert/replace many: each assoc array must have the same keys!");
if ($many && !isset($data[$key])) DB::nonSQLError('insert/replace many: each assoc array must have the same keys!');
$datum = $data[$key];
if (is_object($datum) && ($datum instanceof MeekroDBEval)) {
$datum = $datum->text;
} else {
$datum = (is_int($datum) ? $datum : "'" . DB::escape($datum) . "'");
}
$datum = DB::sanitize($datum);
$insert_values[] = $datum;
}
@@ -184,15 +212,47 @@ class DB
$keys_str = implode(', ', DB::wrapStr($keys, '`'));
$values_str = implode(',', $values);
DB::queryNull("$which INTO $table ($keys_str) VALUES $values_str");
if (isset($options['ignore']) && $options['ignore'] && strtolower($which) == 'insert') {
DB::queryNull("INSERT IGNORE INTO $table ($keys_str) VALUES $values_str");
} else if (isset($options['update']) && $options['update'] && strtolower($which) == 'insert') {
DB::queryNull("INSERT INTO $table ($keys_str) VALUES $values_str ON DUPLICATE KEY UPDATE {$options['update']}");
} else {
DB::queryNull("$which INTO $table ($keys_str) VALUES $values_str");
}
}
public static function insert($table, $data) {
return DB::insertOrReplace('INSERT', $table, $data);
}
public static function insert($table, $data) { return DB::insertOrReplace('INSERT', $table, $data); }
public static function insertIgnore($table, $data) { return DB::insertOrReplace('INSERT', $table, $data, array('ignore' => true)); }
public static function replace($table, $data) { return DB::insertOrReplace('REPLACE', $table, $data); }
public static function replace($table, $data) {
return DB::insertOrReplace('REPLACE', $table, $data);
public static function insertUpdate() {
$args = func_get_args();
$table = array_shift($args);
$data = array_shift($args);
if (! isset($args[0])) { // update will have all the data of the insert
if (isset($data[0]) && is_array($data[0])) { //multiple insert rows specified -- failing!
DB::nonSQLError("Badly formatted insertUpdate() query -- you didn't specify the update component!");
}
$args[0] = $data;
}
if (is_array($args[0])) {
$keyval = array();
foreach ($args[0] as $key => $value) {
$value = DB::sanitize($value);
$keyval[] = "`" . $key . "`=" . $value;
}
$updatestr = implode(', ', $keyval);
} else {
$updatestr = call_user_func_array('DB::parseQueryParams', $args);
}
return DB::insertOrReplace('INSERT', $table, $data, array('update' => $updatestr));
}
public static function delete() {
@@ -230,11 +290,11 @@ class DB
foreach ($args as $arg) {
$type = array_shift($types);
$pos = strpos($sql, '?');
if ($pos === false) die("Badly formatted SQL query: $sql");
if ($pos === false) DB::nonSQLError("Badly formatted SQL query: $sql");
if ($type == 's') $replacement = "'" . DB::escape($arg) . "'";
else if ($type == 'i') $replacement = intval($arg);
else die("Badly formatted SQL query: $sql");
else DB::nonSQLError("Badly formatted SQL query: $sql");
$sql = substr_replace($sql, $replacement, $pos, 1);
}
@@ -285,7 +345,7 @@ class DB
$result = "'%" . DB::escape(str_replace(array('%', '_'), array('\%', '\_'), $arg)) . "%'";
} else {
$array_type = true;
if (! is_array($arg)) die("Badly formatted SQL query: $sql -- expecting array, but didn't get one!");
if (! is_array($arg)) DB::nonSQLError("Badly formatted SQL query: $sql -- expecting array, but didn't get one!");
}
if ($type == 'ls') $result = DB::wrapStr($arg, "'", true);
@@ -293,7 +353,7 @@ class DB
else if ($type == 'ld') $result = array_map('floatval', $arg);
else if ($type == 'lb') $result = array_map('DB::formatTableName', $arg);
else if ($type == 'll') $result = $arg;
else if (! $result) die("Badly formatted SQL query: $sql");
else if (! $result) DB::nonSQLError("Badly formatted SQL query: $sql");
if (is_array($result)) {
if (! $array_type) $result = $result[0];
@@ -328,13 +388,16 @@ class DB
}
public static function queryNull() { $args = func_get_args(); return DB::prependCall('DB::queryHelper', $args, 'null'); }
public static function queryRaw() { $args = func_get_args(); return DB::prependCall('DB::queryHelper', $args, 'buffered'); }
public static function queryBuf() { $args = func_get_args(); return DB::prependCall('DB::queryHelper', $args, 'buffered'); }
public static function queryUnbuf() { $args = func_get_args(); return DB::prependCall('DB::queryHelper', $args, 'unbuffered'); }
public static function queryHelper() {
$args = func_get_args();
$type = array_shift($args);
if ($type != 'buffered' && $type != 'unbuffered' && $type != 'null') die("Error -- first argument to queryHelper must be buffered or unbuffered!");
if ($type != 'buffered' && $type != 'unbuffered' && $type != 'null') {
DB::nonSQLError('Error -- first argument to queryHelper must be buffered or unbuffered!');
}
$is_buffered = ($type == 'buffered');
$is_null = ($type == 'null');
@@ -351,6 +414,7 @@ class DB
$error_handler = is_callable(DB::$error_handler) ? DB::$error_handler : 'meekrodb_error_handler';
call_user_func($error_handler, array(
'type' => 'sql',
'query' => $sql,
'error' => $error
));
@@ -531,7 +595,9 @@ class WhereClause {
public $clauses = array();
function __construct($type) {
$this->type = strtolower($type);
$type = strtolower($type);
if ($type != 'or' && $type != 'and') DB::nonSQLError('you must use either WhereClause(and) or WhereClause(or)');
$this->type = $type;
}
function add() {
@@ -548,7 +614,13 @@ class WhereClause {
function negateLast() {
$i = count($this->clauses) - 1;
$this->clauses[$i] = 'NOT (' . $this->clauses[$i] . ')';
if (!isset($this->clauses[$i])) return;
if ($this->clauses[$i] instanceof WhereClause) {
$this->clauses[$i]->negate();
} else {
$this->clauses[$i] = 'NOT (' . $this->clauses[$i] . ')';
}
}
function negate() {
@@ -565,11 +637,8 @@ class WhereClause {
return count($this->clauses);
}
function text($minimal = false) {
if (count($this->clauses) == 0) {
if ($minimal) return '(1)';
else return '';
}
function text() {
if (count($this->clauses) == 0) return '(1)';
$A = array();
foreach ($this->clauses as $clause) {
@@ -615,8 +684,8 @@ class MeekroDBException extends Exception {
}
function meekrodb_error_handler($params) {
$out[] = "QUERY: " . $params['query'];
$out[] = "ERROR: " . $params['error'];
if (isset($params['query'])) $out[] = "QUERY: " . $params['query'];
if (isset($params['error'])) $out[] = "ERROR: " . $params['error'];
$out[] = "";
if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {

View File

@@ -1,26 +1,9 @@
<?php
function new_error_callback($params) {
global $error_callback_worked;
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $error_callback_worked = 1;
}
function my_debug_handler($params) {
global $debug_callback_worked;
if (substr_count($params['query'], 'SELECT')) $debug_callback_worked = 1;
}
class BasicTest extends SimpleTest {
function __construct() {
error_reporting(E_ALL);
require_once '../db.class.php';
DB::$user = 'libdb_user';
DB::$password = 'sdf235sklj';
DB::$dbName = 'libdb_test';
DB::query("DROP DATABASE libdb_test");
DB::query("CREATE DATABASE libdb_test");
DB::useDB('libdb_test');
foreach (DB::tableList() as $table) {
DB::query("DROP TABLE $table");
}
}
@@ -30,7 +13,8 @@ class BasicTest extends SimpleTest {
`username` VARCHAR( 255 ) NOT NULL ,
`password` VARCHAR( 255 ) NOT NULL ,
`age` INT NOT NULL DEFAULT '10',
`height` DOUBLE NOT NULL DEFAULT '10.0'
`height` DOUBLE NOT NULL DEFAULT '10.0',
`favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi'
) ENGINE = InnoDB");
}
@@ -73,12 +57,13 @@ class BasicTest extends SimpleTest {
'age' => 15,
'height' => 10.371
));
DB::insert('`libdb_test`.`accounts`', array(
$dbname = DB::$dbName;
DB::insert("`$dbname`.`accounts`", array(
'username' => 'Charlie\'s Friend',
'password' => 'goodbye',
'age' => 30,
'height' => 155.23
'height' => 155.23,
'favorite_word' => null,
));
$this->assert(DB::insertId() === 3);
@@ -86,6 +71,9 @@ class BasicTest extends SimpleTest {
$counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
$this->assert($counter === strval(3));
$password = DB::queryFirstField("SELECT password FROM accounts WHERE favorite_word IS NULL");
$this->assert($password === 'goodbye');
DB::$param_char = '###';
$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'));
@@ -109,6 +97,12 @@ class BasicTest extends SimpleTest {
$this->assert($username === 'Bart');
$this->assert($password === 'hello');
$this->assert($age == 15);
$mysqli_result = DB::queryRaw("SELECT * FROM accounts WHERE favorite_word IS NULL");
$this->assert($mysqli_result instanceof MySQLi_Result);
$row = $mysqli_result->fetch_assoc();
$this->assert($row['password'] === 'goodbye');
$this->assert($mysqli_result->fetch_assoc() === null);
}
function test_4_query() {
@@ -120,7 +114,7 @@ class BasicTest extends SimpleTest {
$this->assert(count($results) === 2);
$columnlist = DB::columnList('accounts');
$this->assert(count($columnlist) === 5);
$this->assert(count($columnlist) === 6);
$this->assert($columnlist[0] === 'id');
$this->assert($columnlist[4] === 'height');
@@ -129,7 +123,7 @@ class BasicTest extends SimpleTest {
$this->assert($tablelist[0] === 'accounts');
$tablelist = null;
$tablelist = DB::tableList('libdb_test');
$tablelist = DB::tableList(DB::$dbName);
$this->assert(count($tablelist) === 1);
$this->assert($tablelist[0] === 'accounts');
}
@@ -148,11 +142,13 @@ class BasicTest extends SimpleTest {
DB::update('accounts', array(
'password' => DB::sqleval("REPEAT('blah', %i)", 4),
'favorite_word' => null,
), 'username=%s', 'newguy');
$row = null;
$row = DB::queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy');
$this->assert($row['password'] === 'blahblahblahblah');
$this->assert($row['favorite_word'] === null);
DB::query("DELETE FROM accounts WHERE password=%s", 'blahblahblahblah');
$this->assert(DB::affectedRows() === 1);
@@ -203,77 +199,107 @@ class BasicTest extends SimpleTest {
}
function test_5_error_handler() {
global $error_callback_worked, $static_error_callback_worked, $nonstatic_error_callback_worked,
$anonymous_error_callback_worked;
function test_5_insert_blobs() {
DB::query("CREATE TABLE `storedata` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`picture` BLOB
) ENGINE = InnoDB");
DB::$error_handler = 'new_error_callback';
DB::query("SELET * FROM accounts");
$this->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);
$smile = file_get_contents('smile1.jpg');
DB::insert('storedata', array(
'picture' => $smile,
));
DB::query("INSERT INTO storedata (picture) VALUES (%s)", $smile);
DB::query("INSERT INTO storedata (picture) VALUES (?)", 's', $smile);
$getsmile = DB::queryFirstField("SELECT picture FROM storedata WHERE id=1");
$getsmile2 = DB::queryFirstField("SELECT picture FROM storedata WHERE id=2");
$getsmile3 = DB::queryFirstField("SELECT picture FROM storedata WHERE id=3");
$this->assert($smile === $getsmile);
$this->assert($smile === $getsmile2);
$this->assert($smile === $getsmile3);
}
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;
function test_6_insert_ignore() {
DB::insertIgnore('accounts', array(
'id' => 1, //duplicate primary key
'username' => 'gonesoon',
'password' => 'something',
'age' => 61,
'height' => 199.194
));
}
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() {
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);
function test_7_insert_update() {
DB::insertUpdate('accounts', array(
'id' => 2, //duplicate primary key
'username' => 'gonesoon',
'password' => 'something',
'age' => 61,
'height' => 199.194
), 'age = age + %i', 1);
try {
DB::insert('`libdb_test`.`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;
$this->assert(DB::affectedRows() === 2); // a quirk of MySQL, even though only 1 row was updated
DB::debugMode('my_debug_handler');
DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
$result = DB::query("SELECT * FROM accounts WHERE age = %i", 16);
$this->assert(count($result) === 1);
$this->assert($result[0]['height'] === '10.371');
$this->assert($debug_callback_worked === 1);
DB::insertUpdate('accounts', array(
'id' => 2, //duplicate primary key
'username' => 'blahblahdude',
'age' => 233,
'height' => 199.194
));
DB::debugMode(false);
$result = DB::query("SELECT * FROM accounts WHERE age = %i", 233);
$this->assert(count($result) === 1);
$this->assert($result[0]['height'] === '199.194');
$this->assert($result[0]['username'] === 'blahblahdude');
DB::insertUpdate('accounts', array(
'id' => 2, //duplicate primary key
'username' => 'gonesoon',
'password' => 'something',
'age' => 61,
'height' => 199.194
), array(
'age' => 74,
));
$result = DB::query("SELECT * FROM accounts WHERE age = %i", 74);
$this->assert(count($result) === 1);
$this->assert($result[0]['height'] === '199.194');
$this->assert($result[0]['username'] === 'blahblahdude');
$multiples[] = array(
'id' => 3, //duplicate primary key
'username' => 'gonesoon',
'password' => 'something',
'age' => 61,
'height' => 199.194
);
$multiples[] = array(
'id' => 1, //duplicate primary key
'username' => 'gonesoon',
'password' => 'something',
'age' => 61,
'height' => 199.194
);
DB::insertUpdate('accounts', $multiples, array('age' => 914));
$this->assert(DB::affectedRows() === 4);
$result = DB::query("SELECT * FROM accounts WHERE age=914 ORDER BY id ASC");
$this->assert(count($result) === 2);
$this->assert($result[0]['username'] === 'Abe');
$this->assert($result[1]['username'] === 'Charlie\'s Friend');
DB::query("UPDATE accounts SET age=15, username='Bart' WHERE age=%i", 74);
$this->assert(DB::affectedRows() === 1);
}
}

83
simpletest/ErrorTest.php Normal file
View File

@@ -0,0 +1,83 @@
<?
function new_error_callback($params) {
global $error_callback_worked;
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $error_callback_worked = 1;
}
function my_debug_handler($params) {
global $debug_callback_worked;
if (substr_count($params['query'], 'SELECT')) $debug_callback_worked = 1;
}
class ErrorTest extends SimpleTest {
function test_1_error_handler() {
global $error_callback_worked, $static_error_callback_worked, $nonstatic_error_callback_worked;
DB::$error_handler = 'new_error_callback';
DB::query("SELET * FROM accounts");
$this->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);
}
}
?>

View File

@@ -0,0 +1,19 @@
<?
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

@@ -0,0 +1,64 @@
<?
class WhereClauseTest extends SimpleTest {
function test_1_basic_where() {
$where = new WhereClause('and');
$where->add('username=%s', 'Bart');
$where->add('password=%s', 'hello');
$result = DB::query("SELECT * FROM accounts WHERE %l", $where->text());
$this->assert(count($result) === 1);
$this->assert($result[0]['age'] === '15');
}
function test_2_simple_grouping() {
$where = new WhereClause('and');
$where->add('password=%s', 'hello');
$subclause = $where->addClause('or');
$subclause->add('age=%i', 15);
$subclause->add('age=%i', 14);
$result = DB::query("SELECT * FROM accounts WHERE %l", $where->text());
$this->assert(count($result) === 1);
$this->assert($result[0]['age'] === '15');
}
function test_3_negate_last() {
$where = new WhereClause('and');
$where->add('password=%s', 'hello');
$subclause = $where->addClause('or');
$subclause->add('username!=%s', 'Bart');
$subclause->negateLast();
$result = DB::query("SELECT * FROM accounts WHERE %l", $where->text());
$this->assert(count($result) === 1);
$this->assert($result[0]['age'] === '15');
}
function test_4_negate_last_query() {
$where = new WhereClause('and');
$where->add('password=%s', 'hello');
$subclause = $where->addClause('or');
$subclause->add('username!=%s', 'Bart');
$where->negateLast();
$result = DB::query("SELECT * FROM accounts WHERE %l", $where->text());
$this->assert(count($result) === 1);
$this->assert($result[0]['age'] === '15');
}
function test_5_negate() {
$where = new WhereClause('and');
$where->add('password=%s', 'hello');
$subclause = $where->addClause('or');
$subclause->add('username!=%s', 'Bart');
$subclause->negate();
$result = DB::query("SELECT * FROM accounts WHERE %l", $where->text());
$this->assert(count($result) === 1);
$this->assert($result[0]['age'] === '15');
}
}
?>

BIN
simpletest/smile1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

View File

@@ -11,37 +11,34 @@ 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;
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 'WhereClauseTest.php';
require_once 'ErrorTest.php';
$classes_to_test = array(
'BasicTest',
'WhereClauseTest',
'ErrorTest',
);
if ($is_php_53) {
require_once 'ErrorTest_53.php';
$classes_to_test[] = 'ErrorTest_53';
}
foreach ($classes_to_test as $class) {