CliResultPrinter.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace mindplay\test\lib\ResultPrinter;
  3. use mindplay\test\lib\Colors;
  4. use mindplay\test\lib\xTest;
  5. use mindplay\test\lib\xTestRunner;
  6. class CliResultPrinter extends ResultPrinter
  7. {
  8. /**
  9. * Colors.
  10. *
  11. * @var Colors
  12. */
  13. protected $colors;
  14. /**
  15. * Creates CLI result printer instance.
  16. *
  17. * @param Colors $colors Colors.
  18. */
  19. public function __construct(Colors $colors)
  20. {
  21. $this->colors = $colors;
  22. }
  23. /**
  24. * Prints the header before the test output.
  25. *
  26. * @param xTestRunner $testRunner Test runner.
  27. * @param string $pattern Test filename pattern.
  28. * @return void
  29. */
  30. public function suiteHeader(xTestRunner $testRunner, $pattern)
  31. {
  32. echo 'Unit Tests' . PHP_EOL;
  33. echo 'Codebase: ' . $testRunner->getRootPath() . PHP_EOL;
  34. echo 'Test Suite: ' . $pattern . PHP_EOL;
  35. }
  36. /**
  37. * Creates code coverage report.
  38. *
  39. * @param \PHP_CodeCoverage $coverage Code coverage collector.
  40. * @return void
  41. */
  42. public function createCodeCoverageReport(\PHP_CodeCoverage $coverage = null)
  43. {
  44. if (!isset($coverage)) {
  45. echo 'Code coverage analysis unavailable. To enable code coverage, the xdebug php module must be installed and enabled.' . PHP_EOL;
  46. return;
  47. }
  48. echo PHP_EOL . 'Creating code coverage report in clover format ... ';
  49. $writer = new \PHP_CodeCoverage_Report_Clover;
  50. $writer->process($coverage, FULL_PATH . '/clover.xml');
  51. echo 'done' . PHP_EOL;
  52. }
  53. /**
  54. * Prints test header.
  55. *
  56. * @param xTest $test Test.
  57. * @return void
  58. */
  59. public function testHeader(xTest $test)
  60. {
  61. $class = get_class($test);
  62. echo PHP_EOL;
  63. echo 'Test Class: ' . $this->colors->getColoredString($class, 'blue') . PHP_EOL;
  64. echo 'Results:' . PHP_EOL;
  65. }
  66. /**
  67. * Test case result.
  68. *
  69. * @param \ReflectionMethod $testCaseMethod Test case method.
  70. * @param string $resultColor Result color.
  71. * @param string $resultMessage Result message.
  72. * @return void
  73. */
  74. public function testCaseResult(\ReflectionMethod $testCaseMethod, $resultColor, $resultMessage)
  75. {
  76. echo '(' . $testCaseMethod->getStartLine() . ') ' . $this->getTestCaseName($testCaseMethod, true);
  77. echo ' - ' . $this->colors->getColoredString($resultMessage, $resultColor);
  78. echo PHP_EOL;
  79. }
  80. /**
  81. * Prints test footer.
  82. *
  83. * @param xTest $test Test.
  84. * @param integer $total Total test case count.
  85. * @param integer $passed Passed test case count.
  86. * @return void
  87. */
  88. public function testFooter(xTest $test, $total, $passed)
  89. {
  90. echo $total . ' Tests. ';
  91. if ($passed == $total) {
  92. echo $this->colors->getColoredString('All Tests Passed', 'green') . PHP_EOL;
  93. } else {
  94. echo $this->colors->getColoredString(($total - $passed) . ' Tests Failed', 'red') . PHP_EOL;
  95. }
  96. }
  97. }