Skip to content
Browse files

Logging transaction commands in DboSource, fixes #2457

  • Loading branch information...
1 parent df5d9ac commit 37314a21883e955402850b2474632f3578228fc6 @lorenzo lorenzo committed
Showing with 51 additions and 0 deletions.
  1. +9 −0 lib/Cake/Model/Datasource/DboSource.php
  2. +42 −0 lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
View
9 lib/Cake/Model/Datasource/DboSource.php
@@ -2004,6 +2004,9 @@ public function truncate($table) {
*/
public function begin() {
if ($this->_transactionStarted || $this->_connection->beginTransaction()) {
+ if ($this->fullDebug && empty($this->_transactionNesting)) {
+ $this->logQuery('BEGIN');
+ }
$this->_transactionStarted = true;
$this->_transactionNesting++;
return true;
@@ -2024,6 +2027,9 @@ public function commit() {
if ($this->_transactionNesting <= 0) {
$this->_transactionStarted = false;
$this->_transactionNesting = 0;
+ if ($this->fullDebug) {
+ $this->logQuery('COMMIT');
+ }
return $this->_connection->commit();
}
return true;
@@ -2040,6 +2046,9 @@ public function commit() {
*/
public function rollback() {
if ($this->_transactionStarted && $this->_connection->rollBack()) {
+ if ($this->fullDebug) {
+ $this->logQuery('ROLLBACK');
+ }
$this->_transactionStarted = false;
$this->_transactionNesting = 0;
return true;
View
42 lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
@@ -23,6 +23,12 @@
App::uses('DboSource', 'Model/Datasource');
require_once dirname(dirname(__FILE__)) . DS . 'models.php';
+class MockPDO extends PDO {
+
+ public function __construct() {
+ }
+}
+
class MockDataSource extends DataSource {
}
@@ -645,6 +651,7 @@ public function testGetLog() {
$this->testDb->logQuery('Query 2');
$log = $this->testDb->getLog();
+
$expected = array('query' => 'Query 1', 'affected' => '', 'numRows' => '', 'took' => '');
$this->assertEquals($log['log'][0], $expected);
$expected = array('query' => 'Query 2', 'affected' => '', 'numRows' => '', 'took' => '');
@@ -783,4 +790,39 @@ function testLastError() {
$expected = 'something: bad';
$this->assertEquals($expected, $result);
}
+
+/**
+ * Tests that transaction commands are logged
+ *
+ * @return void
+ **/
+ public function testTransactionLogging() {
+ $conn = $this->getMock('MockPDO');
+ $db = new DboTestSource;
+ $db->setConnection($conn);
+ $conn->expects($this->exactly(2))->method('beginTransaction')
+ ->will($this->returnValue(true));
+ $conn->expects($this->once())->method('commit')->will($this->returnValue(true));
+ $conn->expects($this->once())->method('rollback')->will($this->returnValue(true));
+
+ $db->begin();
+ $log = $db->getLog();
+ $expected = array('query' => 'BEGIN', 'affected' => '', 'numRows' => '', 'took' => '');
+ $this->assertEquals($expected, $log['log'][0]);
+
+ $db->commit();
+ $expected = array('query' => 'COMMIT', 'affected' => '', 'numRows' => '', 'took' => '');
+ $log = $db->getLog();
+ $this->assertEquals($expected, $log['log'][0]);
+
+ $db->begin();
+ $expected = array('query' => 'BEGIN', 'affected' => '', 'numRows' => '', 'took' => '');
+ $log = $db->getLog();
+ $this->assertEquals($expected, $log['log'][0]);
+
+ $db->rollback();
+ $expected = array('query' => 'ROLLBACK', 'affected' => '', 'numRows' => '', 'took' => '');
+ $log = $db->getLog();
+ $this->assertEquals($expected, $log['log'][0]);
+ }
}

0 comments on commit 37314a2

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