Skip to content
This repository
Browse code

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...
commit af4b0c9c1baa445ad4b3458c94cafd117b4cd55a 1 parent 7055939
Mark Story authored
6  lib/Cake/Model/Datasource/DboSource.php
@@ -460,7 +460,11 @@ protected function _execute($sql, $params = array(), $prepareOptions = array())
460 460
  * @return string Error message with error number
461 461
  */
462 462
 	public function lastError(PDOStatement $query = null) {
463  
-		$error = $query->errorInfo();
  463
+		if ($query) {
  464
+			$error = $query->errorInfo();
  465
+		} else {
  466
+			$error = $this->_connection->errorInfo();
  467
+		}
464 468
 		if (empty($error[2])) {
465 469
 			return null;
466 470
 		}
20  lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
@@ -39,6 +39,10 @@ public function mergeAssociation(&$data, &$merge, $association, $type, $selfJoin
39 39
 	public function setConfig($config) {
40 40
 		$this->config = $config;
41 41
 	}
  42
+
  43
+	public function setConnection($conn) {
  44
+		$this->_connection = $conn;
  45
+	}
42 46
 }
43 47
 
44 48
 /**
@@ -786,4 +790,20 @@ function testGroupNoModel() {
786 790
 		$this->assertEqual(' GROUP BY created', $result);
787 791
 	}
788 792
 
  793
+/**
  794
+ * Test getting the last error.
  795
+ */
  796
+	function testLastError() {
  797
+		$result = $this->db->lastError();
  798
+		$this->assertNull($result);
  799
+
  800
+		$stmt = $this->getMock('PDOStatement');
  801
+		$stmt->expects($this->any())
  802
+			->method('errorInfo')
  803
+			->will($this->returnValue(array('', 'something', 'bad')));
  804
+
  805
+		$result = $this->db->lastError($stmt);
  806
+		$expected = 'something: bad';
  807
+		$this->assertEquals($expected, $result);
  808
+	}
789 809
 }

0 notes on commit af4b0c9

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