Browse Source

Adding __debugInfo to Database\Query

Jose Lorenzo Rodriguez 12 years ago
parent
commit
b735c5dc30
2 changed files with 53 additions and 1 deletions
  1. 17 1
      src/Database/Query.php
  2. 36 0
      tests/TestCase/Database/QueryTest.php

+ 17 - 1
src/Database/Query.php

@@ -204,7 +204,7 @@ class Query implements ExpressionInterface, IteratorAggregate {
 		$query->_bindStatement($statement);
 		$statement->execute();
 
-		return $query->_decorateStatement($statement);
+		return $this->_iterator = $query->_decorateStatement($statement);
 	}
 
 /**
@@ -1821,4 +1821,20 @@ class Query implements ExpressionInterface, IteratorAggregate {
 		return sprintf('(%s)', $this->sql());
 	}
 
+/**
+ * Returns an array that can be used to describe the internal state of this
+ * object.
+ *
+ * @return array
+ */
+	public function __debugInfo() {
+		return [
+			'sql' => $this->sql(),
+			'params' => $this->valueBinder()->bindings(),
+			'defaultTypes' => $this->_defaultTypes,
+			'decorators' => count($this->_resultDecorators),
+			'executed' => $this->_iterator ? true : false
+		];
+	}
+
 }

+ 36 - 0
tests/TestCase/Database/QueryTest.php

@@ -2397,6 +2397,42 @@ class QueryTest extends TestCase {
 	}
 
 /**
+ * Tests __debugInfo
+ *
+ * @return void
+ */
+	public function testDebugInfo() {
+		$query = (new Query($this->connection))->select('*')
+			->from('articles')
+			->defaultTypes(['id' => 'integer'])
+			->where(['id' => '1']);
+
+		$expected = [
+			'sql' => $query->sql(),
+			'params' => [
+				':c0' => ['value' => '1', 'type' => 'integer', 'placeholder' => 'c0']
+			],
+			'defaultTypes' => ['id' => 'integer'],
+			'decorators' => 0,
+			'executed' => false
+		];
+		$result = $query->__debugInfo();
+		$this->assertEquals($expected, $result);
+
+		$query->execute();
+		$expected = [
+			'sql' => $query->sql(),
+			'params' => [
+				':c0' => ['value' => '1', 'type' => 'integer', 'placeholder' => 'c0']
+			],
+			'defaultTypes' => ['id' => 'integer'],
+			'decorators' => 0,
+			'executed' => true
+		];
+		$result = $query->__debugInfo();
+	}
+
+/**
  * Assertion for comparing a table's contents with what is in it.
  *
  * @param string $table