test.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. <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 DisplayEvents($aEvents, $sTitle)
  57. {
  58. echo "<h4>$sTitle</h4>\n";
  59. if (count($aEvents) > 0)
  60. {
  61. echo "<ul>\n";
  62. foreach ($aEvents as $sEvent)
  63. {
  64. echo "<li>$sEvent</li>\n";
  65. }
  66. echo "</ul>\n";
  67. }
  68. else
  69. {
  70. echo "<p>none</p>\n";
  71. }
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////
  74. // Main
  75. ///////////////////////////////////////////////////////////////////////////////
  76. require_once('../approot.inc.php');
  77. require_once(APPROOT.'/application/utils.inc.php');
  78. require_once('./test.class.inc.php');
  79. require_once('./testlist.inc.php');
  80. require_once(APPROOT.'/core/cmdbobject.class.inc.php');
  81. $sTodo = utils::ReadParam("todo", "");
  82. if ($sTodo == '')
  83. {
  84. // Show the list of tests
  85. //
  86. echo "<h3>Existing tests</h3>\n";
  87. echo "<ul>\n";
  88. foreach (get_declared_classes() as $sClassName)
  89. {
  90. if (!IsAValidTestClass($sClassName)) continue;
  91. $sName = call_user_func(array($sClassName, 'GetName'));
  92. $sDescription = call_user_func(array($sClassName, 'GetDescription'));
  93. echo "<li><a href=\"?todo=exec&testid=$sClassName\">$sName</a> ($sDescription)</li>\n";
  94. }
  95. echo "</ul>\n";
  96. }
  97. else if ($sTodo == 'exec')
  98. {
  99. // Execute a test
  100. //
  101. $sTestClass = ReadMandatoryParam("testid");
  102. if (!IsAValidTestClass($sTestClass))
  103. {
  104. echo "<p>Wrong value for testid, expecting a valid class name</p>\n";
  105. }
  106. else
  107. {
  108. $oTest = new $sTestClass();
  109. echo "<h3>Testing: ".$oTest->GetName()."</h3>\n";
  110. $bRes = $oTest->Execute();
  111. }
  112. /*
  113. MyHelpers::var_dump_html($oTest->GetResults());
  114. MyHelpers::var_dump_html($oTest->GetWarnings());
  115. MyHelpers::var_dump_html($oTest->GetErrors());
  116. */
  117. if ($bRes)
  118. {
  119. echo "<p>Success :-)</p>\n";
  120. DisplayEvents($oTest->GetResults(), 'Results');
  121. }
  122. else
  123. {
  124. echo "<p>Failure :-(</p>\n";
  125. }
  126. DisplayEvents($oTest->GetErrors(), 'Errors');
  127. DisplayEvents($oTest->GetWarnings(), 'Warnings');
  128. // Render the output
  129. //
  130. echo "<h4>Actual output</h4>\n";
  131. echo "<div style=\"border: dashed; background-color:light-grey;\">\n";
  132. echo $oTest->GetOutput();
  133. echo "</div>\n";
  134. }
  135. else
  136. {
  137. }
  138. ?>