add some simple tests for the WhereClause system, which will now be official and documented
This commit is contained in:
13
db.class.php
13
db.class.php
@@ -62,7 +62,7 @@ class DB
|
||||
return $mysql;
|
||||
}
|
||||
|
||||
protected static function nonSQLError($message) {
|
||||
public static function nonSQLError($message) {
|
||||
if (DB::$throw_exception_on_nonsql_error) {
|
||||
$e = new MeekroDBException($message);
|
||||
throw $e;
|
||||
@@ -564,7 +564,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() {
|
||||
@@ -598,11 +600,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) {
|
||||
|
||||
28
simpletest/WhereClauseTest.php
Normal file
28
simpletest/WhereClauseTest.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?
|
||||
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');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -27,10 +27,12 @@ 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',
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user