mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
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:
parent
705593908b
commit
af4b0c9c1b
2 changed files with 25 additions and 1 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue