Commit a29fafb5 authored by Taddeus Kroes's avatar Taddeus Kroes

added more tests for SQL plugin

parent 2f4f7391
...@@ -66,12 +66,19 @@ class pQuerySql extends pQuery implements pQueryExtension { ...@@ -66,12 +66,19 @@ class pQuerySql extends pQuery implements pQueryExtension {
if( !count($args) ) if( !count($args) )
return; return;
// Parse arguments as variables. Arrays and
// Replace variable indices by names equal to their indices // Replace variable indices by names equal to their indices
if( !is_array($args[0]) ) $variables = array();
array_unshift($args, null);
foreach( $args as $i => $argument ) {
if( is_array($argument) )
$variables = array_merge($variables, $argument);
else
$variables[$i] = $argument;
}
// Replace variables by their escaped values // Replace variables by their escaped values
$this->set($args); $this->set($variables);
} }
/** /**
...@@ -133,7 +140,7 @@ class pQuerySql extends pQuery implements pQueryExtension { ...@@ -133,7 +140,7 @@ class pQuerySql extends pQuery implements pQueryExtension {
$result = mysql_query($this->query, self::$link); $result = mysql_query($this->query, self::$link);
if( !$result ) if( !$result )
return self::mysql_error(); return self::mysql_error($this->query);
$this->result = $result; $this->result = $result;
$this->executed = true; $this->executed = true;
...@@ -210,10 +217,7 @@ class pQuerySql extends pQuery implements pQueryExtension { ...@@ -210,10 +217,7 @@ class pQuerySql extends pQuery implements pQueryExtension {
*/ */
static function set_login_data($host, $username, $password, $dbname) { static function set_login_data($host, $username, $password, $dbname) {
// Close any existing connection // Close any existing connection
if( self::$link ) { self::disconnect();
mysql_close(self::$link);
self::$link = null;
}
self::$login_data = array_merge(self::$login_data, self::$login_data = array_merge(self::$login_data,
compact('host', 'username', 'password', 'dbname')); compact('host', 'username', 'password', 'dbname'));
...@@ -224,16 +228,16 @@ class pQuerySql extends pQuery implements pQueryExtension { ...@@ -224,16 +228,16 @@ class pQuerySql extends pQuery implements pQueryExtension {
*/ */
static function assert_login_data_exist() { static function assert_login_data_exist() {
if( !isset(self::$login_data['host']) ) if( !isset(self::$login_data['host']) )
return self::error('No SQL host specified.'); return self::error('No MySQL database server host is specified.');
if( !isset(self::$login_data['username']) ) if( !isset(self::$login_data['username']) )
return self::error('No SQL username specified.'); return self::error('No username is specified for the MySQL server.');
if( !isset(self::$login_data['password']) ) if( !isset(self::$login_data['password']) )
return self::error('No SQL password specified.'); return self::error('No password is specified for the MySQL server.');
if( !isset(self::$login_data['host']) ) if( !isset(self::$login_data['host']) )
return self::error('No SQL host specified.'); return self::error('No MySQL database name is specified.');
} }
/** /**
...@@ -262,24 +266,44 @@ class pQuerySql extends pQuery implements pQueryExtension { ...@@ -262,24 +266,44 @@ class pQuerySql extends pQuery implements pQueryExtension {
return self::mysql_error(); return self::mysql_error();
} }
/**
* Close the current connection, if any.
*
* @uses mysql_close
*/
static function disconnect() {
// Return if the connection has already been closed
if( !self::$link )
return;
mysql_close(self::$link);
self::$link = null;
}
/** /**
* Echo the latest MySQL error. * Echo the latest MySQL error.
* If a query is specified and debug mode is on, add the query to the error message.
*
* @param string $query The query that was executed, if any.
*/ */
static function mysql_error() { static function mysql_error($query='') {
self::error('MySQL error %d: %s.', mysql_errno(), mysql_error()); $error = sprintf('MySQL error %d: %s.', mysql_errno(), mysql_error());
PQUERY_DEBUG && $error .= "\nQuery: ".$this->query;
self::error($error);
} }
/** /**
* Extention of {@link pQuery::error}, returning FALSE (useful in result loops). * Extention of {@link pQuery::error}, returning FALSE (useful in result loops).
* Also, the current query is printed in debug mode. * Also, the current query is printed in debug mode.
* *
* @param string $error The error message
* @returns bool FALSE * @returns bool FALSE
*/ */
static function error() { static function error($error /* [ , $arg1 [ , ... ] ] */) {
parent::error('MySQL error %d: %s.', mysql_errno(), mysql_error()); $args = func_get_args();
call_user_func_array('pQuery::error', $args);
if( PQUERY_DEBUG ) //parent::error('SQL error %d: %s.', mysql_errno(), mysql_error());
echo $this->query;
return false; return false;
} }
......
<?php <?php
__p::load_plugin('sql'); __p::load_plugin('sql');
include '../../debug.php';
class pQuerySqlTest extends UnitTestCase { class pQuerySqlTest extends UnitTestCase {
function __construct() { function __construct() {
...@@ -8,7 +9,12 @@ class pQuerySqlTest extends UnitTestCase { ...@@ -8,7 +9,12 @@ class pQuerySqlTest extends UnitTestCase {
} }
function setUp() { function setUp() {
__sql::set_login_data('localhost', 'root', '', 'pquery_test');
}
function tearDown() {
__sql::disconnect();
__sql::$login_data = array();
} }
function test_set_login_data() { function test_set_login_data() {
...@@ -22,7 +28,6 @@ class pQuerySqlTest extends UnitTestCase { ...@@ -22,7 +28,6 @@ class pQuerySqlTest extends UnitTestCase {
function test_no_login_data() { function test_no_login_data() {
$this->expectException('pQueryException'); $this->expectException('pQueryException');
__sql::$login_data = array();
__sql::assert_login_data_exist(); __sql::assert_login_data_exist();
} }
...@@ -33,6 +38,7 @@ class pQuerySqlTest extends UnitTestCase { ...@@ -33,6 +38,7 @@ class pQuerySqlTest extends UnitTestCase {
} }
function test_variable_query() { function test_variable_query() {
self::set_login_data();
$sql = _sql("select id from foo where bar = '[bar]'") $sql = _sql("select id from foo where bar = '[bar]'")
->set(array('bar' => 'test1')); ->set(array('bar' => 'test1'));
$this->assertEqual($sql->query, "select id from foo where bar = 'test1'"); $this->assertEqual($sql->query, "select id from foo where bar = 'test1'");
...@@ -45,12 +51,26 @@ class pQuerySqlTest extends UnitTestCase { ...@@ -45,12 +51,26 @@ class pQuerySqlTest extends UnitTestCase {
} }
function test_escaped_query() { function test_escaped_query() {
self::set_login_data();
$sql = _sql("select id from foo where bar = '[bar]'") $sql = _sql("select id from foo where bar = '[bar]'")
->set(array('bar' => "select id from foo where bar = 'test1'")); ->set(array('bar' => "select id from foo where bar = 'test1'"));
$this->assertNotEqual($sql->query, "select id from foo where bar = 'select id from foo where bar = 'test1''"); $this->assertNotEqual($sql->query, "select id from foo where bar = 'select id from foo where bar = 'test1''");
} }
function test_constructor_simple() {
self::set_login_data();
$sql = _sql("select id from foo where bar = '[0]'", 'test1');
$this->assertEqual($sql->query, "select id from foo where bar = 'test1'");
}
function test_constructor_advanced() {
self::set_login_data();
$sql = _sql("[0] [bar] [foo] [2]", '1', array('bar' => '2', 'foo' => '3'), '4');
$this->assertEqual($sql->query, "1 2 3 4");
}
function test_select_simple() { function test_select_simple() {
self::set_login_data();
$sql = _sql("select bar from foo where id = 1"); $sql = _sql("select bar from foo where id = 1");
$result = $sql->fetch('object'); $result = $sql->fetch('object');
$this->assertEqual($result->bar, 'test1'); $this->assertEqual($result->bar, 'test1');
...@@ -58,9 +78,14 @@ class pQuerySqlTest extends UnitTestCase { ...@@ -58,9 +78,14 @@ class pQuerySqlTest extends UnitTestCase {
} }
function test_result_count() { function test_result_count() {
__sql::set_login_data('localhost', 'root', '', 'pquery_test');
$sql = _sql("select bar from foo where id in (1, 2)"); $sql = _sql("select bar from foo where id in (1, 2)");
$this->assertEqual($sql->result_count(), 2); $this->assertEqual($sql->result_count(), 2);
} }
static function set_login_data() {
__sql::set_login_data('localhost', 'root', '', 'pquery_test');
}
} }
?> ?>
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment