Skip to content

Commit c00bde6

Browse files
committed
Fixed small issue and adding tests for new query error logging feature
1 parent b79f255 commit c00bde6

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/Database/Log/LoggingStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function execute($params = null) {
5656
throw $e;
5757
}
5858

59+
$query->numRows = $this->rowCount();
5960
$this->_log($query, $params, $t);
6061
return $result;
6162
}
@@ -71,7 +72,6 @@ public function execute($params = null) {
7172
*/
7273
protected function _log($query, $params, $startTime) {
7374
$query->took = round((microtime(true) - $startTime) * 1000, 0);
74-
$query->numRows = $this->rowCount();
7575
$query->params = $params ?: $this->_compiledParams;
7676
$query->query = $this->queryString;
7777
$this->logger()->log($query);

tests/TestCase/Database/Log/LoggingStatementTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testExecuteWithParams() {
6161
->with($this->logicalAnd(
6262
$this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
6363
$this->attributeEqualTo('query', 'SELECT bar FROM foo'),
64-
$this->attributeEqualTo('took', 5, 5),
64+
$this->attributeEqualTo('took', 5, 200),
6565
$this->attributeEqualTo('numRows', 4),
6666
$this->attributeEqualTo('params', ['a' => 1, 'b' => 2])
6767
));
@@ -85,7 +85,7 @@ public function testExecuteWithBinding() {
8585
->with($this->logicalAnd(
8686
$this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
8787
$this->attributeEqualTo('query', 'SELECT bar FROM foo'),
88-
$this->attributeEqualTo('took', 5, 5),
88+
$this->attributeEqualTo('took', 5, 200),
8989
$this->attributeEqualTo('numRows', 4),
9090
$this->attributeEqualTo('params', ['a' => 1, 'b' => '2013-01-01'])
9191
));
@@ -94,7 +94,7 @@ public function testExecuteWithBinding() {
9494
->with($this->logicalAnd(
9595
$this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
9696
$this->attributeEqualTo('query', 'SELECT bar FROM foo'),
97-
$this->attributeEqualTo('took', 5, 5),
97+
$this->attributeEqualTo('took', 5, 200),
9898
$this->attributeEqualTo('numRows', 4),
9999
$this->attributeEqualTo('params', ['a' => 1, 'b' => '2014-01-01'])
100100
));
@@ -112,4 +112,31 @@ public function testExecuteWithBinding() {
112112
$st->execute();
113113
}
114114

115+
/**
116+
* Tests that queries are logged despite database errors
117+
*
118+
* @expectedException \LogicException
119+
* @expectedExceptionMessage This is bad
120+
* @return void
121+
*/
122+
public function testExecuteWithError() {
123+
$exception = new \LogicException('This is bad');
124+
$inner = $this->getMock('PDOStatement');
125+
$inner->expects($this->once())->method('execute')
126+
->will($this->throwException($exception));
127+
$logger = $this->getMock('\Cake\Database\Log\QueryLogger');
128+
$logger->expects($this->once())
129+
->method('log')
130+
->with($this->logicalAnd(
131+
$this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
132+
$this->attributeEqualTo('query', 'SELECT bar FROM foo'),
133+
$this->attributeEqualTo('took', 5, 200),
134+
$this->attributeEqualTo('params', []),
135+
$this->attributeEqualTo('error', $exception)
136+
));
137+
$st = new LoggingStatement($inner);
138+
$st->queryString = 'SELECT bar FROM foo';
139+
$st->logger($logger);
140+
$st->execute();
141+
}
115142
}

0 commit comments

Comments
 (0)