Browse Source

Merge branch '3.0' of github.com:cakephp/cakephp into 3.0

Mark Story 12 years ago
parent
commit
91dcfedd7e

+ 17 - 1
src/Database/Query.php

@@ -204,7 +204,7 @@ class Query implements ExpressionInterface, IteratorAggregate {
 		$query->_bindStatement($statement);
 		$query->_bindStatement($statement);
 		$statement->execute();
 		$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());
 		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
+		];
+	}
+
 }
 }

+ 15 - 0
src/ORM/Query.php

@@ -708,4 +708,19 @@ class Query extends DatabaseQuery {
 		);
 		);
 	}
 	}
 
 
+/**
+ * {@inheritdoc}
+ */
+	public function __debugInfo() {
+		return parent::__debugInfo() + [
+			'hydrate' => $this->_hydrate,
+			'buffered' => $this->_useBufferedResults,
+			'formatters' => count($this->_formatters),
+			'mapReducers' => count($this->_mapReduce),
+			'contain' => $this->contain(),
+			'extraOptions' => $this->_options,
+			'repository' => $this->_repository
+		];
+	}
+
 }
 }

+ 13 - 0
src/ORM/ResultSet.php

@@ -439,4 +439,17 @@ class ResultSet implements Countable, Iterator, Serializable, JsonSerializable {
 		}
 		}
 	}
 	}
 
 
+/**
+ * Returns an array that can be used to describe the internal state of this
+ * object.
+ *
+ * @return array
+ */
+	public function __debugInfo() {
+		return [
+			'query' => $this->_query,
+			'items' => $this->toArray(),
+		];
+	}
+
 }
 }

+ 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.
  * Assertion for comparing a table's contents with what is in it.
  *
  *
  * @param string $table
  * @param string $table

+ 48 - 0
tests/TestCase/ORM/QueryTest.php

@@ -1767,4 +1767,52 @@ class QueryTest extends TestCase {
 		$this->assertEquals('tag3', $results[0]->articles->articles_tags->tag->name);
 		$this->assertEquals('tag3', $results[0]->articles->articles_tags->tag->name);
 	}
 	}
 
 
+/**
+ * Tests __debugInfo
+ *
+ * @return void
+ */
+	public function testDebugInfo() {
+		$table = TableRegistry::get('authors');
+		$table->hasMany('articles');
+		$query = $table->find()
+			->where(['id > ' => 1])
+			->bufferResults(false)
+			->hydrate(false)
+			->matching('articles')
+			->applyOptions(['foo' => 'bar'])
+			->formatResults(function($results) {
+				return $results;
+			})
+			->mapReduce(function($item, $key, $mr) {
+				$mr->emit($item);
+			});
+
+		$expected = [
+			'sql' => $query->sql(),
+			'params' => $query->valueBinder()->bindings(),
+			'defaultTypes' => [
+				'authors.id' => 'integer',
+				'id' => 'integer',
+				'authors.name' => 'string',
+				'name' => 'string'
+			],
+			'decorators' => 0,
+			'executed' => false,
+			'hydrate' => false,
+			'buffered' => false,
+			'formatters' => 1,
+			'mapReducers' => 1,
+			'contain' => [
+				'articles' => [
+					'queryBuilder' => null,
+					'matching' => true
+				]
+			],
+			'extraOptions' => ['foo' => 'bar'],
+			'repository' => $table
+		];
+		$this->assertSame($expected, $query->__debugInfo());
+	}
+
 }
 }

+ 15 - 0
tests/TestCase/ORM/ResultSetTest.php

@@ -230,4 +230,19 @@ class ResultSetTest extends TestCase {
 		$this->assertEquals($expected, $results);
 		$this->assertEquals($expected, $results);
 	}
 	}
 
 
+/**
+ * Tests __debugInfo
+ *
+ * @return void
+ */
+	public function testDebugInfo() {
+		$query = $this->table->find('all');
+		$results = $query->all();
+		$expected = [
+			'query' => $query,
+			'items' => $results->toArray()
+		];
+		$this->assertSame($expected, $results->__debugInfo());
+	}
+
 }
 }