test.class.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <?php
  2. require_once('coreexception.class.inc.php');
  3. require_once('attributedef.class.inc.php');
  4. require_once('filterdef.class.inc.php');
  5. require_once('stimulus.class.inc.php');
  6. require_once('MyHelpers.class.inc.php');
  7. require_once('expression.class.inc.php');
  8. require_once('cmdbsource.class.inc.php');
  9. require_once('sqlquery.class.inc.php');
  10. require_once('dbobject.class.php');
  11. require_once('dbobjectsearch.class.php');
  12. require_once('dbobjectset.class.php');
  13. require_once('userrights.class.inc.php');
  14. require_once('../webservices/webservices.class.inc.php');
  15. // Just to differentiate programmatically triggered exceptions and other kind of errors (usefull?)
  16. class UnitTestException extends Exception
  17. {}
  18. /**
  19. * Improved display of the backtrace
  20. *
  21. * @package iTopORM
  22. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  23. * @author Denis Flaven <denisflave@free.fr>
  24. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  25. * @link www.itop.com
  26. * @since 1.0
  27. * @version 1.1.1.1 $
  28. */
  29. class ExceptionFromError extends Exception
  30. {
  31. public function getTraceAsHtml()
  32. {
  33. $aBackTrace = $this->getTrace();
  34. return MyHelpers::get_callstack_html(0, $this->getTrace());
  35. // return "<pre>\n".$this->getTraceAsString()."</pre>\n";
  36. }
  37. }
  38. /**
  39. * Test handler API and basic helpers
  40. *
  41. * @package iTopORM
  42. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  43. * @author Denis Flaven <denisflave@free.fr>
  44. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  45. * @link www.itop.com
  46. * @since 1.0
  47. * @version 1.1.1.1 $
  48. */
  49. abstract class TestHandler
  50. {
  51. protected $m_aSuccesses;
  52. protected $m_aWarnings;
  53. protected $m_aErrors;
  54. protected $m_sOutput;
  55. public function __construct()
  56. {
  57. $this->m_aSuccesses = array();
  58. $this->m_aWarnings = array();
  59. $this->m_aErrors = array();
  60. }
  61. abstract static public function GetName();
  62. abstract static public function GetDescription();
  63. protected function DoPrepare() {return true;}
  64. abstract protected function DoExecute();
  65. protected function DoCleanup() {return true;}
  66. protected function ReportSuccess($sMessage, $sSubtestId = '')
  67. {
  68. $this->m_aSuccesses[] = $sMessage;
  69. }
  70. protected function ReportWarning($sMessage, $sSubtestId = '')
  71. {
  72. $this->m_aWarnings[] = $sMessage;
  73. }
  74. protected function ReportError($sMessage, $sSubtestId = '')
  75. {
  76. $this->m_aErrors[] = $sMessage;
  77. }
  78. public function GetResults()
  79. {
  80. return $this->m_aSuccesses;
  81. }
  82. public function GetWarnings()
  83. {
  84. return $this->m_aWarnings;
  85. }
  86. public function GetErrors()
  87. {
  88. return $this->m_aErrors;
  89. }
  90. public function GetOutput()
  91. {
  92. return $this->m_sOutput;
  93. }
  94. public function error_handler($errno, $errstr, $errfile, $errline)
  95. {
  96. // Note: return false to call the default handler (stop the program if an error)
  97. switch ($errno)
  98. {
  99. case E_USER_ERROR:
  100. $this->ReportError($errstr);
  101. //throw new ExceptionFromError("Fatal error in line $errline of file $errfile: $errstr");
  102. break;
  103. case E_USER_WARNING:
  104. $this->ReportWarning($errstr);
  105. break;
  106. case E_USER_NOTICE:
  107. $this->ReportWarning($errstr);
  108. break;
  109. default:
  110. throw new ExceptionFromError("Fatal warning in line $errline of file $errfile: $errstr");
  111. $this->ReportWarning("Unknown error type: [$errno] $errstr");
  112. echo "Unknown error type: [$errno] $errstr<br />\n";
  113. break;
  114. }
  115. return true; // do not call the default handler
  116. }
  117. public function Execute()
  118. {
  119. ob_start();
  120. set_error_handler(array($this, 'error_handler'));
  121. try
  122. {
  123. $this->DoPrepare();
  124. $this->DoExecute();
  125. }
  126. catch (ExceptionFromError $e)
  127. {
  128. $this->ReportError($e->getMessage().' - '.$e->getTraceAsHtml());
  129. }
  130. catch (CoreException $e)
  131. {
  132. //$this->ReportError($e->getMessage());
  133. //$this->ReportError($e->__tostring());
  134. $this->ReportError($e->getMessage().' - '.$e->getTraceAsHtml());
  135. }
  136. catch (Exception $e)
  137. {
  138. //$this->ReportError($e->getMessage());
  139. //$this->ReportError($e->__tostring());
  140. $this->ReportError('class '.get_class($e).' --- '.$e->getMessage().' - '.$e->getTraceAsString());
  141. }
  142. restore_error_handler();
  143. $this->m_sOutput = ob_get_clean();
  144. return (count($this->GetErrors()) == 0);
  145. }
  146. }
  147. /**
  148. * Test to execute a piece of code (checks if an error occurs)
  149. *
  150. * @package iTopORM
  151. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  152. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  153. * @link www.itop.com
  154. * @since 1.0
  155. * @version $itopversion$
  156. */
  157. abstract class TestFunction extends TestHandler
  158. {
  159. // simply overload DoExecute (temporary)
  160. }
  161. /**
  162. * Test to execute a piece of code (checks if an error occurs)
  163. *
  164. * @package iTopORM
  165. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  166. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  167. * @link www.itop.com
  168. * @since 1.0
  169. * @version $itopversion$
  170. */
  171. abstract class TestWebServices extends TestHandler
  172. {
  173. // simply overload DoExecute (temporary)
  174. static protected function DoPostRequestAuth($sRelativeUrl, $aData, $sLogin = 'admin', $sPassword = 'admin', $sOptionnalHeaders = null)
  175. {
  176. $aDataAndAuth = $aData;
  177. $aDataAndAuth['operation'] = 'login';
  178. $aDataAndAuth['auth_user'] = $sLogin;
  179. $aDataAndAuth['auth_pwd'] = $sPassword;
  180. $sHost = $_SERVER['HTTP_HOST'];
  181. $sRawPath = $_SERVER['SCRIPT_NAME'];
  182. $sPath = dirname($sRawPath);
  183. $sUrl = "http://$sHost/$sPath/$sRelativeUrl";
  184. return self::DoPostRequest($sUrl, $aDataAndAuth, $sOptionnalHeaders);
  185. }
  186. // Source: http://netevil.org/blog/2006/nov/http-post-from-php-without-curl
  187. // originaly named after do_post_request
  188. // Partially adapted to our coding conventions
  189. static protected function DoPostRequest($sUrl, $aData, $sOptionnalHeaders = null)
  190. {
  191. // $sOptionnalHeaders is a string containing additional HTTP headers that you would like to send in your request.
  192. $sData = http_build_query($aData);
  193. $aParams = array('http' => array(
  194. 'method' => 'POST',
  195. 'content' => $sData,
  196. 'header'=> "Content-type: application/x-www-form-urlencoded\r\nContent-Length: ".strlen($sData)."\r\n",
  197. ));
  198. if ($sOptionnalHeaders !== null)
  199. {
  200. $aParams['http']['header'] .= $sOptionnalHeaders;
  201. }
  202. $ctx = stream_context_create($aParams);
  203. $fp = @fopen($sUrl, 'rb', false, $ctx);
  204. if (!$fp)
  205. {
  206. throw new Exception("Problem with $sUrl, $php_errormsg");
  207. }
  208. $response = @stream_get_contents($fp);
  209. if ($response === false)
  210. {
  211. throw new Exception("Problem reading data from $sUrl, $php_errormsg");
  212. }
  213. return $response;
  214. }
  215. }
  216. /**
  217. * Test to execute a piece of code (checks if an error occurs)
  218. *
  219. * @package iTopORM
  220. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  221. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  222. * @link www.itop.com
  223. * @since 1.0
  224. * @version $itopversion$
  225. */
  226. abstract class TestSoapWebService extends TestHandler
  227. {
  228. // simply overload DoExecute (temporary)
  229. function __construct()
  230. {
  231. parent::__construct();
  232. }
  233. }
  234. /**
  235. * Test to check that a function outputs some values depending on its input
  236. *
  237. * @package iTopORM
  238. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  239. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  240. * @link www.itop.com
  241. * @since 1.0
  242. * @version $itopversion$
  243. */
  244. abstract class TestFunctionInOut extends TestFunction
  245. {
  246. abstract static public function GetCallSpec(); // parameters to call_user_func
  247. abstract static public function GetInOut(); // array of input => output
  248. protected function DoExecute()
  249. {
  250. $aTests = $this->GetInOut();
  251. if (is_array($aTests))
  252. {
  253. foreach ($aTests as $iTestId => $aTest)
  254. {
  255. $ret = call_user_func_array($this->GetCallSpec(), $aTest['args']);
  256. if ($ret != $aTest['output'])
  257. {
  258. // Note: to be improved to cope with non string parameters
  259. $this->ReportError("Found '$ret' while expecting '".$aTest['output']."'", $iTestId);
  260. }
  261. else
  262. {
  263. $this->ReportSuccess("Found the expected output '$ret'", $iTestId);
  264. }
  265. }
  266. }
  267. else
  268. {
  269. $ret = call_user_func($this->GetCallSpec());
  270. $this->ReportSuccess('Finished successfully');
  271. }
  272. }
  273. }
  274. /**
  275. * Test to check an URL (Searches for Error/Warning/Etc keywords)
  276. *
  277. * @package iTopORM
  278. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  279. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  280. * @link www.itop.com
  281. * @since 1.0
  282. * @version $itopversion$
  283. */
  284. abstract class TestUrl extends TestHandler
  285. {
  286. abstract static public function GetUrl();
  287. abstract static public function GetErrorKeywords();
  288. abstract static public function GetWarningKeywords();
  289. protected function DoExecute()
  290. {
  291. return true;
  292. }
  293. }
  294. /**
  295. * Test to check a user management module
  296. *
  297. * @package iTopORM
  298. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  299. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  300. * @link www.itop.com
  301. * @since 1.0
  302. * @version $itopversion$
  303. */
  304. abstract class TestUserRights extends TestHandler
  305. {
  306. protected function DoExecute()
  307. {
  308. return true;
  309. }
  310. }
  311. /**
  312. * Test to execute a scenario on a given DB
  313. *
  314. * @package iTopORM
  315. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  316. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  317. * @link www.itop.com
  318. * @since 1.0
  319. * @version $itopversion$
  320. */
  321. abstract class TestScenarioOnDB extends TestHandler
  322. {
  323. abstract static public function GetDBHost();
  324. abstract static public function GetDBUser();
  325. abstract static public function GetDBPwd();
  326. abstract static public function GetDBName();
  327. protected function DoPrepare()
  328. {
  329. $sDBHost = $this->GetDBHost();
  330. $sDBUser = $this->GetDBUser();
  331. $sDBPwd = $this->GetDBPwd();
  332. $sDBName = $this->GetDBName();
  333. CMDBSource::Init($sDBHost, $sDBUser, $sDBPwd);
  334. if (CMDBSource::IsDB($sDBName))
  335. {
  336. CMDBSource::DropDB($sDBName);
  337. }
  338. CMDBSource::CreateDB($sDBName);
  339. }
  340. protected function DoCleanup()
  341. {
  342. // CMDBSource::DropDB($this->GetDBName());
  343. }
  344. }
  345. /**
  346. * Test to use a business model on a given DB
  347. *
  348. * @package iTopORM
  349. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  350. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  351. * @link www.itop.com
  352. * @since 1.0
  353. * @version $itopversion$
  354. */
  355. abstract class TestBizModel extends TestHandler
  356. {
  357. // abstract static public function GetDBSubName();
  358. // abstract static public function GetBusinessModelFile();
  359. abstract static public function GetConfigFile();
  360. protected function DoPrepare()
  361. {
  362. MetaModel::Startup($this->GetConfigFile(), true); // allow missing DB
  363. MetaModel::CheckDefinitions();
  364. // something here to create records... but that's another story
  365. }
  366. protected function ResetDB()
  367. {
  368. if (MetaModel::DBExists())
  369. {
  370. MetaModel::DBDrop();
  371. }
  372. MetaModel::DBCreate();
  373. }
  374. static protected function show_list($oObjectSet)
  375. {
  376. $oObjectSet->Rewind();
  377. $aData = array();
  378. while ($oItem = $oObjectSet->Fetch())
  379. {
  380. $aValues = array();
  381. foreach(MetaModel::GetAttributesList(get_class($oItem)) as $sAttCode)
  382. {
  383. $aValues[$sAttCode] = $oItem->GetAsHTML($sAttCode);
  384. }
  385. //echo $oItem->GetKey()." => ".implode(", ", $aValues)."</br>\n";
  386. $aData[] = $aValues;
  387. }
  388. echo MyHelpers::make_table_from_assoc_array($aData);
  389. }
  390. static protected function search_and_show_list(DBObjectSearch $oMyFilter)
  391. {
  392. $oObjSet = new CMDBObjectSet($oMyFilter);
  393. echo $oMyFilter->__DescribeHTML()."' - Found ".$oObjSet->Count()." items.</br>\n";
  394. self::show_list($oObjSet);
  395. }
  396. static protected function search_and_show_list_from_sibusql($sSibuSQL)
  397. {
  398. echo $sSibuSQL."...<br/>\n";
  399. $oNewFilter = DBObjectSearch::FromSibuSQL($sSibuSQL);
  400. self::search_and_show_list($oNewFilter);
  401. }
  402. }
  403. /**
  404. * Test to execute a scenario common to any business model (tries to build all the possible queries, etc.)
  405. *
  406. * @package iTopORM
  407. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  408. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  409. * @link www.itop.com
  410. * @since 1.0
  411. * @version $itopversion$
  412. */
  413. abstract class TestBizModelGeneric extends TestBizModel
  414. {
  415. static public function GetName()
  416. {
  417. return 'Full test on a given business model';
  418. }
  419. static public function GetDescription()
  420. {
  421. return 'Systematic tests: gets each and every existing class and tries every attribute, search filters, etc.';
  422. }
  423. protected function DoPrepare()
  424. {
  425. parent::DoPrepare();
  426. if (!MetaModel::DBExists())
  427. {
  428. MetaModel::DBCreate();
  429. }
  430. // something here to create records... but that's another story
  431. }
  432. protected function DoExecute()
  433. {
  434. foreach(MetaModel::GetClasses() as $sClassName)
  435. {
  436. if (MetaModel::IsAbstract($sClassName)) continue;
  437. $oNobody = MetaModel::GetObject($sClassName, 123);
  438. $oBaby = new $sClassName;
  439. $oFilter = new DBObjectSearch($sClassName);
  440. // Challenge reversibility of SibusQL / filter object
  441. //
  442. $sExpr1 = $oFilter->ToSibuSQL();
  443. $oNewFilter = DBObjectSearch::FromSibuSQL($sExpr1);
  444. $sExpr2 = $oNewFilter->ToSibuSQL();
  445. if ($sExpr1 != $sExpr2)
  446. {
  447. $this->ReportError("Found two different SibuSQL expression out of the (same?) filter: <em>$sExpr1</em> != <em>$sExpr2</em>");
  448. }
  449. // Use the filter (perform the query)
  450. //
  451. $oSet = new CMDBObjectSet($oFilter);
  452. $this->ReportSuccess('Found '.$oSet->Count()." objects of class $sClassName");
  453. }
  454. return true;
  455. }
  456. }
  457. ?>