Clean code

This commit is contained in:
Laurent Destailleur 2020-09-16 03:15:31 +02:00
parent adeed1464d
commit a3492dfb5b

View File

@ -17,12 +17,26 @@
*/ */
/** /**
* \file test/unit/MyObjectTest.php * \file test/phpunit/MyObjectTest.php
* \ingroup mymodule * \ingroup mymodule
* \brief PHPUnit test for MyObject class. * \brief PHPUnit test for MyObject class.
*/ */
namespace test\unit; 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/mymodule/class/myobject.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;
$langs->load("main");
/** /**
* Class MyObjectTest * Class MyObjectTest
@ -30,13 +44,43 @@ namespace test\unit;
*/ */
class MyObjectTest extends \PHPUnit_Framework_TestCase class MyObjectTest extends \PHPUnit_Framework_TestCase
{ {
protected $savconf;
protected $savuser;
protected $savlangs;
protected $savdb;
/**
* Constructor
* We save global variables into local variables
*
* @return BOMTest
*/
public function __construct()
{
parent::__construct();
//$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";
}
/** /**
* Global test setup * Global test setup
* @return void * @return void
*/ */
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
fwrite(STDOUT, __METHOD__."\n"); 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";
} }
/** /**
@ -45,36 +89,13 @@ class MyObjectTest extends \PHPUnit_Framework_TestCase
*/ */
protected function setUp() protected function setUp()
{ {
fwrite(STDOUT, __METHOD__."\n"); global $conf,$user,$langs,$db;
} $conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
/** print __METHOD__."\n";
* Verify pre conditions
* @return void
*/
protected function assertPreConditions()
{
fwrite(STDOUT, __METHOD__."\n");
}
/**
* A sample test
* @return bool
*/
public function testSomething()
{
fwrite(STDOUT, __METHOD__."\n");
// TODO: test something
$this->assertTrue(true);
}
/**
* Verify post conditions
* @return void
*/
protected function assertPostConditions()
{
fwrite(STDOUT, __METHOD__."\n");
} }
/** /**
@ -83,7 +104,7 @@ class MyObjectTest extends \PHPUnit_Framework_TestCase
*/ */
protected function tearDown() protected function tearDown()
{ {
fwrite(STDOUT, __METHOD__."\n"); print __METHOD__."\n";
} }
/** /**
@ -92,19 +113,30 @@ class MyObjectTest extends \PHPUnit_Framework_TestCase
*/ */
public static function tearDownAfterClass() public static function tearDownAfterClass()
{ {
fwrite(STDOUT, __METHOD__."\n"); global $conf,$user,$langs,$db;
$db->rollback();
print __METHOD__."\n";
} }
/** /**
* Unsuccessful test * A sample test
* * @return bool
* @param Exception $e Exception
* @return void
* @throws Exception
*/ */
protected function onNotSuccessfulTest(Exception $e) public function testSomething()
{ {
fwrite(STDOUT, __METHOD__."\n"); global $conf,$user,$langs,$db;
throw $e; $conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
$result = true;
print __METHOD__." result=".$result."\n";
$this->assertTrue($result);
return $result;
} }
} }