From c34a5eda05fa3b2823a1d908acac23dc705227a1 Mon Sep 17 00:00:00 2001 From: sparkyx Date: Sun, 26 Oct 2025 13:38:30 +0100 Subject: [PATCH] PHPUNIT : add UBL21 and FacturX --- unit-test/include/class/FacturXTest.php | 142 +++++++++++++++ unit-test/include/class/InvoiceUBL21Test.php | 175 +++++++++++++++++++ 2 files changed, 317 insertions(+) create mode 100644 unit-test/include/class/FacturXTest.php create mode 100644 unit-test/include/class/InvoiceUBL21Test.php diff --git a/unit-test/include/class/FacturXTest.php b/unit-test/include/class/FacturXTest.php new file mode 100644 index 000000000..60ab28c8c --- /dev/null +++ b/unit-test/include/class/FacturXTest.php @@ -0,0 +1,142 @@ + + * + */ + +/** + * @file + * @brief noalyss + */ +use PHPUnit\Framework\TestCase; + + +/** + * @testdox Class InvoiceUBL21 : used for managing invoice format UBL21 + * @backupGlobals false + * @coversDefaultClass InvoiceUBL21Test + */ +class FacturXTest extends TestCase { + + /** + * @var Fiche + */ + protected $object; + protected $connection; + + /** + * @testdox thx jr_id makes an $array + * @covers \Noalyss\Invoice\XMLDocument\InvoiceUBL21::build_data + */ + function testBuild_Data() { + $cn=\Dossier::connect(); + $facturx=new \Noalyss\XMLDocument\FacturX($cn); + } + + + /** + * @testdox Make an XML UBL21 + */ + function testDOMMake_XML() + { + + $cn = \Dossier::connect(); + // clean data + $cn->exec_sql('update jrn set jr_pj_name=null,jr_pj_type=null, jr_pj = null where jr_id=$1', [2]); + + /** + * create PDF Invoice + */ + $sold = new \Acc_Sold($cn, 2); + $sold->get(); + $array = \Acc_Ledger_Sale::convert_to_array($sold); + $array['gen_doc'] = -2; //<- Standard invoice + $acc_document = new \Acc_Document($cn, 2); + $acc_document->create_document($sold->det->jr_internal, $array); + // verification du PDF dans DB + $row = $cn->get_row("select jr_pj_name,jr_pj, jr_pj_type + from jrn + where jr_id=$1 + ", [2]); + + $this->assertTrue("inv-std-VEN2.pdf" == $row['jr_pj_name'], "Incorrect invoice name [{$row['jr_pj_name']}]"); + $this->assertTrue("application/pdf" == $row['jr_pj_type'], "Incorrect invoice type"); + $this->assertTrue($row['jr_pj'] != "", "OID not created"); + + /** + * create invoice PDF + */ + $sold=new \Acc_Sold($cn,2); + $sold->get(); + $array= \Acc_Ledger_Sale::convert_to_array($sold); + $array['gen_doc']=-2; //<- Standard invoice + $acc_document=new \Acc_Document($cn,2); + $acc_document->create_document($sold->det->jr_internal, $array); + // verification du PDF dans DB + $row=$cn->get_row("select jr_pj_name,jr_pj, jr_pj_type + from jrn + where jr_id=$1 + ",[2]); + + $this->assertTrue("inv-std-VEN2.pdf"==$row['jr_pj_name'],"Incorrect invoice name [{$row['jr_pj_name']}]"); + $this->assertTrue("application/pdf"==$row['jr_pj_type'],"Incorrect invoice type"); + $this->assertTrue($row['jr_pj'] != "","OID not created"); + // get the file + $cn->start(); + $pdf_filename= tempnam("/tmp", "phpunit"); + \Noalyss\Facility::save_file ('/tmp', __FUNCTION__.".txt", $pdf_filename); + $acc_document->export_file($pdf_filename); + $cn->commit(); + // make the facturX invoice + $facturx = new \Noalyss\XMLDocument\FacturX($cn); + $facturx->set_pdf_filename($pdf_filename); + + $xml = $facturx->create_invoice(2); + $oid=$cn->lo_write($xml); + $this->assertTrue($oid != false , "cannot save XML in DB"); + $acc_document->replace_receipt($oid); + $file= \Noalyss\Facility::save_file('/tmp', 'facturX', $xml); + + } + + /** + * @brief to verify : MY_COUNTRY_CODE, SIREN, SIRET + * + */ + function testCompany_Data() + { + $cn=\Dossier::connect(); + $facturx=new \Noalyss\XMLDocument\FacturX($cn); + $a_error=$facturx->check_company_data(); + $this->assertTrue(count($a_error)==10, "nb of error incorrect ".print_r($a_error,true)); + } + + function testCustomer_Data() + { + $cn=\Dossier::connect(); + $facturx=new \Noalyss\XMLDocument\FacturX($cn); + $facturx->build_data(2); + $customer=$facturx->get_data()['customer']; + $a_error = $facturx->check_customer_data($customer['card_id']); + print_r($a_error); + $this->assertTrue(count($a_error)==6, "nb of error incorrect ,expected 6, received = ".print_r($a_error,true)); + } +} diff --git a/unit-test/include/class/InvoiceUBL21Test.php b/unit-test/include/class/InvoiceUBL21Test.php new file mode 100644 index 000000000..a53eace33 --- /dev/null +++ b/unit-test/include/class/InvoiceUBL21Test.php @@ -0,0 +1,175 @@ + + * + */ + +/** + * @file + * @brief noalyss + */ +use PHPUnit\Framework\TestCase; + + +/** + * @testdox Class InvoiceUBL21 : used for managing invoice format UBL21 + * @backupGlobals false + * @coversDefaultClass InvoiceUBL21Test + */ +class InvoiceUBL21Test extends TestCase { + + /** + * @var Fiche + */ + protected $object; + protected $connection; + + + /** + * @testdox thx jr_id makes an $array + * @covers \Noalyss\Invoice\XMLDocument\InvoiceUBL21::build_data + */ + function testBuild_Data() { + $cn = \Dossier::connect(); + $ublinvoice21 = new \Noalyss\XMLDocument\InvoiceUBL21($cn); + } + + /** + * @brief check that the parameter extra has a value or add a temporary one + * @param $code (string) code to check + * @param $value (string) temp value + * @return true : operation succeed , false code doesn't exist + */ + function parameter_extra_set($code,$value) + { + $cn = \Dossier::connect(); + $c=$cn->get_value("select pe_value from parameter_extra where pe_code = $1", + [$code]); + $id=1; + if ( $c == "") { + $id = $cn->get_value(" update parameter_extra set pe_value=$1 where pe_code=$2 returning id", + [$code,$value]); + + } + return ($id != "")?true:false; + } + + /** + * @brief if parameter extra has a temp value reset it to null + * @param $code (string) code to check + * @param $value (string) temp value + * @return true : operation succeed , false code doesn't exist + */ + function parameter_extra_clean($code,$value) + { + $cn = \Dossier::connect(); + $c=$cn->get_value("select pe_value from parameter_extra where pe_code = $1", + [$code]); + $id=1; + if ( $c == $value ) { + $id = $cn->get_value(" update parameter_extra set pe_value=null where pe_code=$1 returning id", + [$code]); + + } + return ($id != "")?true:false; + } + /** + * @testdox Make an XML UBL21, create the PDF invoice and include it into the XML + */ + function testDOMMake_XML() { + try { + + $cn = \Dossier::connect(); + // clean first + $cn->exec_sql('update jrn set jr_pj_name=null,jr_pj_type=null, jr_pj = null where jr_id=$1',[2]); + $tmp_value= uniqid("phpunit"); + // COMPANY_BANK_IBAN and COMPANY_BANK_BIC must have a value + + if ( ! $this->parameter_extra_set('COMPANY_BANK_IBAN',$tmp_value)) + { + $this->assertTrue(false,"failed to set COMPANY_BANK_IBAN"); + } + + if ( ! $this->parameter_extra_set('COMPANY_BANK_BIC',$tmp_value) ) + { + $this->assertTrue(false,"failed to set COMPANY_BANK_IBAN"); + } + + $cn->start(); + $ublinvoice21 = new \Noalyss\XMLDocument\InvoiceUBL21($cn); + // create document + $sold=new \Acc_Sold($cn,2); + $sold->get(); + $array= \Acc_Ledger_Sale::convert_to_array($sold); + $array['gen_doc']=-2; //<- Standard invoice + $acc_document=new \Acc_Document($cn,2); + $acc_document->create_document($sold->det->jr_internal, $array); + $cn->commit(); + // verification du PDF dans DB + $row=$cn->get_row("select jr_pj_name,jr_pj, jr_pj_type + from jrn + where jr_id=$1 + ",[2]); + + $this->assertTrue("inv-std-VEN2-pdf"==$row['jr_pj_name'],"Incorrect invoice name"); + $this->assertTrue("application/pdf"==$row['jr_pj_type'],"Incorrect invoice type"); + $this->assertTrue($row['jr_pj'] != "","OID not created"); + $cn->start(); + $pdf_filename= tempnam("/tmp", "phpunit"); + \Noalyss\Facility::save_file ('/tmp', __FUNCTION__.".txt", $pdf_filename); + $acc_document->export_file($pdf_filename); + $cn->commit(); + + + $ublinvoice21->set_pdf_filename($pdf_filename); + $ublinvoice21->make_xml(2); + $data_id = $ublinvoice21->get_data()['id']; + $file = fopen("invoice-" . $data_id . ".xml", "w+"); + fwrite($file, $ublinvoice21->saveXML()); + fclose($file); + $this->assertTrue(file_exists("invoice-" . $data_id . ".xml") + ,"file not create"); + echo "save saved invoice {$data_id}.xml \n"; + $this->parameter_extra_clean('COMPANY_BANK_IBAN',$tmp_value); + $this->parameter_extra_clean('COMPANY_BANK_BIC',$tmp_value); + + } catch (Exception $exc) { + echo "ERROR" ; ///$exc->getTraceAsString(); + } + } + + function testCompany_Data() { + $cn = \Dossier::connect(); + $ublinvoice21 = new \Noalyss\XMLDocument\InvoiceUBL21($cn); + $a_error=array(); + $a_error=$ublinvoice21->check_company_data(); + $this->assertTrue(count($a_error) ==8 , " nb of errors incorrect ".print_r($a_error,true)); + } + + function testCustomer_Data() { + $cn = \Dossier::connect(); + $ublinvoice21 = new \Noalyss\XMLDocument\InvoiceUBL21($cn); + $ublinvoice21->build_data(2); + $customer = $ublinvoice21->get_data()['customer']; + $a_error=$ublinvoice21->check_customer_data($customer['card_id']); + $this->assertTrue(count($a_error) ==6 , " nb of errors incorrect ".print_r($a_error,true)); + } +}