HtmlRunner.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. require_once 'Sweety/Runner/AbstractTestRunner.php';
  3. /**
  4. * Runs SimpleTest cases as a group through a JS enabled browser.
  5. * @package Sweety
  6. * @author Chris Corbyn
  7. */
  8. class Sweety_Runner_HtmlRunner extends Sweety_Runner_AbstractTestRunner
  9. {
  10. /**
  11. * The path to a valid template file.
  12. * @var string
  13. */
  14. private $_template;
  15. /**
  16. * The name of the test suite.
  17. * @var string
  18. */
  19. private $_name;
  20. /**
  21. * Creates a new HtmlRunner scanning the given directories.
  22. * @param string[] $dirs
  23. * @param string $template to load
  24. * @param string $name of the test suite
  25. */
  26. public function __construct(array $dirs, $template, $name)
  27. {
  28. $this->_dirs = $dirs;
  29. $this->_template = $template;
  30. $this->_name = $name;
  31. }
  32. /**
  33. * Runs all test cases found under the given directories.
  34. * @param string[] $directories to scan for test cases
  35. * @param string To be prepended to class names
  36. * @return int
  37. */
  38. public function runAllTests($dirs = array())
  39. {
  40. //We don't want test output to be cached
  41. header("Cache-Control: no-cache, must-revalidate");
  42. header("Pragma: no-cache");
  43. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  44. if (empty($dirs))
  45. {
  46. $dirs = $this->_dirs;
  47. }
  48. $testCases = $this->findTests($dirs);
  49. foreach ($testCases as $k => $testCase)
  50. {
  51. if (preg_match($this->getIgnoredClassRegex(), $testCase))
  52. {
  53. unset($testCases[$k]);
  54. }
  55. }
  56. usort($testCases, array($this, '_sort'));
  57. $vars = array(
  58. //String
  59. 'testCases' => $testCases,
  60. //String
  61. 'suiteName' => $this->_name,
  62. // testCase => pass | fail | running
  63. 'runTests' => array(),
  64. //Integer
  65. 'caseCount' => 0,
  66. //Integer
  67. 'runCount' => 0,
  68. //Integer
  69. 'passCount' => 0,
  70. //Integer
  71. 'failCount' => 0,
  72. //Integer
  73. 'exceptionCount' => 0,
  74. // type => pass | fail | exception | output | internal, path => testCase, text => ...
  75. 'messages' => array(),
  76. // pass | fail
  77. 'result' => 'idle'
  78. );
  79. if (isset($_REQUEST['runtests']))
  80. {
  81. $reporter = $this->getReporter();
  82. $reporter->setTemplateVars($vars);
  83. if (!$reporter->isStarted())
  84. {
  85. $reporter->start();
  86. }
  87. $this->_runTestList((array)$_REQUEST['runtests'], $reporter);
  88. $reporter->finish();
  89. }
  90. else
  91. {
  92. foreach ($testCases as $testCase)
  93. {
  94. $vars['runTests'][$testCase] = 'idle'; //Show all checked by default
  95. }
  96. }
  97. $this->_render($vars);
  98. }
  99. /**
  100. * Run tests in the given array using the REST API (kind of).
  101. * @param string[] $tests
  102. * @param Sweety_Reporter $reporter
  103. * @return int
  104. */
  105. protected function _runTestList(array $tests, Sweety_Reporter $reporter)
  106. {
  107. $protocol = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';
  108. //Most likely a HTTP/1.0 server not supporting HOST header
  109. $server = !empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '127.0.0.1';
  110. $path = '/';
  111. if (!empty($_SERVER['REQUEST_URI']))
  112. {
  113. $path = preg_replace('/\?.*$/sD', '', $_SERVER['REQUEST_URI']);
  114. }
  115. $baseUrl = $protocol . $server . $path;
  116. foreach ($tests as $testCase)
  117. {
  118. $url = $baseUrl . '?test=' . $testCase . '&format=xml';
  119. $xml = file_get_contents($url);
  120. $this->parseXml($xml, $testCase);
  121. }
  122. return 0;
  123. }
  124. /**
  125. * Renders the view for the suite.
  126. * @param string[] $templateVars
  127. */
  128. protected function _render($vars = array())
  129. {
  130. foreach ($vars as $k => $v)
  131. {
  132. $$k = $v;
  133. }
  134. require_once $this->_template;
  135. }
  136. }