From 7e5ae8d78082891386f170d2d3db9b7ca7436e4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Tue, 9 Jul 2013 12:31:46 +0200 Subject: [PATCH 1/3] test lastinsertid --- tests/lib/db.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/lib/db.php b/tests/lib/db.php index e817a2db5e..e965ce8e1c 100644 --- a/tests/lib/db.php +++ b/tests/lib/db.php @@ -71,7 +71,19 @@ class Test_DB extends PHPUnit_Framework_TestCase { $result = $query->execute(array('uri_3')); $this->assertTrue((bool)$result); } - + + public function testLastInsertId() { + $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (?,?)'); + $result = OC_DB::executeAudited($query, array('insertid 1','uri_1')); + $id1 = OC_DB::insertid('*PREFIX*'.$this->table2); + // we don't know the id we should expect, so insert another row + $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (?,?)'); + $result = OC_DB::executeAudited($query, array('insertid 2','uri_2')); + $id2 = OC_DB::insertid('*PREFIX*'.$this->table2); + // now we can check if the two ids are in correct order + $this->assertEquals($id1+1, $id2); + } + public function testinsertIfNotExist() { $categoryentries = array( array('user' => 'test', 'type' => 'contact', 'category' => 'Family', 'expectedResult' => 1), From 05fa55f2eb6997f3b59a2418a64447427a1b19ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Tue, 9 Jul 2013 13:17:01 +0200 Subject: [PATCH 2/3] always return int --- lib/db.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/db.php b/lib/db.php index e70d66fc2b..dd48c32949 100644 --- a/lib/db.php +++ b/lib/db.php @@ -344,7 +344,7 @@ class OC_DB { $result = self::executeAudited('SELECT lastval() AS id'); $row = $result->fetchRow(); self::raiseExceptionOnError($row, 'fetching row for insertid failed'); - return $row['id']; + return (int)$row['id']; } else if( $type === 'mssql') { if($table !== null) { $prefix = OC_Config::getValue( "dbtableprefix", "oc_" ); @@ -368,7 +368,7 @@ class OC_DB { $result = self::$connection->lastInsertId($table); } self::raiseExceptionOnError($result, 'insertid failed'); - return $result; + return (int)$result; } /** From b8bd1e5a81080a756913e5804b877aafd770f6e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Tue, 9 Jul 2013 13:17:25 +0200 Subject: [PATCH 3/3] check type, assertgreaterthan --- tests/lib/db.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/lib/db.php b/tests/lib/db.php index e965ce8e1c..79e05f30a1 100644 --- a/tests/lib/db.php +++ b/tests/lib/db.php @@ -74,14 +74,16 @@ class Test_DB extends PHPUnit_Framework_TestCase { public function testLastInsertId() { $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (?,?)'); - $result = OC_DB::executeAudited($query, array('insertid 1','uri_1')); + $result1 = OC_DB::executeAudited($query, array('insertid 1','uri_1')); $id1 = OC_DB::insertid('*PREFIX*'.$this->table2); + $this->assertInternalType('int', $id1); + // we don't know the id we should expect, so insert another row - $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (?,?)'); - $result = OC_DB::executeAudited($query, array('insertid 2','uri_2')); + $result2 = OC_DB::executeAudited($query, array('insertid 2','uri_2')); $id2 = OC_DB::insertid('*PREFIX*'.$this->table2); // now we can check if the two ids are in correct order - $this->assertEquals($id1+1, $id2); + $this->assertInternalType('int', $id2); + $this->assertGreaterThan($id1, $id2); } public function testinsertIfNotExist() {