From d9b1e4b2d27bc6b5d82567b5b1c433878b5bd1d4 Mon Sep 17 00:00:00 2001 From: sparkyx Date: Mon, 19 May 2025 23:01:49 +0200 Subject: [PATCH] Code Improve : Single_Record --- include/lib/single_record.class.php | 39 ++++--- unit-test/include/lib/Single_RecordTest.php | 118 ++++++++++++++++++++ 2 files changed, 144 insertions(+), 13 deletions(-) create mode 100644 unit-test/include/lib/Single_RecordTest.php diff --git a/include/lib/single_record.class.php b/include/lib/single_record.class.php index 6b14869c8..bb7a8ad80 100644 --- a/include/lib/single_record.class.php +++ b/include/lib/single_record.class.php @@ -27,22 +27,37 @@ define ('CODE_EXCP_DUPLICATE',901); /** * @brief Objec to check a double insert into the database, this duplicate occurs after * a refresh of the web page - * in */ class Single_Record { public $name; public $id; + private $dbconx; //!< database connexion /** - * Constructor $p_name will be set to $this->name, it is also the name + * @brief Constructor $p_name will be set to $this->name, it is also the name * of the tag hidden in a form * @remark $cn Db connexion - * @param $p_name + * @param $p_name (string) name of the HIDDEN INPUT + * @param $dbconx Database connx , if not given; use global $cn; */ - function __construct($p_name) + function __construct($p_name,$dbconx=-1) { $this->name=$p_name; + $this->id=0; + if ( is_numeric($dbconx ) ) { + global $cn; + $this->dbconx=$cn; + }else { + $this->dbconx=$dbconx; + } + + } + function __toString(): string { + return "Single_Record[name=" . $this->name + . ", id=" . $this->id + . ", dbconx=" . $this->dbconx + . "]"; } /** * @brief return a string with a tag hidden and a uniq value @@ -51,8 +66,7 @@ class Single_Record */ function hidden() { - global $cn; - $this->id=$cn->get_next_seq('uos_pk_seq'); + $this->id=$this->dbconx->get_next_seq('uos_pk_seq'); return HtmlInput::hidden($this->name,$this->id); } /** @@ -62,19 +76,18 @@ class Single_Record */ function save($p_array=null) { - global $cn; - if ( $p_array == null ) $p_array=$_POST; - $this->id=$p_array[$this->name]; + if ( $p_array == null ) $p_array=$_POST; + $this->id=$p_array[$this->name]; $sql="insert into tool_uos(uos_value) values ($1)"; try { - $cn->exec_sql($sql,array($this->id)); + $this->dbconx->exec_sql($sql,array($this->id)); } catch (Exception $e) { throw new Exception('Duplicate value'); } } /** - * Count how many time we have this->id into the table tool_uos + * @brief Count how many time we have this->id into the table tool_uos * @remark global $cn Database connexion * @param $p_array is the array where to find the key name, usually it is * $_POST. The default value is $_POST @@ -85,7 +98,7 @@ class Single_Record global $cn; if ( $p_array == null ) $p_array=$_POST; $this->id=$p_array[$this->name]; - $count=$cn->get_value('select count(*) from tool_uos where uos_value=$1', + $count=$this->dbconx->get_value('select count(*) from tool_uos where uos_value=$1', array($this->id)); return $count; } @@ -96,7 +109,7 @@ class Single_Record $this->id=$p_array[$this->name]; try { - $count=$cn->get_value('select count(*) from tool_uos where uos_value=$1', + $count=$this->dbconx->get_value('select count(*) from tool_uos where uos_value=$1', array($this->id)); if ($count != 0 ) throw new Exception ('DUPLICATE',CODE_EXCP_DUPLICATE); }catch (Exception $e) diff --git a/unit-test/include/lib/Single_RecordTest.php b/unit-test/include/lib/Single_RecordTest.php new file mode 100644 index 000000000..ae634deb9 --- /dev/null +++ b/unit-test/include/lib/Single_RecordTest.php @@ -0,0 +1,118 @@ +hidden(); + $this->assertTrue($test_dup->id > 0, " id not computed " . $test_dup); + } + + /** + * @covers Single_Record::hidden + * @covers Single_Record::__construct() + * @global $g_connection + */ + function testHiddenDefaultDatabase() { + global $g_connection; + global $cn; + $cn = $g_connection; + $test_dup = new \Single_Record("test_dup"); + $r = $test_dup->hidden(); + $this->assertTrue($test_dup->id > 0, " id not computed " . $test_dup); + } + + /** + * @covers Single_Record::hidden + * @covers Single_Record::__construct() + * @global $g_connection + */ + function testSequence() { + global $g_connection; + global $cn; + $cn = $g_connection; + $test_dup = new \Single_Record("test_dup"); + $r = $test_dup->hidden(); + $a = $test_dup->id; + $this->assertTrue($test_dup->id > 0, " id not computed " . $test_dup); + $r = $test_dup->hidden(); + $this->assertTrue($test_dup->id > $a, " id not incremented" . $test_dup); + } + + /** + * @covers Single_Record::save() + * @global $g_connection + */ + function testSave() { + global $g_connection; + global $cn; + $cn = $g_connection; + $test_dup = new \Single_Record("test_dup"); + $r = $test_dup->hidden(); + $this->assertTrue($test_dup->id > 0, " id not computed " . $test_dup); + $test_dup->save(["test_dup"=>$test_dup->id]); + } + + /** + * @covers Single_Record::check() + * @global $g_connection + */ + function testCheck() { + global $g_connection; + global $cn; + $this->expectException (\Exception::class); + $cn = $g_connection; + $test_dup = new \Single_Record("test_dup"); + $r = $test_dup->hidden(); + $test_dup->save(["test_dup"=>$test_dup->id]); + $this->assertTrue($test_dup->id > 0, " id not computed " . $test_dup); + $test_dup->check(['test_dup'=>$test_dup->id]); + $test_dup->check(['test_dup'=>$test_dup->id]); + } + /** + * @covers Single_Record::get_count() + * @global $g_connection + */ + function testCount() { + global $g_connection; + global $cn; + $cn = $g_connection; + $test_dup = new \Single_Record("test_dup"); + $r = $test_dup->hidden(); + $this->assertTrue($test_dup->id > 0, " id not computed " . $test_dup); + $test_dup->save(["test_dup"=>$test_dup->id]); + $cnt = $test_dup->get_count(['test_dup'=>$test_dup->id]); + $this->assertTrue($cnt > 0,"not count") ; + } +}