re-organize test files, make it easy to test for both php 5.2 and 5.3
This commit is contained in:
@@ -1,28 +1,6 @@
|
||||
<?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 = 'meekrodb_test_us';
|
||||
|
||||
include 'test_setup.php'; //test config values go here
|
||||
DB::$password = $set_password;
|
||||
DB::$dbName = $set_db;
|
||||
DB::$host = $set_host;
|
||||
|
||||
|
||||
foreach (DB::tableList() as $table) {
|
||||
DB::query("DROP TABLE $table");
|
||||
}
|
||||
@@ -215,81 +193,9 @@ 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;
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
83
simpletest/ErrorTest.php
Normal file
83
simpletest/ErrorTest.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
19
simpletest/ErrorTest_53.php
Normal file
19
simpletest/ErrorTest_53.php
Normal 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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user