PageRenderTime 34ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/test/phpunit/ContratTest.php

https://github.com/asterix14/dolibarr
PHP | 256 lines | 134 code | 34 blank | 88 comment | 2 complexity | 3b07e6609af942fc11cec61b1355eacb MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. /**
  19. * \file test/phpunit/ContratTest.php
  20. * \ingroup test
  21. * \brief PHPUnit test
  22. * \remarks To run this script as CLI: phpunit filename.php
  23. */
  24. global $conf,$user,$langs,$db;
  25. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  26. require_once 'PHPUnit/Autoload.php';
  27. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  28. require_once dirname(__FILE__).'/../../htdocs/contrat/class/contrat.class.php';
  29. if (empty($user->id))
  30. {
  31. print "Load permissions for admin user nb 1\n";
  32. $user->fetch(1);
  33. $user->getrights();
  34. }
  35. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  36. /**
  37. * Class for PHPUnit tests
  38. *
  39. * @backupGlobals disabled
  40. * @backupStaticAttributes enabled
  41. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  42. */
  43. class ContratTest extends PHPUnit_Framework_TestCase
  44. {
  45. protected $savconf;
  46. protected $savuser;
  47. protected $savlangs;
  48. protected $savdb;
  49. /**
  50. * Constructor
  51. * We save global variables into local variables
  52. *
  53. * @return ContratTest
  54. */
  55. function ContratTest()
  56. {
  57. //$this->sharedFixture
  58. global $conf,$user,$langs,$db;
  59. $this->savconf=$conf;
  60. $this->savuser=$user;
  61. $this->savlangs=$langs;
  62. $this->savdb=$db;
  63. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  64. //print " - db ".$db->db;
  65. print "\n";
  66. }
  67. // Static methods
  68. public static function setUpBeforeClass()
  69. {
  70. global $conf,$user,$langs,$db;
  71. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  72. print __METHOD__."\n";
  73. }
  74. public static function tearDownAfterClass()
  75. {
  76. global $conf,$user,$langs,$db;
  77. $db->rollback();
  78. print __METHOD__."\n";
  79. }
  80. /**
  81. */
  82. protected function setUp()
  83. {
  84. global $conf,$user,$langs,$db;
  85. $conf=$this->savconf;
  86. $user=$this->savuser;
  87. $langs=$this->savlangs;
  88. $db=$this->savdb;
  89. print __METHOD__."\n";
  90. }
  91. /**
  92. */
  93. protected function tearDown()
  94. {
  95. print __METHOD__."\n";
  96. }
  97. /**
  98. */
  99. public function testContratCreate()
  100. {
  101. global $conf,$user,$langs,$db;
  102. $conf=$this->savconf;
  103. $user=$this->savuser;
  104. $langs=$this->savlangs;
  105. $db=$this->savdb;
  106. $localobject=new Contrat($this->savdb);
  107. $localobject->initAsSpecimen();
  108. $result=$localobject->create($user,$langs,$conf);
  109. $this->assertLessThan($result, 0);
  110. print __METHOD__." result=".$result."\n";
  111. return $result;
  112. }
  113. /**
  114. * @depends testContratCreate
  115. * The depends says test is run only if previous is ok
  116. */
  117. public function testContratFetch($id)
  118. {
  119. global $conf,$user,$langs,$db;
  120. $conf=$this->savconf;
  121. $user=$this->savuser;
  122. $langs=$this->savlangs;
  123. $db=$this->savdb;
  124. $localobject=new Contrat($this->savdb);
  125. $result=$localobject->fetch($id);
  126. $this->assertLessThan($result, 0);
  127. print __METHOD__." id=".$id." result=".$result."\n";
  128. return $localobject;
  129. }
  130. /**
  131. * @depends testContratFetch
  132. * The depends says test is run only if previous is ok
  133. */
  134. /* public function testContratUpdate($localobject)
  135. {
  136. global $conf,$user,$langs,$db;
  137. $conf=$this->savconf;
  138. $user=$this->savuser;
  139. $langs=$this->savlangs;
  140. $db=$this->savdb;
  141. $localobject->note='New note after update';
  142. $result=$localobject->update($user);
  143. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  144. $this->assertLessThan($result, 0);
  145. return $localobject;
  146. }
  147. */
  148. /**
  149. * @depends testContratFetch
  150. * The depends says test is run only if previous is ok
  151. */
  152. public function testContratValid($localobject)
  153. {
  154. global $conf,$user,$langs,$db;
  155. $conf=$this->savconf;
  156. $user=$this->savuser;
  157. $langs=$this->savlangs;
  158. $db=$this->savdb;
  159. $result=$localobject->update_statut($user);
  160. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  161. $this->assertLessThan($result, 0);
  162. return $localobject;
  163. }
  164. /**
  165. * @depends testContratValid
  166. * The depends says test is run only if previous is ok
  167. */
  168. public function testContratOther($localobject)
  169. {
  170. global $conf,$user,$langs,$db;
  171. $conf=$this->savconf;
  172. $user=$this->savuser;
  173. $langs=$this->savlangs;
  174. $db=$this->savdb;
  175. /*$result=$localobject->setstatus(0);
  176. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  177. $this->assertLessThan($result, 0);
  178. */
  179. $localobject->info($localobject->id);
  180. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  181. $this->assertNotEquals($localobject->date_creation, '');
  182. return $localobject->id;
  183. }
  184. /**
  185. * @depends testContratOther
  186. * The depends says test is run only if previous is ok
  187. */
  188. public function testContratDelete($id)
  189. {
  190. global $conf,$user,$langs,$db;
  191. $conf=$this->savconf;
  192. $user=$this->savuser;
  193. $langs=$this->savlangs;
  194. $db=$this->savdb;
  195. $localobject=new Contrat($this->savdb);
  196. $result=$localobject->fetch($id);
  197. $result=$localobject->delete($id);
  198. print __METHOD__." id=".$id." result=".$result."\n";
  199. $this->assertLessThan($result, 0);
  200. return $result;
  201. }
  202. /**
  203. *
  204. */
  205. public function testVerifyNumRef()
  206. {
  207. global $conf,$user,$langs,$db;
  208. $conf=$this->savconf;
  209. $user=$this->savuser;
  210. $langs=$this->savlangs;
  211. $db=$this->savdb;
  212. $localobject=new Contrat($this->savdb);
  213. $result=$localobject->ref='refthatdoesnotexists';
  214. $result=$localobject->VerifyNumRef();
  215. print __METHOD__." result=".$result."\n";
  216. $this->assertEquals($result, 0);
  217. return $result;
  218. }
  219. }
  220. ?>