test.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // Copyright (C) 2010-2014 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Core test page
  20. *
  21. * @copyright Copyright (C) 2010-2014 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. ?>
  25. <style>
  26. .vardump {
  27. font-size:8pt;
  28. line-height:100%;
  29. }
  30. </style>
  31. <?php
  32. ///////////////////////////////////////////////////////////////////////////////
  33. // Helpers
  34. ///////////////////////////////////////////////////////////////////////////////
  35. function ReadMandatoryParam($sName)
  36. {
  37. $value = utils::ReadParam($sName, null);
  38. if (is_null($value))
  39. {
  40. echo "<p>Missing mandatory argument <b>$sName</b></p>";
  41. exit;
  42. }
  43. return $value;
  44. }
  45. function IsAValidTestClass($sClassName)
  46. {
  47. // Must be a child of TestHandler
  48. //
  49. if (!is_subclass_of($sClassName, 'TestHandler')) return false;
  50. // Must not be abstract
  51. //
  52. $oReflectionClass = new ReflectionClass($sClassName);
  53. if (!$oReflectionClass->isInstantiable()) return false;
  54. return true;
  55. }
  56. function GetTestClassLine($sClassName)
  57. {
  58. $oReflectionClass = new ReflectionClass($sClassName);
  59. return $oReflectionClass->getStartLine();
  60. }
  61. function DisplayEvents($aEvents, $sTitle)
  62. {
  63. echo "<h4>$sTitle</h4>\n";
  64. if (count($aEvents) > 0)
  65. {
  66. echo "<ul>\n";
  67. foreach ($aEvents as $sEvent)
  68. {
  69. echo "<li>$sEvent</li>\n";
  70. }
  71. echo "</ul>\n";
  72. }
  73. else
  74. {
  75. echo "<p>none</p>\n";
  76. }
  77. }
  78. ///////////////////////////////////////////////////////////////////////////////
  79. // Main
  80. ///////////////////////////////////////////////////////////////////////////////
  81. require_once('../approot.inc.php');
  82. require_once(APPROOT.'/application/utils.inc.php');
  83. require_once('./test.class.inc.php');
  84. require_once('./testlist.inc.php');
  85. require_once(APPROOT.'/core/cmdbobject.class.inc.php');
  86. $sTodo = utils::ReadParam("todo", "");
  87. if ($sTodo == '')
  88. {
  89. // Show the list of tests
  90. //
  91. echo "<h3>Existing tests</h3>\n";
  92. echo "<ul>\n";
  93. foreach (get_declared_classes() as $sClassName)
  94. {
  95. if (!IsAValidTestClass($sClassName)) continue;
  96. $sName = call_user_func(array($sClassName, 'GetName'));
  97. $sDescription = call_user_func(array($sClassName, 'GetDescription'));
  98. echo "<li><a href=\"?todo=exec&testid=$sClassName\">$sName</a> ($sDescription)</li>\n";
  99. }
  100. echo "</ul>\n";
  101. }
  102. else if ($sTodo == 'exec')
  103. {
  104. // Execute a test
  105. //
  106. $sTestClass = ReadMandatoryParam("testid");
  107. if (!IsAValidTestClass($sTestClass))
  108. {
  109. echo "<p>Wrong value for testid, expecting a valid class name</p>\n";
  110. }
  111. else
  112. {
  113. $oTest = new $sTestClass();
  114. $iStartLine = GetTestClassLine($sTestClass);
  115. echo "<h3>Testing: ".$oTest->GetName()."</h3>\n";
  116. echo "<h6>testlist.inc.php: $iStartLine</h6>\n";
  117. $bRes = $oTest->Execute();
  118. }
  119. /*
  120. MyHelpers::var_dump_html($oTest->GetResults());
  121. MyHelpers::var_dump_html($oTest->GetWarnings());
  122. MyHelpers::var_dump_html($oTest->GetErrors());
  123. */
  124. if ($bRes)
  125. {
  126. echo "<p>Success :-)</p>\n";
  127. DisplayEvents($oTest->GetResults(), 'Results');
  128. }
  129. else
  130. {
  131. echo "<p>Failure :-(</p>\n";
  132. }
  133. DisplayEvents($oTest->GetErrors(), 'Errors');
  134. DisplayEvents($oTest->GetWarnings(), 'Warnings');
  135. // Render the output
  136. //
  137. echo "<h4>Actual output</h4>\n";
  138. echo "<div style=\"border: dashed; background-color:light-grey;\">\n";
  139. echo $oTest->GetOutput();
  140. echo "</div>\n";
  141. }
  142. else
  143. {
  144. }
  145. ?>