Skip to content

Commit

Permalink
Fixed small issue and adding tests for new query error logging feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 19, 2014
1 parent b79f255 commit c00bde6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Database/Log/LoggingStatement.php
Expand Up @@ -56,6 +56,7 @@ public function execute($params = null) {
throw $e;
}

$query->numRows = $this->rowCount();
$this->_log($query, $params, $t);
return $result;
}
Expand All @@ -71,7 +72,6 @@ public function execute($params = null) {
*/
protected function _log($query, $params, $startTime) {
$query->took = round((microtime(true) - $startTime) * 1000, 0);
$query->numRows = $this->rowCount();
$query->params = $params ?: $this->_compiledParams;
$query->query = $this->queryString;
$this->logger()->log($query);
Expand Down
33 changes: 30 additions & 3 deletions tests/TestCase/Database/Log/LoggingStatementTest.php
Expand Up @@ -61,7 +61,7 @@ public function testExecuteWithParams() {
->with($this->logicalAnd(
$this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
$this->attributeEqualTo('query', 'SELECT bar FROM foo'),
$this->attributeEqualTo('took', 5, 5),
$this->attributeEqualTo('took', 5, 200),
$this->attributeEqualTo('numRows', 4),
$this->attributeEqualTo('params', ['a' => 1, 'b' => 2])
));
Expand All @@ -85,7 +85,7 @@ public function testExecuteWithBinding() {
->with($this->logicalAnd(
$this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
$this->attributeEqualTo('query', 'SELECT bar FROM foo'),
$this->attributeEqualTo('took', 5, 5),
$this->attributeEqualTo('took', 5, 200),
$this->attributeEqualTo('numRows', 4),
$this->attributeEqualTo('params', ['a' => 1, 'b' => '2013-01-01'])
));
Expand All @@ -94,7 +94,7 @@ public function testExecuteWithBinding() {
->with($this->logicalAnd(
$this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
$this->attributeEqualTo('query', 'SELECT bar FROM foo'),
$this->attributeEqualTo('took', 5, 5),
$this->attributeEqualTo('took', 5, 200),
$this->attributeEqualTo('numRows', 4),
$this->attributeEqualTo('params', ['a' => 1, 'b' => '2014-01-01'])
));
Expand All @@ -112,4 +112,31 @@ public function testExecuteWithBinding() {
$st->execute();
}

/**
* Tests that queries are logged despite database errors
*
* @expectedException \LogicException
* @expectedExceptionMessage This is bad
* @return void
*/
public function testExecuteWithError() {
$exception = new \LogicException('This is bad');
$inner = $this->getMock('PDOStatement');
$inner->expects($this->once())->method('execute')
->will($this->throwException($exception));
$logger = $this->getMock('\Cake\Database\Log\QueryLogger');
$logger->expects($this->once())
->method('log')
->with($this->logicalAnd(
$this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
$this->attributeEqualTo('query', 'SELECT bar FROM foo'),
$this->attributeEqualTo('took', 5, 200),
$this->attributeEqualTo('params', []),
$this->attributeEqualTo('error', $exception)
));
$st = new LoggingStatement($inner);
$st->queryString = 'SELECT bar FROM foo';
$st->logger($logger);
$st->execute();
}
}

0 comments on commit c00bde6

Please sign in to comment.