2
0
forked from Wavyzz/dolibarr

Enhance PHPUnits

This commit is contained in:
Laurent Destailleur
2011-12-30 12:39:44 +01:00
parent ce8f97e4aa
commit 51479c7894
6 changed files with 338 additions and 36 deletions

View File

@@ -134,7 +134,7 @@ class Contact extends CommonObject
$sql.= " ".$conf->entity;
$sql.= ")";
dol_syslog("Contact::create sql=".$sql);
dol_syslog(get_class($this)."::create sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
@@ -178,7 +178,7 @@ class Contact extends CommonObject
else
{
$this->db->rollback();
dol_syslog("Contact::create ".$this->error, LOG_ERR);
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
return -2;
}
}
@@ -187,7 +187,7 @@ class Contact extends CommonObject
$this->error=$this->db->lasterror();
$this->db->rollback();
dol_syslog("Contact::create ".$this->error, LOG_ERR);
dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
return -1;
}
}
@@ -208,7 +208,7 @@ class Contact extends CommonObject
$this->id = $id;
// Nettoyage parametres
// Clean parameters
$this->name=trim($this->name);
$this->firstname=trim($this->firstname);
@@ -217,8 +217,11 @@ class Contact extends CommonObject
$this->phone_perso=trim($this->phone_perso);
$this->phone_mobile=trim($this->phone_mobile);
$this->fax=trim($this->fax);
$this->zip=($this->zip?$this->zip:$this->cp);
$this->town=($this->town?$this->town:$this->ville);
$this->country_id=($this->country_id > 0?$this->country_id:$this->fk_pays);
$this->state_id=($this->state_id > 0?$this->state_id:$this->fk_departement);
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET ";
@@ -231,7 +234,7 @@ class Contact extends CommonObject
$sql .= ", cp='".$this->db->escape($this->zip)."'";
$sql .= ", ville='".$this->db->escape($this->town)."'";
$sql .= ", fk_pays=".($this->country_id>0?$this->country_id:'NULL');
$sql .= ", fk_departement=".($this->fk_departement>0?$this->fk_departement:'NULL');
$sql .= ", fk_departement=".($this->state_id>0?$this->state_id:'NULL');
$sql .= ", poste='".$this->db->escape($this->poste)."'";
$sql .= ", fax='".$this->db->escape($this->fax)."'";
$sql .= ", email='".$this->db->escape($this->email)."'";
@@ -245,7 +248,7 @@ class Contact extends CommonObject
$sql .= ", default_lang=".($this->default_lang?"'".$this->default_lang."'":"null");
$sql .= " WHERE rowid=".$id;
dol_syslog("Contact::update sql=".$sql,LOG_DEBUG);
dol_syslog(get_class($this)."::update sql=".$sql,LOG_DEBUG);
$result = $this->db->query($sql);
if ($result)
{
@@ -267,7 +270,7 @@ class Contact extends CommonObject
else
{
$this->error=join(',',$this->errors);
dol_syslog("Contact::update Error ".$this->error,LOG_ERR);
dol_syslog(get_class($this)."::update Error ".$this->error,LOG_ERR);
$this->db->rollback();
return -$error;
}
@@ -275,7 +278,7 @@ class Contact extends CommonObject
else
{
$this->error=$this->db->lasterror().' sql='.$sql;
dol_syslog("Contact::update Error ".$this->error,LOG_ERR);
dol_syslog(get_class($this)."::update Error ".$this->error,LOG_ERR);
$this->db->rollback();
return -1;
}
@@ -390,7 +393,7 @@ class Contact extends CommonObject
if ($user) $sql .= ", fk_user_modif=".$user->id;
$sql .= " WHERE rowid=".$id;
//print "update_perso: ".$this->birthday.'-'.$this->db->idate($this->birthday);
dol_syslog("Contact::update_perso this->birthday=".$this->birthday." - sql=".$sql);
dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." - sql=".$sql);
$resql = $this->db->query($sql);
if (! $resql)
{
@@ -468,7 +471,7 @@ class Contact extends CommonObject
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
$sql.= " WHERE c.rowid = ". $id;
dol_syslog("Contact::fetch sql=".$sql);
dol_syslog(get_class($this)."::fetch sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
@@ -547,7 +550,7 @@ class Contact extends CommonObject
else
{
$this->error=$this->db->error();
dol_syslog("Contact::fetch ".$this->error, LOG_ERR);
dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
return -1;
}
@@ -572,7 +575,7 @@ class Contact extends CommonObject
else
{
$this->error=$this->db->error();
dol_syslog("Contact::fetch ".$this->error, LOG_ERR);
dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
return -1;
}
}
@@ -588,7 +591,7 @@ class Contact extends CommonObject
else
{
$this->error=$this->db->error();
dol_syslog("Contact::fetch ".$this->error, LOG_ERR);
dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
return -1;
}
}
@@ -612,7 +615,7 @@ class Contact extends CommonObject
$sql.=" AND fk_socpeople = ". $this->id;
$sql.=" GROUP BY tc.element";
dol_syslog("Contact::load_ref_elements sql=".$sql);
dol_syslog(get_class($this)."::load_ref_elements sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
@@ -633,7 +636,7 @@ class Contact extends CommonObject
else
{
$this->error=$this->db->error()." - ".$sql;
dol_syslog("Contact::load_ref_elements Error ".$this->error, LOG_ERR);
dol_syslog(get_class($this)."::load_ref_elements Error ".$this->error, LOG_ERR);
return -1;
}
}
@@ -664,7 +667,7 @@ class Contact extends CommonObject
$sql.= " WHERE ec.fk_socpeople=".$this->id;
$sql.= " AND ec.fk_c_type_contact=tc.rowid";
$sql.= " AND tc.source='external'";
dol_syslog("Contact::delete sql=".$sql);
dol_syslog(get_class($this)."::delete sql=".$sql);
$resql = $this->db->query($sql);
if ($resql)
{
@@ -677,7 +680,7 @@ class Contact extends CommonObject
$sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
$sqldel.=" WHERE rowid = ".$obj->rowid;
dol_syslog("Contact::delete sql=".$sqldel);
dol_syslog(get_class($this)."::delete sql=".$sqldel);
$result = $this->db->query($sqldel);
if (! $result)
{
@@ -699,7 +702,7 @@ class Contact extends CommonObject
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople";
$sql .= " WHERE rowid=".$this->id;
dol_syslog("Contact::delete sql=".$sql);
dol_syslog(get_class($this)."::delete sql=".$sql);
$result = $this->db->query($sql);
if (! $result)
{
@@ -1035,16 +1038,14 @@ class Contact extends CommonObject
// Initialise parameters
$this->id=0;
$this->specimen=1;
$this->nom = 'DOLIBARR';
$this->name = $this->nom;
$this->prenom = 'SPECIMEN';
$this->firstname = $this->prenom;
$this->name = 'DOLIBARR';
$this->firstname = 'SPECIMEN';
$this->address = '61 jump street';
$this->cp = '75000';
$this->ville = 'Paris';
$this->fk_pays = 1;
$this->pays_code = 'FR';
$this->pays = 'France';
$this->zip = '75000';
$this->town = 'Paris';
$this->country_id = 1;
$this->country_code = 'FR';
$this->country = 'France';
$this->email = 'specimen@specimen.com';
$socid = rand(1, $num_socs);
$this->socid = $socids[$socid];

View File

@@ -221,16 +221,16 @@ abstract class ActionsCardCommon
{
if ($this->object->particulier)
{
dol_syslog("This thirdparty is a personal people",LOG_DEBUG);
dol_syslog(get_class($this)."::doActions This thirdparty is a personal people",LOG_DEBUG);
$contact=new Contact($this->db);
$contact->civilite_id = $this->object->civilite_id;
$contact->name = $this->object->name_bis;
$contact->firstname = $this->object->firstname;
$contact->address = $this->object->address;
$contact->cp = $this->object->cp;
$contact->ville = $this->object->ville;
$contact->fk_pays = $this->object->fk_pays;
$contact->zip = $this->object->zip;
$contact->town = $this->object->town;
$contact->country_id = $this->object->country_id;
$contact->socid = $this->object->id; // fk_soc
$contact->status = 1;
$contact->email = $this->object->email;

View File

@@ -234,11 +234,9 @@ if (empty($reshook))
$contact->firstname = $object->firstname;
$contact->address = $object->address;
$contact->zip = $object->zip;
$contact->cp = $object->cp; // TODO obsolete
$contact->town = $object->town;
$contact->ville = $object->ville; // TODO obsolete
$contact->fk_departement = $object->state_id;
$contact->fk_pays = $object->country_id;
$contact->state_id = $object->state_id;
$contact->country_id = $object->country_id;
$contact->socid = $object->id; // fk_soc
$contact->status = 1;
$contact->email = $object->email;

View File

@@ -74,6 +74,8 @@ class AllTests
require_once dirname(__FILE__).'/SocieteTest.php';
$suite->addTestSuite('SocieteTest');
require_once dirname(__FILE__).'/ContactTest.php';
$suite->addTestSuite('ContactTest');
require_once dirname(__FILE__).'/AdherentTest.php';
$suite->addTestSuite('AdherentTest');

299
test/phpunit/ContactTest.php Executable file
View File

@@ -0,0 +1,299 @@
<?php
/* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
*
* 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, see <http://www.gnu.org/licenses/>.
* or see http://www.gnu.org/
*/
/**
* \file test/phpunit/ContactTest.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/societe/class/societe.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 ContactTest extends PHPUnit_Framework_TestCase
{
protected $savconf;
protected $savuser;
protected $savlangs;
protected $savdb;
/**
* Constructor
* We save global variables into local variables
*
* @return ContactTest
*/
function ContactTest()
{
//$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";
}
// Static methods
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";
}
public static function tearDownAfterClass()
{
global $conf,$user,$langs,$db;
$db->rollback();
print __METHOD__."\n";
}
/**
*/
protected function setUp()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
print __METHOD__."\n";
}
/**
*/
protected function tearDown()
{
print __METHOD__."\n";
}
/**
*/
public function testContactCreate()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new Contact($this->savdb);
$localobject->initAsSpecimen();
$result=$localobject->create($user);
print __METHOD__." result=".$result."\n";
$this->assertLessThanOrEqual($result, 0);
return $result;
}
/**
* @depends testContactCreate
* The depends says test is run only if previous is ok
*/
public function testContactFetch($id)
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new Contact($this->savdb);
$result=$localobject->fetch($id);
print __METHOD__." id=".$id." result=".$result."\n";
$this->assertLessThan($result, 0);
$result=$localobject->verify();
print __METHOD__." id=".$id." result=".$result."\n";
$this->assertEquals($result, 0);
return $localobject;
}
/**
* @depends testContactFetch
* The depends says test is run only if previous is ok
*/
public function testContactUpdate($localobject)
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject->note='New note after update';
//$localobject->note_public='New note public after update';
$localobject->name='New name';
$localobject->address='New address';
$localobject->zip='New zip';
$localobject->town='New town';
$localobject->country_id=2;
$localobject->status=0;
$localobject->tel='New tel';
$localobject->fax='New fax';
$localobject->email='New email';
$localobject->url='New url';
$localobject->idprof1='new idprof1';
$localobject->idprof2='new idprof2';
$localobject->idprof3='new idprof3';
$localobject->idprof4='new idprof4';
$result=$localobject->update($localobject->id,$user);
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0);
$result=$localobject->update_note($localobject->note);
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0);
//$result=$localobject->update_note_public($localobject->note_public);
//print __METHOD__." id=".$localobject->id." result=".$result."\n";
//$this->assertLessThan($result, 0);
$newobject=new Contact($this->savdb);
$result=$newobject->fetch($localobject->id);
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0);
$this->assertEquals($localobject->note, $newobject->note);
//$this->assertEquals($localobject->note_public, $newobject->note_public);
$this->assertEquals($localobject->name, $newobject->name);
$this->assertEquals($localobject->address, $newobject->address);
$this->assertEquals($localobject->zip, $newobject->zip);
$this->assertEquals($localobject->town, $newobject->town);
$this->assertEquals($localobject->country_id, $newobject->country_id);
$this->assertEquals($localobject->status, $newobject->status);
$this->assertEquals($localobject->tel, $newobject->tel);
$this->assertEquals($localobject->fax, $newobject->fax);
$this->assertEquals($localobject->email, $newobject->email);
$this->assertEquals($localobject->url, $newobject->url);
$this->assertEquals($localobject->idprof1, $newobject->idprof1);
$this->assertEquals($localobject->idprof2, $newobject->idprof2);
$this->assertEquals($localobject->idprof3, $newobject->idprof3);
$this->assertEquals($localobject->idprof4, $newobject->idprof4);
return $localobject;
}
/**
* @depends testContactUpdate
* The depends says test is run only if previous is ok
*/
public function testContactOther($localobject)
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
/* $result=$localobject->set_as_client();
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0);
$result=$localobject->set_price_level(1,$user);
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0);
$result=$localobject->set_remise_client(10,'Gift',$user);
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0);
$result=$localobject->getNomUrl(1);
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertNotEquals($result, '');
$result=$localobject->getFullAddress();
print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertContains("New address\nNew zip New town", $result);
$result=$localobject->isInEEC();
print __METHOD__." id=".$localobject->id." country_code=".$this->country_code." result=".$result."\n";
$this->assertTrue(true, $result);
*/
$localobject->info($localobject->id);
print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
$this->assertNotEquals($localobject->date_creation, '');
return $localobject->id;
}
/**
* @depends testContactOther
* The depends says test is run only if previous is ok
*/
public function testContactDelete($id)
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new Contact($this->savdb);
$result=$localobject->fetch($id);
$result=$localobject->delete($id);
print __METHOD__." id=".$id." result=".$result."\n";
$this->assertLessThan($result, 0);
return $result;
}
/**
*/
public function testContactStatic()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$localobject=new Contact($db);
return;
}
}
?>

View File

@@ -172,6 +172,7 @@ class SocieteTest extends PHPUnit_Framework_TestCase
$localobject->address='New address';
$localobject->zip='New zip';
$localobject->town='New town';
$localobject->country_id=2;
$localobject->status=0;
$localobject->tel='New tel';
$localobject->fax='New fax';
@@ -202,6 +203,7 @@ class SocieteTest extends PHPUnit_Framework_TestCase
$this->assertEquals($localobject->address, $newobject->address);
$this->assertEquals($localobject->zip, $newobject->zip);
$this->assertEquals($localobject->town, $newobject->town);
$this->assertEquals($localobject->country_id, $newobject->country_id);
$this->assertEquals($localobject->status, $newobject->status);
$this->assertEquals($localobject->tel, $newobject->tel);
$this->assertEquals($localobject->fax, $newobject->fax);