test.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////////
  3. // Helpers
  4. ///////////////////////////////////////////////////////////////////////////////
  5. function ReadMandatoryParam($sName)
  6. {
  7. $value = utils::ReadParam($sName, null);
  8. if (is_null($value))
  9. {
  10. echo "<p>Missing mandatory argument <b>$sName</b></p>";
  11. exit;
  12. }
  13. return $value;
  14. }
  15. function IsAValidTestClass($sClassName)
  16. {
  17. // Must be a child of TestHandler
  18. //
  19. if (!is_subclass_of($sClassName, 'TestHandler')) return false;
  20. // Must not be abstract
  21. //
  22. $oReflectionClass = new ReflectionClass($sClassName);
  23. if (!$oReflectionClass->isInstantiable()) return false;
  24. return true;
  25. }
  26. function DisplayEvents($aEvents, $sTitle)
  27. {
  28. echo "<h4>$sTitle</h4>\n";
  29. if (count($aEvents) > 0)
  30. {
  31. echo "<ul>\n";
  32. foreach ($aEvents as $sEvent)
  33. {
  34. echo "<li>$sEvent</li>\n";
  35. }
  36. echo "</ul>\n";
  37. }
  38. else
  39. {
  40. echo "<p>none</p>\n";
  41. }
  42. }
  43. ///////////////////////////////////////////////////////////////////////////////
  44. // Main
  45. ///////////////////////////////////////////////////////////////////////////////
  46. require_once('../application/utils.inc.php');
  47. require_once('../core/test.class.inc.php');
  48. require_once('testlist.inc.php');
  49. require_once('../core/cmdbobject.class.inc.php');
  50. $sTodo = utils::ReadParam("todo", "");
  51. if ($sTodo == '')
  52. {
  53. // Show the list of tests
  54. //
  55. echo "<h3>Existing tests</h3>\n";
  56. echo "<ul>\n";
  57. foreach (get_declared_classes() as $sClassName)
  58. {
  59. if (!IsAValidTestClass($sClassName)) continue;
  60. $sName = call_user_func(array($sClassName, 'GetName'));
  61. $sDescription = call_user_func(array($sClassName, 'GetDescription'));
  62. echo "<li><a href=\"?todo=exec&testid=$sClassName\">$sName</a> ($sDescription)</li\n";
  63. }
  64. echo "</ul>\n";
  65. }
  66. else if ($sTodo == 'exec')
  67. {
  68. // Execute a test
  69. //
  70. $sTestClass = ReadMandatoryParam("testid");
  71. if (!IsAValidTestClass($sTestClass))
  72. {
  73. echo "<p>Wrong value for testid, expecting a valid class name</p>\n";
  74. }
  75. else
  76. {
  77. $oTest = new $sTestClass();
  78. echo "<h3>Testing: ".$oTest->GetName()."</h3>\n";
  79. $bRes = $oTest->Execute();
  80. }
  81. /*
  82. MyHelpers::var_dump_html($oTest->GetResults());
  83. MyHelpers::var_dump_html($oTest->GetWarnings());
  84. MyHelpers::var_dump_html($oTest->GetErrors());
  85. */
  86. if ($bRes)
  87. {
  88. echo "<p>Success :-)</p>\n";
  89. DisplayEvents($oTest->GetResults(), 'Results');
  90. }
  91. else
  92. {
  93. echo "<p>Failure :-(</p>\n";
  94. }
  95. DisplayEvents($oTest->GetErrors(), 'Errors');
  96. DisplayEvents($oTest->GetWarnings(), 'Warnings');
  97. // Render the output
  98. //
  99. echo "<h4>Actual output</h4>\n";
  100. echo "<div style=\"border: dashed; background-color:light-grey;\">\n";
  101. echo $oTest->GetOutput();
  102. echo "</div>\n";
  103. }
  104. else
  105. {
  106. }
  107. ?>