Files
dolibarr/test/phpunit/DiscountTest.php
Alexandre Janniaux a0a3339eb9 DiscountTest: fix create when no societe exists
Fix the following error when running without the dataset.

    ✘ Discount create
    ┐
    ├ ERROR:  23503: insert or update on table "llx_societe_remise_except" violates foreign key constraint "fk_soc_remise_fk_soc"                                                                                                                                                                                                                                                                                                                                      
    ├ DETAIL:  Key (fk_soc)=(1) is not present in table "llx_societe".                                                                                                                                                                                                                                                                                                                                                                                                 
    ├ SCHEMA NAME:  public                                                                                                                                                                                                                                                                                                                                                                                                                                             
    ├ TABLE NAME:  llx_societe_remise_except                                                                                                                                                                                                                                                                                                                                                                                                                           
    ├ CONSTRAINT NAME:  fk_soc_remise_fk_soc                                                                                                                                                                                                                                                                                                                                                                                                                           
    ├ LOCATION:  ri_ReportViolation, ri_triggers.c:2596 - sql=INSERT INTO llx_societe_remise_except (entity, datec, fk_soc, discount_type, fk_user, description, amount_ht, amount_tva, amount_ttc, tva_tx, vat_src_code, multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc, fk_facture_source, fk_invoice_supplier_source) VALUES (1, '2024-01-05 14:05:20', 1, 0, 1, 'Specimen discount', 10, 1.96, 11.96, 19.6, '', 0, 0, 0,  null, null),
    ├ Failed asserting that 0 is less than -1.                                                                                                                                                                                                                                                                                                                                                                                                                         
    │
    │ dolibarr/test/phpunit/DiscountTest.php:147
    ┴
2024-01-05 15:05:33 +01:00

209 lines
4.8 KiB
PHP

<?php
/* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
* or see https://www.gnu.org/
*/
/**
* \file test/phpunit/DiscountTest.php
* \ingroup test
* \brief PHPUnit test
* \remarks To run this script as CLI: phpunit filename.php
*/
global $conf,$user,$langs,$db;
//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
//require_once 'PHPUnit/Autoload.php';
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
require_once dirname(__FILE__).'/../../htdocs/core/class/discount.class.php';
if (empty($user->id)) {
print "Load permissions for admin user nb 1\n";
$user->fetch(1);
$user->getrights();
}
$conf->global->MAIN_DISABLE_ALL_MAILS=1;
/**
* Class for PHPUnit tests
*
* @backupGlobals disabled
* @backupStaticAttributes enabled
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
*/
class DiscountTest extends PHPUnit\Framework\TestCase
{
protected $savconf;
protected $savuser;
protected $savlangs;
protected $savdb;
/**
* Constructor
* We save global variables into local variables
*
* @param string $name Name
* @return DiscountTest
*/
public function __construct($name = '')
{
parent::__construct($name);
//$this->sharedFixture
global $conf,$user,$langs,$db;
$this->savconf=$conf;
$this->savuser=$user;
$this->savlangs=$langs;
$this->savdb=$db;
print __METHOD__." db->type=".$db->type." user->id=".$user->id;
//print " - db ".$db->db;
print "\n";
}
/**
* setUpBeforeClass
*
* @return void
*/
public static function setUpBeforeClass(): void
{
global $conf,$user,$langs,$db;
$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
print __METHOD__."\n";
}
/**
* tearDownAfterClass
*
* @return void
*/
public static function tearDownAfterClass(): void
{
global $conf,$user,$langs,$db;
$db->rollback();
print __METHOD__."\n";
}
/**
* Init phpunit tests
*
* @return void
*/
protected function setUp(): void
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
print __METHOD__."\n";
//print $db->getVersion()."\n";
}
/**
* End phpunit tests
*
* @return void
*/
protected function tearDown(): void
{
print __METHOD__."\n";
}
/**
* testDiscountCreate
*
* @return int
*/
public function testDiscountCreate()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$soc = new Societe($db);
$soc->name = "CommandeTest Unittest";
$socid = $soc->create($user);
$this->assertLessThan($socid, 0, $soc->errorsToString());
$localobject=new DiscountAbsolute($db);
$localobject->initAsSpecimen();
$localobject->fk_soc = $socid;
$result=$localobject->create($user);
$this->assertLessThan($result, 0);
print __METHOD__." result=".$result."\n";
return $result;
}
/**
* testDiscountFetch
*
* @param int $id Id of discount
* @return int
*
* @depends testDiscountCreate
* The depends says test is run only if previous is ok
*/
public function testDiscountFetch($id)
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new DiscountAbsolute($db);
$result=$localobject->fetch($id);
$this->assertLessThan($result, 0);
print __METHOD__." id=".$id." result=".$result."\n";
return $id;
}
/**
* testDiscountDelete
*
* @param int $id Id of discount
* @return int
*
* @depends testDiscountFetch
* The depends says test is run only if previous is ok
*/
public function testDiscountDelete($id)
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new DiscountAbsolute($db);
$result=$localobject->fetch($id);
$result=$localobject->delete($user);
print __METHOD__." id=".$id." result=".$result."\n";
$this->assertLessThan($result, 0);
return $result;
}
}