Skip to content

Commit 5a6882e

Browse files
author
Alexander Obuhovich
committed
Test suite improvements
1. moving out suite progress output to new ResultPrinter classes 2. create CliResultPrinter class to be used, when invoked from CLI 3. creating WebResultPrinter class to be used, when invoked from Web 4. send proper exit code to allow usage from Travis CI
1 parent 2b3cda8 commit 5a6882e

File tree

9 files changed

+505
-103
lines changed

9 files changed

+505
-103
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
composer.lock
22
vendor
3+
clover.xml

test/lib/Colors.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
namespace mindplay\test\lib;
3+
4+
5+
class Colors
6+
{
7+
private $foregroundColors = array(
8+
'black' => '0;30',
9+
'dark_gray' => '1;30',
10+
'blue' => '0;34',
11+
'light_blue' => '1;34',
12+
'green' => '0;32',
13+
'light_green' => '1;32',
14+
'cyan' => '0;36',
15+
'light_cyan' => '1;36',
16+
'red' => '0;31',
17+
'light_red' => '1;31',
18+
'purple' => '0;35',
19+
'light_purple' => '1;35',
20+
'brown' => '0;33',
21+
'yellow' => '1;33',
22+
'light_gray' => '0;37',
23+
'white' => '1;37',
24+
);
25+
26+
private $backgroundColors = array(
27+
'black' => '40',
28+
'red' => '41',
29+
'green' => '42',
30+
'yellow' => '43',
31+
'blue' => '44',
32+
'magenta' => '45',
33+
'cyan' => '46',
34+
'light_gray' => '47',
35+
);
36+
37+
/**
38+
* Returns colored string
39+
*
40+
* @param string $text Text to color.
41+
* @param string $foregroundColor Foreground color.
42+
* @param string $backgroundColor Background color.
43+
* @return string
44+
*/
45+
public function getColoredString($text, $foregroundColor = null, $backgroundColor = null)
46+
{
47+
$coloredString = '';
48+
49+
// check if given foreground color found
50+
if (isset($this->foregroundColors[$foregroundColor])) {
51+
$coloredString .= "\033[" . $this->foregroundColors[$foregroundColor] . 'm';
52+
}
53+
54+
// check if given background color found
55+
if (isset($this->backgroundColors[$backgroundColor])) {
56+
$coloredString .= "\033[" . $this->backgroundColors[$backgroundColor] . 'm';
57+
}
58+
59+
// Add string and end coloring
60+
$coloredString .= $text . "\033[0m";
61+
62+
return $coloredString;
63+
}
64+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
namespace mindplay\test\lib\ResultPrinter;
3+
4+
5+
use mindplay\test\lib\Colors;
6+
use mindplay\test\lib\xTest;
7+
use mindplay\test\lib\xTestRunner;
8+
9+
class CliResultPrinter extends ResultPrinter
10+
{
11+
12+
/**
13+
* Colors.
14+
*
15+
* @var Colors
16+
*/
17+
protected $colors;
18+
19+
/**
20+
* Creates CLI result printer instance.
21+
*
22+
* @param Colors $colors Colors.
23+
*/
24+
public function __construct(Colors $colors)
25+
{
26+
$this->colors = $colors;
27+
}
28+
29+
/**
30+
* Prints the header before the test output.
31+
*
32+
* @param xTestRunner $testRunner Test runner.
33+
* @param string $pattern Test filename pattern.
34+
* @return void
35+
*/
36+
public function suiteHeader(xTestRunner $testRunner, $pattern)
37+
{
38+
echo 'Unit Tests' . PHP_EOL;
39+
echo 'Codebase: ' . $testRunner->getRootPath() . PHP_EOL;
40+
echo 'Test Suite: ' . $pattern . PHP_EOL;
41+
}
42+
43+
/**
44+
* Creates code coverage report.
45+
*
46+
* @param \PHP_CodeCoverage $coverage Code coverage collector.
47+
* @return void
48+
*/
49+
public function createCodeCoverageReport(\PHP_CodeCoverage $coverage = null)
50+
{
51+
if (!isset($coverage)) {
52+
echo 'Code coverage analysis unavailable. To enable code coverage, the xdebug php module must be installed and enabled.' . PHP_EOL;
53+
54+
return;
55+
}
56+
57+
echo PHP_EOL . 'Creating code coverage report in clover format ... ';
58+
$writer = new \PHP_CodeCoverage_Report_Clover;
59+
$writer->process($coverage, FULL_PATH . '/clover.xml');
60+
echo 'done' . PHP_EOL;
61+
}
62+
63+
/**
64+
* Prints test header.
65+
*
66+
* @param xTest $test Test.
67+
* @return void
68+
*/
69+
public function testHeader(xTest $test)
70+
{
71+
$class = get_class($test);
72+
73+
echo PHP_EOL;
74+
echo 'Test Class: ' . $this->colors->getColoredString($class, 'blue') . PHP_EOL;
75+
echo 'Results:' . PHP_EOL;
76+
}
77+
78+
/**
79+
* Test case result.
80+
*
81+
* @param \ReflectionMethod $testCaseMethod Test case method.
82+
* @param string $resultColor Result color.
83+
* @param string $resultMessage Result message.
84+
* @return void
85+
*/
86+
public function testCaseResult(\ReflectionMethod $testCaseMethod, $resultColor, $resultMessage)
87+
{
88+
echo '(' . $testCaseMethod->getStartLine() . ') ' . $this->getTestCaseName($testCaseMethod, true);
89+
echo ' - ' . $this->colors->getColoredString($resultMessage, $resultColor);
90+
echo PHP_EOL;
91+
}
92+
93+
/**
94+
* Prints test footer.
95+
*
96+
* @param xTest $test Test.
97+
* @param integer $total Total test case count.
98+
* @param integer $passed Passed test case count.
99+
* @return void
100+
*/
101+
public function testFooter(xTest $test, $total, $passed)
102+
{
103+
echo $total . ' Tests. ';
104+
105+
if ($passed == $total) {
106+
echo $this->colors->getColoredString('All Tests Passed', 'green') . PHP_EOL;
107+
} else {
108+
echo $this->colors->getColoredString(($total - $passed) . ' Tests Failed', 'red') . PHP_EOL;
109+
}
110+
}
111+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
namespace mindplay\test\lib\ResultPrinter;
3+
4+
5+
use mindplay\test\lib\xTest;
6+
use mindplay\test\lib\xTestRunner;
7+
8+
abstract class ResultPrinter
9+
{
10+
/**
11+
* Prints the header before the test output.
12+
*
13+
* @param xTestRunner $testRunner Test runner.
14+
* @param string $pattern Test filename pattern.
15+
* @return void
16+
*/
17+
public function suiteHeader(xTestRunner $testRunner, $pattern)
18+
{
19+
20+
}
21+
22+
/**
23+
* Prints the footer after the test output.
24+
*
25+
* @param xTestRunner $testRunner Test runner.
26+
* @return void
27+
*/
28+
public function suiteFooter(xTestRunner $testRunner)
29+
{
30+
31+
}
32+
33+
/**
34+
* Creates code coverage report.
35+
*
36+
* @param \PHP_CodeCoverage $coverage Code coverage collector.
37+
* @return void
38+
*/
39+
public function createCodeCoverageReport(\PHP_CodeCoverage $coverage = null)
40+
{
41+
42+
}
43+
44+
/**
45+
* Prints test header.
46+
*
47+
* @param xTest $test Test.
48+
* @return void
49+
*/
50+
public function testHeader(xTest $test)
51+
{
52+
53+
}
54+
55+
/**
56+
* Prints test footer.
57+
*
58+
* @param xTest $test Test.
59+
* @param integer $total Total test case count.
60+
* @param integer $passed Passed test case count.
61+
* @return void
62+
*/
63+
public function testFooter(xTest $test, $total, $passed)
64+
{
65+
66+
}
67+
68+
/**
69+
* Test case result.
70+
*
71+
* @param \ReflectionMethod $testCaseMethod Test case method.
72+
* @param string $resultColor Result color.
73+
* @param string $resultMessage Result message.
74+
* @return void
75+
*/
76+
public function testCaseResult(\ReflectionMethod $testCaseMethod, $resultColor, $resultMessage)
77+
{
78+
79+
}
80+
81+
/**
82+
* Returns test case name.
83+
*
84+
* @param \ReflectionMethod $testCaseMethod Test case method.
85+
* @param boolean $humanFormat Use human format.
86+
* @return string
87+
*/
88+
protected function getTestCaseName(\ReflectionMethod $testCaseMethod, $humanFormat = false)
89+
{
90+
$ret = substr($testCaseMethod->name, 4);
91+
92+
return $humanFormat ? ltrim(preg_replace('/([A-Z])/', ' \1', $ret)) : $ret;
93+
}
94+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
namespace mindplay\test\lib\ResultPrinter;
3+
4+
5+
use mindplay\test\lib\xTest;
6+
use mindplay\test\lib\xTestRunner;
7+
8+
class WebResultPrinter extends ResultPrinter
9+
{
10+
11+
/**
12+
* Prints the header before the test output.
13+
*
14+
* @param xTestRunner $testRunner Test runner.
15+
* @param string $pattern Test filename pattern.
16+
* @return void
17+
*/
18+
public function suiteHeader(xTestRunner $testRunner, $pattern)
19+
{
20+
echo '<html>
21+
<head>
22+
<title>Unit Tests</title>
23+
<style type="text/css">
24+
table { border-collapse:collapse; }
25+
td, th { text-align:left; padding:2px 6px; border:solid 1px #aaa; }
26+
</style>
27+
</head>
28+
<body>
29+
<h2>Unit Tests</h2>';
30+
31+
echo '<h4>Codebase: ' . $testRunner->getRootPath() . '</h4>';
32+
echo '<h4>Test Suite: ' . $pattern . '</h4>';
33+
}
34+
35+
/**
36+
* Prints the footer after the test output.
37+
*
38+
* @param xTestRunner $testRunner Test runner.
39+
* @return void
40+
*/
41+
public function suiteFooter(xTestRunner $testRunner)
42+
{
43+
echo '</body></html>';
44+
}
45+
46+
/**
47+
* Creates code coverage report.
48+
*
49+
* @param \PHP_CodeCoverage $coverage Code coverage collector.
50+
* @return void
51+
*/
52+
public function createCodeCoverageReport(\PHP_CodeCoverage $coverage = null)
53+
{
54+
if (!isset($coverage)) {
55+
echo '<h3>Code coverage analysis unavailable</h3><p>To enable code coverage, the xdebug php module must be installed and enabled.</p>';
56+
57+
return;
58+
}
59+
60+
$writer = new \PHP_CodeCoverage_Report_HTML;
61+
$writer->process($coverage, FULL_PATH . '/test/runtime/coverage');
62+
63+
echo '<a href="runtime/coverage" target="_blank">Code coverage report</a>';
64+
}
65+
66+
/**
67+
* Prints test header.
68+
*
69+
* @param xTest $test Test.
70+
* @return void
71+
*/
72+
public function testHeader(xTest $test)
73+
{
74+
$class = get_class($test);
75+
76+
echo '<h3>Test Class: ' . htmlspecialchars($class) . '</h3>';
77+
echo '<table id="' . $class . '-results"><tr><th>Test</th><th>Result</th></tr>';
78+
}
79+
80+
/**
81+
* Test case result.
82+
*
83+
* @param \ReflectionMethod $testCaseMethod Test case method.
84+
* @param string $resultColor Result color.
85+
* @param string $resultMessage Result message.
86+
* @return void
87+
*/
88+
public function testCaseResult(\ReflectionMethod $testCaseMethod, $resultColor, $resultMessage)
89+
{
90+
echo '<tr style="color:white; background:' . $resultColor . '"><td>(' . $testCaseMethod->getStartLine() . ') ';
91+
echo '<a style="color:white" href="?' . $this->getTestCaseName($testCaseMethod) . '">' . $this->getTestCaseName($testCaseMethod, true) . '</a>';
92+
echo '</td><td><pre>' . htmlspecialchars($resultMessage) . '</pre></td></tr>';
93+
}
94+
95+
/**
96+
* Prints test footer.
97+
*
98+
* @param xTest $test Test.
99+
* @param integer $total Total test case count.
100+
* @param integer $passed Passed test case count.
101+
* @return void
102+
*/
103+
public function testFooter(xTest $test, $total, $passed)
104+
{
105+
echo '<tr style="background-color:gray; color:white"><tr><th>' . $total . ' Tests</th><th>';
106+
107+
if ($passed == $total) {
108+
echo 'All Tests Passed' . PHP_EOL;
109+
} else {
110+
echo ($total - $passed) . ' Tests Failed' . PHP_EOL;
111+
}
112+
113+
echo '</th></tr></table>';
114+
115+
if ($passed == $total) {
116+
$class = get_class($test);
117+
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>';
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)