[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Noalyss-commit] [noalyss] 09/16: Code Improve : Single_Record
From: |
dwm |
Subject: |
[Noalyss-commit] [noalyss] 09/16: Code Improve : Single_Record |
Date: |
Tue, 27 May 2025 09:26:09 -0400 (EDT) |
sparkyx pushed a commit to branch unstable
in repository noalyss.
commit d9b1e4b2d27bc6b5d82567b5b1c433878b5bd1d4
Author: sparkyx <danydb@noalyss.eu>
AuthorDate: Mon May 19 23:01:49 2025 +0200
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(-)
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 @@
+<?php
+
+/*
+ * This file is part of NOALYSS.
+ *
+ * NOALYSS is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * NOALYSS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with NOALYSS; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+// Copyright Author Dany De Bontridder danydb@aevalys.eu 22/10/23
+
+/**
+ * @file
+ * @brief test Single_Record
+ */
+use PHPUnit\Framework\TestCase;
+
+class Single_RecordTest extends TestCase {
+
+ /**
+ * @covers Single_Record::hidden
+ * @covers Single_Record::__construct()
+ * @global $g_connection
+ */
+ function testHiddenDatabase() {
+ global $g_connection;
+ $test_dup = new \Single_Record("test_dup", $g_connection);
+ $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 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") ;
+ }
+}
- [Noalyss-commit] [noalyss] 03/16: Cosmetic width menu, (continued)
- [Noalyss-commit] [noalyss] 03/16: Cosmetic width menu, dwm, 2025/05/27
- [Noalyss-commit] [noalyss] 10/16: Data_SQL and Table_Data_SQL add virtual column, to allow transformation of columns in SQL, faster than transforming in PHP afterward, dwm, 2025/05/27
- [Noalyss-commit] [noalyss] 04/16: FIX bug due changes Data_SQL, dwm, 2025/05/27
- [Noalyss-commit] [noalyss] 02/16: FONT MONSERRAT ROBOTO Title and menu, dwm, 2025/05/27
- [Noalyss-commit] [noalyss] 05/16: FREC : checkbox, checked by range, hidden elements must not be checked, dwm, 2025/05/27
- [Noalyss-commit] [noalyss] 08/16: Improve Code: Database : add __toString, dwm, 2025/05/27
- [Noalyss-commit] [noalyss] 14/16: Add column timestamp for table TOOL_UOS, dwm, 2025/05/27
- [Noalyss-commit] [noalyss] 01/16: Code Improve : Data_SQL, dwm, 2025/05/27
- [Noalyss-commit] [noalyss] 06/16: FIX bug due changes Data_SQL, dwm, 2025/05/27
- [Noalyss-commit] [noalyss] 07/16: Detail Operation for small screen, dwm, 2025/05/27
- [Noalyss-commit] [noalyss] 09/16: Code Improve : Single_Record,
dwm <=
- [Noalyss-commit] [noalyss] 12/16: Data_SQL if we use $this->sql , then must be set SQL, dwm, 2025/05/27
- [Noalyss-commit] [noalyss] 13/16: Data_SQL if column doesn't exist, dwm, 2025/05/27
- [Noalyss-commit] [noalyss] 15/16: Fix bug : wrong function name, dwm, 2025/05/27
- [Noalyss-commit] [noalyss] 11/16: CSS : cosmetic : font in a variable + outline input text in orange, dwm, 2025/05/27
- [Noalyss-commit] [noalyss] 16/16: PHP8.3 comptability, dynamic members, dwm, 2025/05/27