Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.0 more debug info 2 #2999

Merged
merged 3 commits into from Mar 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Database/Query.php
Expand Up @@ -204,7 +204,7 @@ public function execute() {
$query->_bindStatement($statement);
$statement->execute();

return $query->_decorateStatement($statement);
return $this->_iterator = $query->_decorateStatement($statement);
}

/**
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe get rid of the ternary operator and use a cast here?

];
}

}
15 changes: 15 additions & 0 deletions src/ORM/Query.php
Expand Up @@ -708,4 +708,19 @@ public function __call($method, $arguments) {
);
}

/**
* {@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 changes: 13 additions & 0 deletions src/ORM/ResultSet.php
Expand Up @@ -439,4 +439,17 @@ protected function _bufferResult($result) {
}
}

/**
* 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 changes: 36 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -2396,6 +2396,42 @@ public function testQuotingInsert() {
$this->assertQuotedQuery('INSERT INTO <foo> \(\(bar\)\)', $sql);
}

/**
* 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.
*
Expand Down
48 changes: 48 additions & 0 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -1767,4 +1767,52 @@ public function testContainInAssociationMatching() {
$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 changes: 15 additions & 0 deletions tests/TestCase/ORM/ResultSetTest.php
Expand Up @@ -230,4 +230,19 @@ public function testGroupBy() {
$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());
}

}