From b80f9ecdeaad7bef1239766aa867dbcc626ef910 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Wed, 24 Sep 2014 09:13:18 +0000 Subject: [PATCH] BUG : dblayer : don't raise an exception when re-inserting unique record dblayer : add more unit tests git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1850 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- Tests/dblayerComplet.php | 46 ++++++++++++++++++++++++++++++++++++++++ dblayer.php | 12 +++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/Tests/dblayerComplet.php b/Tests/dblayerComplet.php index cf8e9d6..f6820ff 100644 --- a/Tests/dblayerComplet.php +++ b/Tests/dblayerComplet.php @@ -259,4 +259,50 @@ class test_dblayer_{ENGINE} extends PHPUnit_Framework_TestCase $this->assertThat($res, $this->lessThanOrEqual(2)); } + // Test the unique feature + public function test_insert3 () + { + $dbconfig = $this->confs["{ENGINE}"]; + $db = new dblayer ($dbconfig["dsn"], $dbconfig["username"], + $dbconfig["password"], $dbconfig["driver_options"]); + $db->table = "users"; + $db->fields = array ("user"=>array ("varchar", "255", "not null"), + "groupmember"=>array ("varchar", "255", "not null"), + "where"=>array ("varchar", "255", "not null"), + "with space"=>array ("varchar", "255", "not null")); + // Unique simple in insert + $db->unique = array ("user"); + $db->primary = "user"; + $db->foreign = array ( + "groupmember"=>array ("grouped", "group", "ON UPDATE CASCADE ON DELETE CASCADE"), + ); + $this->setExpectedException ("Exception"); + $res = $db->insert (array ("user"=>"Us ou\"r", + "groupmember"=>"NEW GROUP", + "where"=>"\$'\"", + "with space"=>"with space")); + } + + public function test_insert4 () + { + $dbconfig = $this->confs["{ENGINE}"]; + $db = new dblayer ($dbconfig["dsn"], $dbconfig["username"], + $dbconfig["password"], $dbconfig["driver_options"]); + $db->table = "users"; + $db->fields = array ("user"=>array ("varchar", "255", "not null"), + "groupmember"=>array ("varchar", "255", "not null"), + "where"=>array ("varchar", "255", "not null"), + "with space"=>array ("varchar", "255", "not null")); + // Unique multiple in insert + $db->unique = array (array ("user", "groupmember")); + $db->primary = "user"; + $db->foreign = array ( + "groupmember"=>array ("grouped", "group", "ON UPDATE CASCADE ON DELETE CASCADE"), + ); + $this->setExpectedException ("Exception"); + $res = $db->insert (array ("user"=>"Us ou\"r", + "groupmember"=>"NEW GROUP", + "where"=>"\$'\"", + "with space"=>"with space")); + } } diff --git a/dblayer.php b/dblayer.php index a4bf152..88043d4 100644 --- a/dblayer.php +++ b/dblayer.php @@ -398,7 +398,7 @@ class dblayer extends PDO } // If there is only the primary key, there is no chance to have a // conflict - if ($updatekey !== false && count ($select) >= 2) + if ($updatekey === false && count ($select) >= 2) { $rc = $this->read ($select, array ($this->primary)); if (count ($rc) > 0) @@ -580,7 +580,15 @@ class dblayer extends PDO throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0], 500); } - $st->execute (); + try + { + $st->execute (); + } + catch (Exception $e) + { + echo "dblayer execute exception : ".$e->getMessage()."\n"; + exit; + } return $this->db->lastInsertId(); }