WebResultPrinter.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace mindplay\test\lib\ResultPrinter;
  3. use mindplay\test\lib\xTest;
  4. use mindplay\test\lib\xTestRunner;
  5. class WebResultPrinter extends ResultPrinter
  6. {
  7. /**
  8. * Prints the header before the test output.
  9. *
  10. * @param xTestRunner $testRunner Test runner.
  11. * @param string $pattern Test filename pattern.
  12. * @return void
  13. */
  14. public function suiteHeader(xTestRunner $testRunner, $pattern)
  15. {
  16. echo '<html>
  17. <head>
  18. <title>Unit Tests</title>
  19. <style type="text/css">
  20. table { border-collapse:collapse; }
  21. td, th { text-align:left; padding:2px 6px; border:solid 1px #aaa; }
  22. </style>
  23. </head>
  24. <body>
  25. <h2>Unit Tests</h2>';
  26. echo '<h4>Codebase: ' . $testRunner->getRootPath() . '</h4>';
  27. echo '<h4>Test Suite: ' . $pattern . '</h4>';
  28. }
  29. /**
  30. * Prints the footer after the test output.
  31. *
  32. * @param xTestRunner $testRunner Test runner.
  33. * @return void
  34. */
  35. public function suiteFooter(xTestRunner $testRunner)
  36. {
  37. echo '</body></html>';
  38. }
  39. /**
  40. * Creates code coverage report.
  41. *
  42. * @param \PHP_CodeCoverage $coverage Code coverage collector.
  43. * @return void
  44. */
  45. public function createCodeCoverageReport(\PHP_CodeCoverage $coverage = null)
  46. {
  47. if (!isset($coverage)) {
  48. echo '<h3>Code coverage analysis unavailable</h3><p>To enable code coverage, the xdebug php module must be installed and enabled.</p>';
  49. return;
  50. }
  51. $writer = new \PHP_CodeCoverage_Report_HTML;
  52. $writer->process($coverage, FULL_PATH . '/test/runtime/coverage');
  53. echo '<a href="runtime/coverage" target="_blank">Code coverage report</a>';
  54. }
  55. /**
  56. * Prints test header.
  57. *
  58. * @param xTest $test Test.
  59. * @return void
  60. */
  61. public function testHeader(xTest $test)
  62. {
  63. $class = get_class($test);
  64. echo '<h3>Test Class: ' . htmlspecialchars($class) . '</h3>';
  65. echo '<table id="' . $class . '-results"><tr><th>Test</th><th>Result</th></tr>';
  66. }
  67. /**
  68. * Test case result.
  69. *
  70. * @param \ReflectionMethod $testCaseMethod Test case method.
  71. * @param string $resultColor Result color.
  72. * @param string $resultMessage Result message.
  73. * @return void
  74. */
  75. public function testCaseResult(\ReflectionMethod $testCaseMethod, $resultColor, $resultMessage)
  76. {
  77. echo '<tr style="color:white; background:' . $resultColor . '"><td>(' . $testCaseMethod->getStartLine() . ') ';
  78. echo '<a style="color:white" href="?' . $this->getTestCaseName($testCaseMethod) . '">' . $this->getTestCaseName($testCaseMethod, true) . '</a>';
  79. echo '</td><td><pre>' . htmlspecialchars($resultMessage) . '</pre></td></tr>';
  80. }
  81. /**
  82. * Prints test footer.
  83. *
  84. * @param xTest $test Test.
  85. * @param integer $total Total test case count.
  86. * @param integer $passed Passed test case count.
  87. * @return void
  88. */
  89. public function testFooter(xTest $test, $total, $passed)
  90. {
  91. echo '<tr style="background-color:gray; color:white"><tr><th>' . $total . ' Tests</th><th>';
  92. if ($passed == $total) {
  93. echo 'All Tests Passed' . PHP_EOL;
  94. } else {
  95. echo ($total - $passed) . ' Tests Failed' . PHP_EOL;
  96. }
  97. echo '</th></tr></table>';
  98. if ($passed == $total) {
  99. $class = get_class($test);
  100. echo '<h4 id="' . $class . '-toggle" style="cursor:pointer" onclick="' . "document.getElementById('{$class}-results').style.display='table'; document.getElementById('{$class}-toggle').style.display='none'; return false;" . '">&raquo; All Tests Passed</h4><script type="text/javascript">document.getElementById("' . $class . '-results").style.display="none";</script>';
  101. }
  102. }
  103. }