Selaa lähdekoodia

added more tests for SQL plugin

Taddeus Kroes 14 vuotta sitten
vanhempi
sitoutus
a29fafb504
2 muutettua tiedostoa jossa 70 lisäystä ja 21 poistoa
  1. 43 19
      pquery.sql.php
  2. 27 2
      test/test_sql.php

+ 43 - 19
pquery.sql.php

@@ -66,12 +66,19 @@ class pQuerySql extends pQuery implements pQueryExtension {
 		if( !count($args) )
 			return;
 		
+		// Parse arguments as variables. Arrays and 
 		// Replace variable indices by names equal to their indices
-		if( !is_array($args[0]) )
-			array_unshift($args, null);
+		$variables = array();
+		
+		foreach( $args as $i => $argument ) {
+			if( is_array($argument) )
+				$variables = array_merge($variables, $argument);
+			else
+				$variables[$i] = $argument;
+		}
 		
 		// Replace variables by their escaped values
-		$this->set($args);
+		$this->set($variables);
 	}
 	
 	/**
@@ -133,7 +140,7 @@ class pQuerySql extends pQuery implements pQueryExtension {
 		$result = mysql_query($this->query, self::$link);
 		
 		if( !$result )
-			return self::mysql_error();
+			return self::mysql_error($this->query);
 		
 		$this->result = $result;
 		$this->executed = true;
@@ -210,10 +217,7 @@ class pQuerySql extends pQuery implements pQueryExtension {
 	 */
 	static function set_login_data($host, $username, $password, $dbname) {
 		// Close any existing connection
-		if( self::$link ) {
-			mysql_close(self::$link);
-			self::$link = null;
-		}
+		self::disconnect();
 		
 		self::$login_data = array_merge(self::$login_data,
 			compact('host', 'username', 'password', 'dbname'));
@@ -224,16 +228,16 @@ class pQuerySql extends pQuery implements pQueryExtension {
 	 */
 	static function assert_login_data_exist() {
 		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']) )
-			return self::error('No SQL username specified.');
+			return self::error('No username is specified for the MySQL server.');
 		
 		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']) )
-			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 {
 			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.
+	 * 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() {
-		self::error('MySQL error %d: %s.', mysql_errno(), mysql_error());
+	static function mysql_error($query='') {
+		$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).
 	 * Also, the current query is printed in debug mode.
 	 * 
+	 * @param string $error The error message
 	 * @returns bool FALSE
 	 */
-	static function error() {
-		parent::error('MySQL error %d: %s.', mysql_errno(), mysql_error());
-		
-		if( PQUERY_DEBUG )
-			echo $this->query;
+	static function error($error /* [ , $arg1 [ , ... ] ] */) {
+		$args = func_get_args();
+		call_user_func_array('pQuery::error', $args);
+		//parent::error('SQL error %d: %s.', mysql_errno(), mysql_error());
 		
 		return false;
 	}

+ 27 - 2
test/test_sql.php

@@ -1,6 +1,7 @@
 <?php
 
 __p::load_plugin('sql');
+include '../../debug.php';
 
 class pQuerySqlTest extends UnitTestCase {
 	function __construct() {
@@ -8,7 +9,12 @@ class pQuerySqlTest extends UnitTestCase {
 	}
 	
 	function setUp() {
-		__sql::set_login_data('localhost', 'root', '', 'pquery_test');
+		
+	}
+	
+	function tearDown() {
+		__sql::disconnect();
+		__sql::$login_data = array();
 	}
 	
 	function test_set_login_data() {
@@ -22,7 +28,6 @@ class pQuerySqlTest extends UnitTestCase {
 	
 	function test_no_login_data() {
 		$this->expectException('pQueryException');
-		__sql::$login_data = array();
 		__sql::assert_login_data_exist();
 	}
 	
@@ -33,6 +38,7 @@ class pQuerySqlTest extends UnitTestCase {
 	}
 	
 	function test_variable_query() {
+		self::set_login_data();
 		$sql = _sql("select id from foo where bar = '[bar]'")
 					->set(array('bar' => 'test1'));
 		$this->assertEqual($sql->query, "select id from foo where bar = 'test1'");
@@ -45,12 +51,26 @@ class pQuerySqlTest extends UnitTestCase {
 	}
 	
 	function test_escaped_query() {
+		self::set_login_data();
 		$sql = _sql("select id from foo where bar = '[bar]'")
 					->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''");
 	}
 	
+	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() {
+		self::set_login_data();
 		$sql = _sql("select bar from foo where id = 1");
 		$result = $sql->fetch('object');
 		$this->assertEqual($result->bar, 'test1');
@@ -58,9 +78,14 @@ class pQuerySqlTest extends UnitTestCase {
 	}
 	
 	function test_result_count() {
+		__sql::set_login_data('localhost', 'root', '', 'pquery_test');
 		$sql = _sql("select bar from foo where id in (1, 2)");
 		$this->assertEqual($sql->result_count(), 2);
 	}
+	
+	static function set_login_data() {
+		__sql::set_login_data('localhost', 'root', '', 'pquery_test');
+	}
 }
 
 ?>