test.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  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; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Core test page
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // Helpers
  26. ///////////////////////////////////////////////////////////////////////////////
  27. function ReadMandatoryParam($sName)
  28. {
  29. $value = utils::ReadParam($sName, null);
  30. if (is_null($value))
  31. {
  32. echo "<p>Missing mandatory argument <b>$sName</b></p>";
  33. exit;
  34. }
  35. return $value;
  36. }
  37. function IsAValidTestClass($sClassName)
  38. {
  39. // Must be a child of TestHandler
  40. //
  41. if (!is_subclass_of($sClassName, 'TestHandler')) return false;
  42. // Must not be abstract
  43. //
  44. $oReflectionClass = new ReflectionClass($sClassName);
  45. if (!$oReflectionClass->isInstantiable()) return false;
  46. return true;
  47. }
  48. function DisplayEvents($aEvents, $sTitle)
  49. {
  50. echo "<h4>$sTitle</h4>\n";
  51. if (count($aEvents) > 0)
  52. {
  53. echo "<ul>\n";
  54. foreach ($aEvents as $sEvent)
  55. {
  56. echo "<li>$sEvent</li>\n";
  57. }
  58. echo "</ul>\n";
  59. }
  60. else
  61. {
  62. echo "<p>none</p>\n";
  63. }
  64. }
  65. ///////////////////////////////////////////////////////////////////////////////
  66. // Main
  67. ///////////////////////////////////////////////////////////////////////////////
  68. require_once('../application/utils.inc.php');
  69. require_once('../core/test.class.inc.php');
  70. require_once('testlist.inc.php');
  71. require_once('../core/cmdbobject.class.inc.php');
  72. $sTodo = utils::ReadParam("todo", "");
  73. if ($sTodo == '')
  74. {
  75. // Show the list of tests
  76. //
  77. echo "<h3>Existing tests</h3>\n";
  78. echo "<ul>\n";
  79. foreach (get_declared_classes() as $sClassName)
  80. {
  81. if (!IsAValidTestClass($sClassName)) continue;
  82. $sName = call_user_func(array($sClassName, 'GetName'));
  83. $sDescription = call_user_func(array($sClassName, 'GetDescription'));
  84. echo "<li><a href=\"?todo=exec&testid=$sClassName\">$sName</a> ($sDescription)</li\n";
  85. }
  86. echo "</ul>\n";
  87. }
  88. else if ($sTodo == 'exec')
  89. {
  90. // Execute a test
  91. //
  92. $sTestClass = ReadMandatoryParam("testid");
  93. if (!IsAValidTestClass($sTestClass))
  94. {
  95. echo "<p>Wrong value for testid, expecting a valid class name</p>\n";
  96. }
  97. else
  98. {
  99. $oTest = new $sTestClass();
  100. echo "<h3>Testing: ".$oTest->GetName()."</h3>\n";
  101. $bRes = $oTest->Execute();
  102. }
  103. /*
  104. MyHelpers::var_dump_html($oTest->GetResults());
  105. MyHelpers::var_dump_html($oTest->GetWarnings());
  106. MyHelpers::var_dump_html($oTest->GetErrors());
  107. */
  108. if ($bRes)
  109. {
  110. echo "<p>Success :-)</p>\n";
  111. DisplayEvents($oTest->GetResults(), 'Results');
  112. }
  113. else
  114. {
  115. echo "<p>Failure :-(</p>\n";
  116. }
  117. DisplayEvents($oTest->GetErrors(), 'Errors');
  118. DisplayEvents($oTest->GetWarnings(), 'Warnings');
  119. // Render the output
  120. //
  121. echo "<h4>Actual output</h4>\n";
  122. echo "<div style=\"border: dashed; background-color:light-grey;\">\n";
  123. echo $oTest->GetOutput();
  124. echo "</div>\n";
  125. }
  126. else
  127. {
  128. }
  129. ?>