noalyss-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Noalyss-commit] [noalyss] 02/13: Fix code + add phpunit


From: Dany De Bontridder
Subject: [Noalyss-commit] [noalyss] 02/13: Fix code + add phpunit
Date: Mon, 30 Aug 2021 10:11:58 -0400 (EDT)

sparkyx pushed a commit to branch master
in repository noalyss.

commit 9d3e6db5ebf597bcb8c5a3f5a6408f7aa0420c58
Author: sparkyx <danydb@noalyss.eu>
AuthorDate: Mon Aug 23 19:55:45 2021 +0200

    Fix code + add phpunit
---
 include/class/lettering.class.php                  |  23 +-
 include/class/periode.class.php                    |   4 +-
 include/class/user.class.php                       |   4 +-
 ..._account.classTest.php => acc_account.Test.php} |  26 ++
 unit-test/include/class/acc_letter.Test.php        | 268 +++++++++++++++++++++
 unit-test/include/class/user.Test.php              |  26 ++
 6 files changed, 340 insertions(+), 11 deletions(-)

diff --git a/include/class/lettering.class.php 
b/include/class/lettering.class.php
index f59e282..6d6187f 100644
--- a/include/class/lettering.class.php
+++ b/include/class/lettering.class.php
@@ -53,8 +53,15 @@ class Lettering
         $this->db=$p_init;
         $a=new User($p_init);
         $exercice=$a->get_exercice();
-        $this->start='01.01.'.$exercice;
-        $this->end='31.12.'.$exercice;
+        if ($exercice > 0) {
+            $periode=new Periode($p_init);
+            $aLimite=$periode->get_limit($exercice);
+            $this->start=$aLimite[0]->first_day();
+            $this->end=$aLimite[1]->last_day();
+        } else {
+            $this->start='01.01.'.$exercice;
+            $this->end='31.12.'.$exercice;
+        }
         // available ledgers
         
$this->sql_ledger=str_replace('jrn_def_id','jr_def_id',$a->get_ledger_sql('ALL',3));
 
@@ -387,11 +394,6 @@ class Lettering
     {
         throw new Exception ('delete not implemented');
     }
