New: Add phpunit tests

This commit is contained in:
Laurent Destailleur 2010-04-26 23:52:51 +00:00
parent 584a4919ea
commit 0e5b5b9946
5 changed files with 109 additions and 68 deletions

View File

@ -29,14 +29,18 @@ require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__).'/../htdocs/master.inc.php'; require_once dirname(__FILE__).'/../htdocs/master.inc.php';
require_once dirname(__FILE__).'/../htdocs/commande/commande.class.php'; require_once dirname(__FILE__).'/../htdocs/commande/commande.class.php';
print "Load permissions for admin user with login 'admin'\n"; if (empty($user->id))
$user->fetch('admin'); {
$user->getrights(); print "Load permissions for admin user with login 'admin'\n";
$user->fetch('admin');
$user->getrights();
}
/** /**
* @backupGlobals enabled * @backupGlobals disabled
* @backupStaticAttributes enabled * @backupStaticAttributes enabled
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
*/ */
class CommandeTest extends PHPUnit_Framework_TestCase class CommandeTest extends PHPUnit_Framework_TestCase
{ {
@ -69,20 +73,19 @@ class CommandeTest extends PHPUnit_Framework_TestCase
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
global $conf,$user,$langs,$db; 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"; print __METHOD__."\n";
if (! $db->transaction_opened) $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
} }
public static function tearDownAfterClass() public static function tearDownAfterClass()
{ {
global $conf,$user,$langs,$db; global $conf,$user,$langs,$db;
$db->rollback();
print __METHOD__."\n"; print __METHOD__."\n";
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
*/ */
protected function setUp() protected function setUp()
{ {
@ -96,8 +99,6 @@ class CommandeTest extends PHPUnit_Framework_TestCase
//print $db->getVersion()."\n"; //print $db->getVersion()."\n";
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
*/ */
protected function tearDown() protected function tearDown()
{ {
@ -105,8 +106,6 @@ class CommandeTest extends PHPUnit_Framework_TestCase
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @covers Commande::create * @covers Commande::create
*/ */
public function testCommandeCreate() public function testCommandeCreate()
@ -127,8 +126,6 @@ class CommandeTest extends PHPUnit_Framework_TestCase
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @depends testCommandeCreate * @depends testCommandeCreate
* @covers Commande::fetch * @covers Commande::fetch
* The depends says test is run only if previous is ok * The depends says test is run only if previous is ok
@ -143,14 +140,13 @@ class CommandeTest extends PHPUnit_Framework_TestCase
$localobject=new Commande($this->savdb); $localobject=new Commande($this->savdb);
$result=$localobject->fetch($id); $result=$localobject->fetch($id);
$this->assertLessThan($result, 0); $this->assertLessThan($result, 0);
print __METHOD__." id=".$id." result=".$result."\n"; print __METHOD__." id=".$id." result=".$result."\n";
return $localobject; return $localobject;
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @depends testCommandeFetch * @depends testCommandeFetch
* @covers Commande::update * @covers Commande::update
* The depends says test is run only if previous is ok * The depends says test is run only if previous is ok
@ -165,14 +161,13 @@ class CommandeTest extends PHPUnit_Framework_TestCase
$localobject->note='New note after update'; $localobject->note='New note after update';
$result=$localobject->update($user); $result=$localobject->update($user);
print __METHOD__." id=".$localobject->id." result=".$result."\n"; print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0); $this->assertLessThan($result, 0);
return $localobject->id; return $localobject->id;
} }
*/ */
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @depends testCommandeFetch * @depends testCommandeFetch
* @covers Commande::valid * @covers Commande::valid
* The depends says test is run only if previous is ok * The depends says test is run only if previous is ok
@ -186,14 +181,13 @@ class CommandeTest extends PHPUnit_Framework_TestCase
$db=$this->savdb; $db=$this->savdb;
$result=$localobject->valid($user); $result=$localobject->valid($user);
print __METHOD__." id=".$localobject->id." result=".$result."\n"; print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0); $this->assertLessThan($result, 0);
return $localobject->id; return $localobject->id;
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @depends testCommandeValid * @depends testCommandeValid
* @covers Commande::delete * @covers Commande::delete
* The depends says test is run only if previous is ok * The depends says test is run only if previous is ok
@ -209,6 +203,7 @@ class CommandeTest extends PHPUnit_Framework_TestCase
$localobject=new Commande($this->savdb); $localobject=new Commande($this->savdb);
$result=$localobject->fetch($id); $result=$localobject->fetch($id);
$result=$localobject->delete($user); $result=$localobject->delete($user);
print __METHOD__." id=".$id." result=".$result."\n"; print __METHOD__." id=".$id." result=".$result."\n";
$this->assertLessThan($result, 0); $this->assertLessThan($result, 0);
return $result; return $result;

View File

@ -29,17 +29,21 @@ require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__).'/../htdocs/master.inc.php'; require_once dirname(__FILE__).'/../htdocs/master.inc.php';
require_once dirname(__FILE__).'/../htdocs/compta/facture/class/facture.class.php'; require_once dirname(__FILE__).'/../htdocs/compta/facture/class/facture.class.php';
print "Load permissions for admin user with login 'admin'\n"; if (empty($user->id))
$user->fetch('admin'); {
$user->getrights(); print "Load permissions for admin user with login 'admin'\n";
$user->fetch('admin');
$user->getrights();
}
/** /**
* @backupGlobals enabled * @backupGlobals disabled
* @backupStaticAttributes enabled * @backupStaticAttributes enabled
* @covers DoliDb * @covers DoliDb
* @covers User * @covers User
* @covers Translate * @covers Translate
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
*/ */
class FactureTest extends PHPUnit_Framework_TestCase class FactureTest extends PHPUnit_Framework_TestCase
{ {
@ -72,20 +76,19 @@ class FactureTest extends PHPUnit_Framework_TestCase
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
global $conf,$user,$langs,$db; 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"; print __METHOD__."\n";
if (! $db->transaction_opened) $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
} }
public static function tearDownAfterClass() public static function tearDownAfterClass()
{ {
global $conf,$user,$langs,$db; global $conf,$user,$langs,$db;
$db->rollback();
print __METHOD__."\n"; print __METHOD__."\n";
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
*/ */
protected function setUp() protected function setUp()
{ {
@ -98,8 +101,6 @@ class FactureTest extends PHPUnit_Framework_TestCase
print __METHOD__."\n"; print __METHOD__."\n";
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
*/ */
protected function tearDown() protected function tearDown()
{ {
@ -107,8 +108,6 @@ class FactureTest extends PHPUnit_Framework_TestCase
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @covers Facture::create * @covers Facture::create
*/ */
public function testFactureCreate() public function testFactureCreate()
@ -129,8 +128,6 @@ class FactureTest extends PHPUnit_Framework_TestCase
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @depends testFactureCreate * @depends testFactureCreate
* @covers Facture::fetch * @covers Facture::fetch
* The depends says test is run only if previous is ok * The depends says test is run only if previous is ok
@ -145,14 +142,13 @@ class FactureTest extends PHPUnit_Framework_TestCase
$localobject=new Facture($this->savdb); $localobject=new Facture($this->savdb);
$result=$localobject->fetch($id); $result=$localobject->fetch($id);
$this->assertLessThan($result, 0); $this->assertLessThan($result, 0);
print __METHOD__." id=".$id." result=".$result."\n"; print __METHOD__." id=".$id." result=".$result."\n";
return $localobject; return $localobject;
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @depends testFactureFetch * @depends testFactureFetch
* @covers Facture::update * @covers Facture::update
* The depends says test is run only if previous is ok * The depends says test is run only if previous is ok
@ -167,14 +163,13 @@ class FactureTest extends PHPUnit_Framework_TestCase
$localobject->note='New note after update'; $localobject->note='New note after update';
$result=$localobject->update($user); $result=$localobject->update($user);
print __METHOD__." id=".$localobject->id." result=".$result."\n"; print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0); $this->assertLessThan($result, 0);
return $localobject; return $localobject;
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @depends testFactureUpdate * @depends testFactureUpdate
* @covers Facture::set_valid * @covers Facture::set_valid
* The depends says test is run only if previous is ok * The depends says test is run only if previous is ok
@ -189,13 +184,12 @@ class FactureTest extends PHPUnit_Framework_TestCase
$result=$localobject->set_valid($user); $result=$localobject->set_valid($user);
print __METHOD__." id=".$localobject->id." result=".$result."\n"; print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0); $this->assertLessThan($result, 0);
return $localobject->id; return $localobject->id;
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @depends testFactureValid * @depends testFactureValid
* @covers Facture::delete * @covers Facture::delete
* The depends says test is run only if previous is ok * The depends says test is run only if previous is ok
@ -211,6 +205,7 @@ class FactureTest extends PHPUnit_Framework_TestCase
$localobject=new Facture($this->savdb); $localobject=new Facture($this->savdb);
$result=$localobject->fetch($id); $result=$localobject->fetch($id);
$result=$localobject->delete($id); $result=$localobject->delete($id);
print __METHOD__." id=".$id." result=".$result."\n"; print __METHOD__." id=".$id." result=".$result."\n";
$this->assertLessThan($result, 0); $this->assertLessThan($result, 0);
return $result; return $result;

View File

@ -1,15 +1,42 @@
<?php <?php
global $conf,$user,$langs,$db; /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
require_once 'PHPUnit/Framework.php'; *
* 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.
*/
/**
* \file test/MyTestSuite.php
* \ingroup test
* \brief This file is a test suite to run all unit tests
* \version $Id$
* \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/Framework.php';
require_once dirname(__FILE__).'/../htdocs/master.inc.php'; require_once dirname(__FILE__).'/../htdocs/master.inc.php';
require_once dirname(__FILE__).'/FactureTest.php'; require_once dirname(__FILE__).'/FactureTest.php';
require_once dirname(__FILE__).'/PropalTest.php'; require_once dirname(__FILE__).'/PropalTest.php';
require_once dirname(__FILE__).'/CommandeTest.php'; require_once dirname(__FILE__).'/CommandeTest.php';
print "Load permissions for admin user with login 'admin'\n"; if (empty($user->id))
$user->fetch('admin'); {
$user->getrights(); print "Load permissions for admin user with login 'admin'\n";
$user->fetch('admin');
$user->getrights();
}
/** /**

View File

@ -29,14 +29,18 @@ require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__).'/../htdocs/master.inc.php'; require_once dirname(__FILE__).'/../htdocs/master.inc.php';
require_once dirname(__FILE__).'/../htdocs/comm/propal/propal.class.php'; require_once dirname(__FILE__).'/../htdocs/comm/propal/propal.class.php';
print "Load permissions for admin user with login 'admin'\n"; if (empty($user->id))
$user->fetch('admin'); {
$user->getrights(); print "Load permissions for admin user with login 'admin'\n";
$user->fetch('admin');
$user->getrights();
}
/** /**
* @backupGlobals enabled * @backupGlobals disabled
* @backupStaticAttributes enabled * @backupStaticAttributes enabled
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
*/ */
class PropalTest extends PHPUnit_Framework_TestCase class PropalTest extends PHPUnit_Framework_TestCase
{ {
@ -69,20 +73,19 @@ class PropalTest extends PHPUnit_Framework_TestCase
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
global $conf,$user,$langs,$db; 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"; print __METHOD__."\n";
if (! $db->transaction_opened) $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
} }
public static function tearDownAfterClass() public static function tearDownAfterClass()
{ {
global $conf,$user,$langs,$db; global $conf,$user,$langs,$db;
$db->rollback();
print __METHOD__."\n"; print __METHOD__."\n";
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
*/ */
protected function setUp() protected function setUp()
{ {
@ -96,8 +99,6 @@ class PropalTest extends PHPUnit_Framework_TestCase
//print $db->getVersion()."\n"; //print $db->getVersion()."\n";
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
*/ */
protected function tearDown() protected function tearDown()
{ {
@ -105,8 +106,6 @@ class PropalTest extends PHPUnit_Framework_TestCase
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @covers Propal::create * @covers Propal::create
*/ */
public function testPropalCreate() public function testPropalCreate()
@ -127,8 +126,6 @@ class PropalTest extends PHPUnit_Framework_TestCase
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @depends testPropalCreate * @depends testPropalCreate
* @covers Propal::fetch * @covers Propal::fetch
* The depends says test is run only if previous is ok * The depends says test is run only if previous is ok
@ -143,14 +140,13 @@ class PropalTest extends PHPUnit_Framework_TestCase
$localobject=new Propal($this->savdb); $localobject=new Propal($this->savdb);
$result=$localobject->fetch($id); $result=$localobject->fetch($id);
$this->assertLessThan($result, 0); $this->assertLessThan($result, 0);
print __METHOD__." id=".$id." result=".$result."\n"; print __METHOD__." id=".$id." result=".$result."\n";
return $localobject; return $localobject;
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @depends testPropalFetch * @depends testPropalFetch
* @covers Propal::update * @covers Propal::update
* The depends says test is run only if previous is ok * The depends says test is run only if previous is ok
@ -165,14 +161,13 @@ class PropalTest extends PHPUnit_Framework_TestCase
$localobject->note='New note after update'; $localobject->note='New note after update';
$result=$localobject->update($user); $result=$localobject->update($user);
print __METHOD__." id=".$localobject->id." result=".$result."\n"; print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0); $this->assertLessThan($result, 0);
return $localobject->id; return $localobject->id;
} }
*/ */
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @depends testPropalFetch * @depends testPropalFetch
* @covers Propal::valid * @covers Propal::valid
* The depends says test is run only if previous is ok * The depends says test is run only if previous is ok
@ -186,14 +181,13 @@ class PropalTest extends PHPUnit_Framework_TestCase
$db=$this->savdb; $db=$this->savdb;
$result=$localobject->valid($user); $result=$localobject->valid($user);
print __METHOD__." id=".$localobject->id." result=".$result."\n"; print __METHOD__." id=".$localobject->id." result=".$result."\n";
$this->assertLessThan($result, 0); $this->assertLessThan($result, 0);
return $localobject->id; return $localobject->id;
} }
/** /**
* @backupGlobals enabled
* @backupStaticAttributes enabled
* @depends testPropalValid * @depends testPropalValid
* @covers Propal::delete * @covers Propal::delete
* The depends says test is run only if previous is ok * The depends says test is run only if previous is ok
@ -209,11 +203,11 @@ class PropalTest extends PHPUnit_Framework_TestCase
$localobject=new Propal($this->savdb); $localobject=new Propal($this->savdb);
$result=$localobject->fetch($id); $result=$localobject->fetch($id);
$result=$localobject->delete($user); $result=$localobject->delete($user);
print __METHOD__." id=".$id." result=".$result."\n"; print __METHOD__." id=".$id." result=".$result."\n";
$this->assertLessThan($result, 0); $this->assertLessThan($result, 0);
return $result; return $result;
} }
} }
?> ?>

30
test/phpunit.xml Normal file
View File

@ -0,0 +1,30 @@
<phpunit backupGlobals="false"
backupStaticAttributes="true"
bootstrap="/path/to/bootstrap.php"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="true"
stopOnFailure="true"
syntaxCheck="false"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader">
<filter>
<blacklist>
<directory suffix=".php">/path/to/files</directory>
<file>/path/to/file</file>
<exclude>
<directory suffix=".php">/path/to/files</directory>
<file>/path/to/file</file>
</exclude>
</blacklist>
<whitelist>
<directory suffix=".php">/path/to/files</directory>
<file>/path/to/file</file>
<exclude>
<directory suffix=".php">/path/to/files</directory>
<file>/path/to/file</file>
</exclude>
</whitelist>
</filter>
</phpunit>