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
This commit is contained in:
mark_story 2011-09-30 21:56:39 -04:00
parent 705593908b
commit af4b0c9c1b
2 changed files with 25 additions and 1 deletions

View file

@ -460,7 +460,11 @@ class DboSource extends DataSource {
* @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;
}

View file

@ -39,6 +39,10 @@ class DboTestSource extends DboSource {
public function setConfig($config) {
$this->config = $config;
}
public function setConnection($conn) {
$this->_connection = $conn;
}
}
/**
@ -786,4 +790,20 @@ class DboSourceTest extends CakeTestCase {
$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);
}
}