-    /**
-     * Unit test for the class
-     */
-    static function test_me()
-    {}
 
 }
 /**
@@ -477,6 +479,7 @@ class Lettering_Account extends Lettering
                                                        
coalesce(let_diff.jl_id,-1) as letter,
                                        diff_letter1 as letter_diff,
                                         round(j_montant/currency_rate,4) as 
currency_amount,
+                                          currency_id,
                                         currency_rate, 
                                         currency_rate_ref,
                            (select cr_code_iso from currency where 
currency_id=currency.id) as cr_code_iso
@@ -505,6 +508,7 @@ class Lettering_Account extends Lettering
                                                let_diff.jl_id as letter,
                                        diff_letter1 as letter_diff,
                                          round(j_montant/currency_rate,4) as 
currency_amount,
+                                         currency_id,
                                         currency_rate, 
                                         currency_rate_ref,
                            (select cr_code_iso from currency where 
currency_id=currency.id) as cr_code_iso
@@ -532,6 +536,7 @@ class Lettering_Account extends Lettering
                                                let_diff.jl_id as letter,
                                        diff_letter1 as letter_diff,
                                          round(j_montant/currency_rate,4) as 
currency_amount,
+                                           currency_id,
                                         currency_rate, 
                                         currency_rate_ref,
                                         (select cr_code_iso from currency 
where currency_id=currency.id) as cr_code_iso
@@ -558,6 +563,7 @@ class Lettering_Account extends Lettering
                                                -1 as letter,
                                        0 as letter_diff,
                                          round(j_montant/currency_rate,4) as 
currency_amount,
+                                           currency_id,
                                         currency_rate, 
                                         currency_rate_ref,
                            (select cr_code_iso from currency where 
currency_id=currency.id) as cr_code_iso
@@ -694,6 +700,7 @@ class Lettering_Card extends Lettering
                                        diff_letter1 as letter_diff,
                                         round(j_montant/currency_rate,4) as 
currency_amount,
                                         currency_rate, 
+                                         currency_id,
                                         currency_rate_ref,
                            (select cr_code_iso from currency where 
currency_id=currency.id) as cr_code_iso
                                                from jrnx join jrn on (j_grpt = 
jr_grpt_id)
@@ -717,6 +724,7 @@ class Lettering_Card extends Lettering
                                                let_diff.jl_id as letter,
                                        diff_letter1 as letter_diff,
                                         round(j_montant/currency_rate,4) as 
currency_amount,
+                                           currency_id,
                                         currency_rate, 
                                         currency_rate_ref,
                            (select cr_code_iso from currency where 
currency_id=currency.id) as cr_code_iso
@@ -740,6 +748,7 @@ class Lettering_Card extends Lettering
              -1 as letter,
                         0 as letter_diff,
                                 round(j_montant/currency_rate,4) as 
currency_amount,
+                                currency_id,
                                 currency_rate, 
                                 currency_rate_ref,
                            (select cr_code_iso from currency where 
currency_id=currency.id) as cr_code_iso
diff --git a/include/class/periode.class.php b/include/class/periode.class.php
index 316ca6c..0949693 100644
--- a/include/class/periode.class.php
+++ b/include/class/periode.class.php
@@ -378,8 +378,8 @@ class Periode
     {
         if ($p_id==0)
             $p_id=$this->p_id;
-        $sql="select p_exercice from parm_periode where p_id=".$p_id;
-        $Res=$this->cn->exec_sql($sql);
+        $sql="select p_exercice from parm_periode where p_id=$1";
+        $Res=$this->cn->exec_sql($sql,[$p_id]);
         if (Database::num_row($Res)==0)
             return null;
         return Database::fetch_result($Res, 0, 0);
diff --git a/include/class/user.class.php b/include/class/user.class.php
index 35771c0..2b8f028 100644
--- a/include/class/user.class.php
+++ b/include/class/user.class.php
@@ -992,8 +992,8 @@ class User
 
     function get_exercice()
     {
-        $sql="select p_exercice from parm_periode where 
p_id=".$this->get_periode();
-        $Ret=$this->db->exec_sql($sql);
+        $sql="select p_exercice from parm_periode where p_id=$1";
+        $Ret=$this->db->exec_sql($sql,[$this->get_periode()]);
         if (Database::num_row($Ret)==1)
         {
             $r=Database::fetch_array($Ret, 0);
diff --git a/unit-test/include/class/acc_account.classTest.php 
b/unit-test/include/class/acc_account.Test.php
similarity index 88%
rename from unit-test/include/class/acc_account.classTest.php
rename to unit-test/include/class/acc_account.Test.php
index 676899a..1a63207 100644
--- a/unit-test/include/class/acc_account.classTest.php
+++ b/unit-test/include/class/acc_account.Test.php
@@ -234,4 +234,30 @@ class Acc_AccountTest extends TestCase
         $result=$this->object->find_parent();
         $this->assertEquals($result,700);
     }
+    public function dataFormatAccount()
+    {
+        return array(
+            ['60élépànç%','60ELEPANC'],
+            ['4000','4000'],
+            ['6é&" #çç!!!,%-.','6ECC'],
+            ['6-alpha6','6ALPHA6'],
+            ['6_alpha6','6ALPHA6'],
+            ['6.alpha6','6ALPHA6'],
+            ['6:alpha6','6:ALPHA6'],
+        );
+    }
+    /**
+     * @brief format properly the accounting
+     * @testdox check psql function format_account
+     * @param string $param
+     * @param string $p_result
+     * @dataProvider dataFormatAccount
+     */
+    public function testFormatAccount($param,$p_result)
+    {
+        global $g_connection;
+        $this->assertEquals($g_connection->get_value("select 
comptaproc.format_account($1)",[$param]),
+                $p_result,"comptaproc.format_account $param does not match 
$p_result");
+    }
+
 }
diff --git a/unit-test/include/class/acc_letter.Test.php 
b/unit-test/include/class/acc_letter.Test.php
new file mode 100644
index 0000000..b5520c1
--- /dev/null
+++ b/unit-test/include/class/acc_letter.Test.php
@@ -0,0 +1,268 @@
+<?php
+
+/*
+ * * Copyright (C) 2021 Dany De Bontridder <dany@alchimerys.be>
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+ * 
+ * Author : Dany De Bontridder danydb@noalyss.eu
+ * 
+ */
+
+/**
+ * @file
+ * @brief 
+ */
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @backupGlobals enabled
+ */
+require 'global.php';
+
+class Acc_Letter extends TestCase
+{
+    /**
+     * @var Fiche
+     */
+    protected $object;
+   
+    /**
+     * Sets up the fixture, for example, opens a network connection.
+     * This method is called before a test method is executed.
+     */
+    protected function setUp()
+    {
+        include 'global.php';
+        $this->a_column=array(
+            "j_id",
+           "j_date",
+           "j_date_fmt",
+           "jr_pj_number",
+           "j_montant",
+           "j_debit",
+           "jr_comment",
+           "jr_internal",
+           "jr_id",
+           "jr_def_id",
+           "letter",
+           "letter_diff",
+           "currency_amount",
+           "currency_id",
+           "currency_rate",
+           "currency_rate_ref",
+           "cr_code_iso"
+    );
+    }
+
+    /**
+     * Tears down the fixture, for example, closes a network connection.
+     * This method is called after a test method is executed.
+     */
+    protected function tearDown()
+    {
+        
+    }
+
+    /**
+     * the setUpBeforeClass() template methods is called before the first test 
of the test case
+     *  class is run 
+     */
+    public static function setUpBeforeClass()
+    {
+        include 'global.php';
+        
+    }
+
+    /**
+     *  tearDownAfterClass() template methods is calleafter the last test of 
the test case class is run,
+     *
+     */
+    static function tearDownAfterClass()
+    {
+        //        include 'global.php';
+    }
+    public function dataget_Letter()
+    {
+               return array([22,577],[26,343]);
+    }
+     /**
+     * @brief 
+     * @testdox testget_letterAccount Lettering_Account::get_letter
+     * @covers Lettering_Card::get_letter
+    *  @dataProvider dataget_Letter
+     */
+    public function testget_letterAccount($p_fiche_id,$p_jrnx_id)
+    {
+        global $g_connection;
+        $letter = new Lettering_Account($g_connection);
+        $fiche=new Fiche($g_connection,$p_fiche_id);
+       $letter->set_parameter('account', 
$fiche->strAttribut(ATTR_DEF_ACCOUNT));
+       $letter->set_parameter('start', '01.01.2010' );
+       $letter->set_parameter('end', '31.12.2019');
+        
+        // lettered operations by card
+        $letter->get_letter();
+        $this->assertTrue(!empty($letter->content)&& 
is_array($letter->content)," result is not an array");
+        $this->assertTrue(isset($letter->content[0]['currency_id']),"missing 
column currency_id");
+        foreach ($this->a_column as $column) {
+            
$this->assertTrue(array_key_exists($column,$letter->content[0]),"missing column 
$column");
+        }
+        
+        // all operations by card
+        $letter->get_all();
+        
+        $this->assertTrue(!empty($letter->content)&& 
is_array($letter->content)," result is not an array");
+        foreach ($this->a_column as $column) {
+            
$this->assertTrue(array_key_exists($column,$letter->content[0]),"missing column 
$column");
+        }
+        
+        // unlettered operations by card
+        $letter->get_unletter();
+        $this->assertTrue(!empty($letter->content)&& 
is_array($letter->content)," result is not an array");
+        foreach ($this->a_column as $column) {
+            
$this->assertTrue(array_key_exists($column,$letter->content[0]),"missing column 
$column");
+        }
+        
+        // lettered with different amount operations by card
+        $restore=$g_connection->get_value("select j_montant from jrnx where 
j_id=$1",[$p_jrnx_id]);
+        $g_connection->exec_sql("update jrnx set j_montant = j_montant -1 
where j_id=$1 ",[$p_jrnx_id]);
+        $letter->get_letter_diff();
+        
+        $this->assertTrue(!empty($letter->content)&& 
is_array($letter->content)," result is not an array");
+         foreach ($this->a_column as $column) {
+            
$this->assertTrue(array_key_exists($column,$letter->content[0]),"missing column 
$column");
+        }
+        
+        $g_connection->exec_sql("update jrnx set j_montant =$2 where j_id=$1 
",[$p_jrnx_id,$restore]);
+
+    }
+   /**
+     * @brief 
+     * @testdox testget_letterCard Lettering_Card::get_letter
+     * @covers Lettering_Card::get_letter
+    *  @dataProvider dataget_Letter
+     */
+    public function testget_letterCard($p_fiche_id,$p_jrnx_id)
+    {
+        global $g_connection;
+        $letter = new Lettering_Card($g_connection);
+        $fiche=new Fiche($g_connection,$p_fiche_id);
+       $letter->set_parameter('quick_code', 
$fiche->strAttribut(ATTR_DEF_QUICKCODE));
+       $letter->set_parameter('start', '01.01.2010' );
+       $letter->set_parameter('end', '31.12.2019');
+        
+        // lettered operations by card
+        $letter->get_letter();
+        $this->assertTrue(!empty($letter->content)&& 
is_array($letter->content)," result is not an array");
+          foreach ($this->a_column as $column) {
+            
$this->assertTrue(array_key_exists($column,$letter->content[0]),"missing column 
$column");
+        }
+        // all operations by card
+        $letter->get_all();
+        
+        $this->assertTrue(!empty($letter->content)&& 
is_array($letter->content)," result is not an array");
+          foreach ($this->a_column as $column) {
+            
$this->assertTrue(array_key_exists($column,$letter->content[0]),"missing column 
$column");
+        }
+        // unlettered operations by card
+        $letter->get_unletter();
+        $this->assertTrue(!empty($letter->content)&& 
is_array($letter->content)," result is not an array");
+        $this->assertTrue(isset($letter->content[0]['currency_id']),"missing 
column currency_id");
+        foreach ($this->a_column as $column) {
+            
$this->assertTrue(array_key_exists($column,$letter->content[0]),"missing column 
$column");
+        }
+        // lettered with different amount operations by card
+        $restore=$g_connection->get_value("select j_montant from jrnx where 
j_id=$1",[$p_jrnx_id]);
+        $g_connection->exec_sql("update jrnx set j_montant = j_montant -1 
where j_id=$1 ",[$p_jrnx_id]);
+        $letter->get_letter_diff();
+        
+        $this->assertTrue(!empty($letter->content)&& 
is_array($letter->content)," result is not an array");
+          foreach ($this->a_column as $column) {
+            
$this->assertTrue(array_key_exists($column,$letter->content[0]),"missing column 
$column");
+        }
+        $g_connection->exec_sql("update jrnx set j_montant =$2 where j_id=$1 
",[$p_jrnx_id,$restore]);
+
+    }
+    public function dataDateLimit()
+    {
+        return array(
+            [100,'01.01.2018','31.12.2018'],
+            [125,'01.01.2019','31.12.2019'],
+            [120,'01.01.2019','31.12.2019']
+        );
+    }
+    /**
+     * @testdox testDateLimitComputed Check the default date
+     * @global type $g_connection
+     * @param type $p_id
+     * @dataProvider dataDateLimit
+     */
+    public function 
testDateLimitComputed($p_periode_id,$p_first_day,$p_last_day)
+    {
+        global $g_connection;
+        $user=new User($g_connection);
+        $restore=$user->get_periode();
+        $user->set_periode($p_periode_id);
+        
+        $lettering=new Lettering($g_connection);
+        $lettering->set_parameter('quick_code', 'FOURNI');
+        
$this->assertEquals($p_first_day,$lettering->get_parameter("start"),"first day 
incorrect");
+        $this->assertEquals($p_last_day,$lettering->get_parameter("end"),"last 
day incorrect");
+        
+        $lettering=new Lettering_Card($g_connection);
+        $lettering->set_parameter('quick_code', 'FOURNI');
+        
$this->assertEquals($p_first_day,$lettering->get_parameter("start"),"first day 
incorrect");
+        $this->assertEquals($p_last_day,$lettering->get_parameter("end"),"last 
day incorrect");
+        
+        $lettering=new Lettering_Account($g_connection);
+        $lettering->set_parameter('quick_code', 'FOURNI');
+        
$this->assertEquals($p_first_day,$lettering->get_parameter("start"),"first day 
incorrect");
+        $this->assertEquals($p_last_day,$lettering->get_parameter("end"),"last 
day incorrect");
+    }
+    /**
+     * @testdox testDateLimitGiven Check the default date
+     * @global type $g_connection
+     * @param type $p_id
+     * @dataProvider dataDateLimit
+     */
+    public function testDateLimitGiven($p_periode_id,$p_first_day,$p_last_day)
+    {
+        global $g_connection;
+        
+        $lettering=new Lettering($g_connection);
+        $lettering->set_parameter('quick_code', 'FOURNI');
+        $lettering->set_parameter('start', $p_first_day);
+        $lettering->set_parameter('end',$p_last_day);
+        
$this->assertEquals($p_first_day,$lettering->get_parameter("start"),"first day 
incorrect");
+        $this->assertEquals($p_last_day,$lettering->get_parameter("end"),"last 
day incorrect");
+        
+        $lettering=new Lettering_Card($g_connection);
+        $lettering->set_parameter('quick_code', 'FOURNI');
+        $lettering->set_parameter('start', $p_first_day);
+        $lettering->set_parameter('end',$p_last_day);
+        
$this->assertEquals($p_first_day,$lettering->get_parameter("start"),"first day 
incorrect");
+        $this->assertEquals($p_last_day,$lettering->get_parameter("end"),"last 
day incorrect");
+        
+        $lettering=new Lettering_Account($g_connection);
+        $lettering->set_parameter('quick_code', 'FOURNI');
+        $lettering->set_parameter('start', $p_first_day);
+        $lettering->set_parameter('end',$p_last_day);
+        
$this->assertEquals($p_first_day,$lettering->get_parameter("start"),"first day 
incorrect");
+        $this->assertEquals($p_last_day,$lettering->get_parameter("end"),"last 
day incorrect");
+    }
+
+}
diff --git a/unit-test/include/class/user.Test.php 
b/unit-test/include/class/user.Test.php
index f8e1272..68836b6 100644
--- a/unit-test/include/class/user.Test.php
+++ b/unit-test/include/class/user.Test.php
@@ -113,4 +113,30 @@ class UserTest extends TestCase
         // check his removal
         $this->assertEquals($cn->get_value('select count(*) from profile_user 
where user_name=$1',array('unknown/user')),0);
     }
+    public function dataPeriode()
+    {
+        return array(
+            [92],
+            [103],
+            [105],
+            [107],
+            [108],
+            [137],
+            [1137]
+        );
+    }
+    /**
+     * 
+     * @param type $p_id periode id
+     * @dataProvider dataPeriode
+     */
+    public function testPeriode($p_id)
+    {
+        global $g_connection; 
+        $this->object->db=$g_connection;
+        $restore=$this->object->get_periode();
+        $this->assertTrue(is_numeric($restore),"Old periode id is not an 
integer");
+        $this->object->set_periode($p_id);
+        $this->assertEquals($p_id,$this->object->get_periode(),"Cannot 
retrieve the right periode");
+    }
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]