dolibarr/test/phpunit/ContratTest.php

232 lines
5.8 KiB
PHP
Raw Permalink Normal View History

2010-04-27 13:21:34 +02:00
<?php
/* Copyright (C) 2010-2014 Laurent Destailleur <eldy@users.sourceforge.net>
2010-04-27 13:21:34 +02:00
*
* 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
2010-04-27 13:21:34 +02:00
* (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
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* or see https://www.gnu.org/
2010-04-27 13:21:34 +02:00
*/
/**
2010-08-16 01:19:46 +02:00
* \file test/phpunit/ContratTest.php
2010-04-27 13:21:34 +02:00
* \ingroup test
2010-10-03 20:53:40 +02:00
* \brief PHPUnit test
2010-04-27 13:21:34 +02:00
* \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';
2010-08-16 01:19:46 +02:00
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
require_once dirname(__FILE__).'/../../htdocs/contrat/class/contrat.class.php';
2010-04-27 13:21:34 +02:00
if (empty($user->id))
{
2019-02-25 00:56:48 +01:00
print "Load permissions for admin user nb 1\n";
$user->fetch(1);
$user->getrights();
2010-04-27 13:21:34 +02:00
}
2010-07-15 01:10:56 +02:00
$conf->global->MAIN_DISABLE_ALL_MAILS=1;
2010-04-27 13:21:34 +02:00
/**
2011-09-23 14:21:00 +02:00
* Class for PHPUnit tests
*
2010-04-27 13:21:34 +02:00
* @backupGlobals disabled
* @backupStaticAttributes enabled
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
*/
2019-07-05 21:28:27 +02:00
class ContratTest extends PHPUnit\Framework\TestCase
2010-04-27 13:21:34 +02:00
{
2019-02-25 00:56:48 +01:00
protected $savconf;
protected $savuser;
protected $savlangs;
protected $savdb;
/**
* Constructor
* We save global variables into local variables
*
* @return ContratTest
*/
public function __construct()
2010-04-27 13:21:34 +02:00
{
2019-02-25 00:56:48 +01:00
parent::__construct();
2010-04-27 13:21:34 +02:00
2019-02-25 00:56:48 +01:00
//$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";
}
2020-05-03 22:47:43 +02:00
/**
* setUpBeforeClass
*
* @return void
*/
2019-02-25 00:56:48 +01:00
public static function setUpBeforeClass()
{
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";
2010-04-27 13:21:34 +02:00
}
2015-01-06 17:54:36 +01:00
2020-05-03 22:47:43 +02:00
/**
* tearDownAfterClass
*
* @return void
*/
2010-04-27 13:21:34 +02:00
public static function tearDownAfterClass()
{
2019-02-25 00:56:48 +01:00
global $conf,$user,$langs,$db;
$db->rollback();
2010-04-27 13:21:34 +02:00
2019-02-25 00:56:48 +01:00
print __METHOD__."\n";
2010-04-27 13:21:34 +02:00
}
2019-02-25 00:56:48 +01:00
/**
* Init phpunit tests
*
* @return void
*/
2010-04-27 13:21:34 +02:00
protected function setUp()
{
2019-02-25 00:56:48 +01:00
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
2010-04-27 13:21:34 +02:00
2019-02-25 00:56:48 +01:00
print __METHOD__."\n";
2010-04-27 13:21:34 +02:00
}
2019-02-25 00:56:48 +01:00
/**
* End phpunit tests
*
* @return void
*/
2010-04-27 13:21:34 +02:00
protected function tearDown()
{
2019-02-25 00:56:48 +01:00
print __METHOD__."\n";
2010-04-27 13:21:34 +02:00
}
/**
2012-02-17 16:02:35 +01:00
* testContratCreate
*
2012-02-17 16:02:35 +01:00
* @return int
2010-04-27 13:21:34 +02:00
*/
public function testContratCreate()
{
2019-02-25 00:56:48 +01:00
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
2010-04-27 13:21:34 +02:00
2019-02-25 00:56:48 +01:00
$localobject=new Contrat($this->savdb);
$localobject->initAsSpecimen();
$result=$localobject->create($user);
2019-02-25 00:56:48 +01:00
print __METHOD__." result=".$result."\n";
$this->assertLessThan($result, 0);
2019-02-25 00:56:48 +01:00
return $result;
2010-04-27 13:21:34 +02:00
}
/**
2012-02-17 16:02:35 +01:00
* testContratFetch
*
2012-02-17 16:02:35 +01:00
* @param int $id Id of contract
* @return int
*
2010-04-27 13:21:34 +02:00
* @depends testContratCreate
* The depends says test is run only if previous is ok
*/
public function testContratFetch($id)
{
2019-02-25 00:56:48 +01:00
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
2010-04-27 13:21:34 +02:00
2019-02-25 00:56:48 +01:00
$localobject=new Contrat($this->savdb);
$result=$localobject->fetch($id);
2019-02-25 00:56:48 +01:00
print __METHOD__." id=".$id." result=".$result."\n";
$this->assertLessThan($result, 0);
2019-02-25 00:56:48 +01:00
return $localobject;
2010-04-27 13:21:34 +02:00
}
2019-10-20 11:59:21 +02:00
/**
2015-01-04 19:26:51 +01:00
* testContratOther
*
2012-02-17 16:02:35 +01:00
* @param Object $localobject Object contract
* @return int
*
2015-01-04 19:26:51 +01:00
* @depends testContratFetch
2010-10-13 21:01:22 +02:00
* The depends says test is run only if previous is ok
*/
public function testContratOther($localobject)
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
/*$result=$localobject->setstatus(0);
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0);
*/
$localobject->info($localobject->id);
print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
$this->assertNotEquals($localobject->date_creation, '');
return $localobject->id;
}
/**
2012-02-17 16:02:35 +01:00
* testContratDelete
*
2012-02-17 16:02:35 +01:00
* @param int $id Id of contract
* @return int
*
2010-10-13 21:01:22 +02:00
* @depends testContratOther
2010-04-27 13:21:34 +02:00
* The depends says test is run only if previous is ok
*/
public function testContratDelete($id)
{
2019-02-25 00:56:48 +01:00
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new Contrat($this->savdb);
$result=$localobject->fetch($id);
$result=$localobject->delete($user);
print __METHOD__." id=".$id." result=".$result."\n";
$this->assertLessThan($result, 0);
return $result;
2010-04-27 13:21:34 +02:00
}
}