From af4b0c9c1baa445ad4b3458c94cafd117b4cd55a Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 30 Sep 2011 21:56:39 -0400 Subject: [PATCH] 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 --- lib/Cake/Model/Datasource/DboSource.php | 6 +++++- .../Case/Model/Datasource/DboSourceTest.php | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 6c99a1df1..6ca24960f 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -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; } diff --git a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php index 4e644545f..13765845c 100644 --- a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php @@ -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); + } }