Permalink
Browse files

Make DboSource::lastError() fallback to the connection.

If there is no argument the connection error should be checked
for an error.
Add a test for most of lastError().  Mocking PDO is a pain.
Fixes #2046
  • Loading branch information...
1 parent 7055939 commit af4b0c9c1baa445ad4b3458c94cafd117b4cd55a @markstory markstory committed Oct 1, 2011
Showing with 25 additions and 1 deletion.
  1. +5 −1 lib/Cake/Model/Datasource/DboSource.php
  2. +20 −0 lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
@@ -460,7 +460,11 @@ protected function _execute($sql, $params = array(), $prepareOptions = array())
* @return string Error message with error number
*/
public function lastError(PDOStatement $query = null) {
- $error = $query->errorInfo();
+ if ($query) {
+ $error = $query->errorInfo();
+ } else {
+ $error = $this->_connection->errorInfo();
+ }
if (empty($error[2])) {
return null;
}
@@ -39,6 +39,10 @@ public function mergeAssociation(&$data, &$merge, $association, $type, $selfJoin
public function setConfig($config) {
$this->config = $config;
}
+
+ public function setConnection($conn) {
+ $this->_connection = $conn;
+ }
}
/**
@@ -786,4 +790,20 @@ function testGroupNoModel() {
$this->assertEqual(' GROUP BY created', $result);
}
+/**
+ * Test getting the last error.
+ */
+ function testLastError() {
+ $result = $this->db->lastError();
+ $this->assertNull($result);
+
+ $stmt = $this->getMock('PDOStatement');
+ $stmt->expects($this->any())
+ ->method('errorInfo')
+ ->will($this->returnValue(array('', 'something', 'bad')));
+
+ $result = $this->db->lastError($stmt);
+ $expected = 'something: bad';
+ $this->assertEquals($expected, $result);
+ }
}

0 comments on commit af4b0c9

Please sign in to comment.