Skip to content
Browse files

Adding __debugInfo to Database\Query

  • Loading branch information...
1 parent 881098f commit b735c5dc3013a504e2ebe44ac0a759091196d5c4 @lorenzo lorenzo committed Mar 10, 2014
Showing with 53 additions and 1 deletion.
  1. +17 −1 src/Database/Query.php
  2. +36 −0 tests/TestCase/Database/QueryTest.php
View
18 src/Database/Query.php
@@ -204,7 +204,7 @@ public function execute() {
$query->_bindStatement($statement);
$statement->execute();
- return $query->_decorateStatement($statement);
+ return $this->_iterator = $query->_decorateStatement($statement);
}
/**
@@ -1821,4 +1821,20 @@ public function __toString() {
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
+ ];
+ }
+
}
View
36 tests/TestCase/Database/QueryTest.php
@@ -2397,6 +2397,42 @@ public function testQuotingInsert() {
}
/**
+ * 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

0 comments on commit b735c5d

Please sign in to comment.
Something went wrong with that request. Please try again.