Skip to content

Commit 953a82f

Browse files
committed
chore: fix merge conflicts
1 parent f348ba9 commit 953a82f

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

src/ResultPrinter90.php

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
3+
namespace Codedungeon\PHPUnitPrettyResultPrinter;
4+
5+
use PHPUnit\Util\Filter;
6+
use PHPUnit\Framework\Test;
7+
use PHPUnit\Runner\Version;
8+
use Bakyt\Console\Phanybar;
9+
use PHPUnit\Framework\TestResult;
10+
use PHPUnit\TextUI\DefaultResultPrinter;
11+
use PHPUnit\Framework\TestFailure;
12+
13+
$low = version_compare(Version::series(), '9.0', '>=');
14+
$high = true; // version_compare(Version::series(),'7.1.99','<=');
15+
16+
if ($low && $high) {
17+
class ResultPrinter90 extends DefaultResultPrinter
18+
{
19+
private $defectListPrinted = false;
20+
21+
private $reverse = true;
22+
23+
public function startTest(Test $test): void
24+
{
25+
$this->className = \get_class($test);
26+
parent::startTest($test);
27+
}
28+
29+
protected function writeProgress(string $progress): void
30+
{
31+
$this->writeProgressEx($progress);
32+
}
33+
34+
protected function writeProgressWithColor(string $progress, string $buffer): void
35+
{
36+
$this->writeProgressWithColorEx($progress, $buffer);
37+
}
38+
39+
protected function printFooter(TestResult $result): void
40+
{
41+
parent::printFooter($result);
42+
if ($this->anyBarEnabled) {
43+
$phanyBar = new Phanybar();
44+
if (sizeof($result->failures())) {
45+
// if errors, we will always show red bar
46+
$phanyBar->send('exclamation', $this->anyBarPort);
47+
} else {
48+
// if no errors and successful, show green
49+
if ($result->wasSuccessful()) {
50+
$phanyBar->send('green', $this->anyBarPort);
51+
} else {
52+
// otherwise show yellow for remaining
53+
$phanyBar->send('yellow', $this->anyBarPort);
54+
}
55+
}
56+
}
57+
}
58+
59+
protected function formatExceptionMsg($exceptionMessage): string
60+
{
61+
$exceptionMessage = preg_replace('/%/u', '%%', $exceptionMessage);
62+
63+
$exceptionMessage = str_replace("+++ Actual\n", '', $exceptionMessage);
64+
$exceptionMessage = str_replace("--- Expected\n", '', $exceptionMessage);
65+
$exceptionMessage = str_replace('@@ @@', '', $exceptionMessage);
66+
67+
if (strpos($exceptionMessage, 'This test did not perform any assertions') !== false) {
68+
$exceptionMessage = $this->setMessageColor('risky', 'This test did not perform any assertions.');
69+
} else {
70+
$marker = $this->markers['fail'];
71+
if ($this->colors) {
72+
$exceptionMessage = preg_replace('/^(Exception.*)$/m', "\033[01;31m$1\033[0m", $exceptionMessage);
73+
$exceptionMessage = preg_replace('/(Failed.*)$/m', "\033[01;31m %1\$s$1\033[0m", $exceptionMessage);
74+
$exceptionMessage = preg_replace("/(\-+.*)$/m", "\033[01;32m$1\033[0m", $exceptionMessage);
75+
$exceptionMessage = preg_replace("/(\++.*)$/m", "\033[01;31m$1\033[0m", $exceptionMessage);
76+
}
77+
78+
$exceptionMessage = sprintf($exceptionMessage, $marker);
79+
}
80+
81+
$exceptionMessage = ' ' . $exceptionMessage;
82+
83+
return "$exceptionMessage";
84+
}
85+
86+
protected function printDefectTrace(TestFailure $defect):void
87+
{
88+
$this->write($this->formatExceptionMsg($defect->getExceptionAsString()));
89+
$trace = Filter::getFilteredStacktrace(
90+
$defect->thrownException()
91+
);
92+
if (!empty($trace)) {
93+
$this->write("\n" . $trace);
94+
}
95+
$exception = $defect->thrownException()->getPrevious();
96+
while ($exception) {
97+
$this->write(
98+
"\nCaused by\n" .
99+
TestFailure::exceptionToString($exception) . "\n" .
100+
Filter::getFilteredStacktrace($exception)
101+
);
102+
$exception = $exception->getPrevious();
103+
}
104+
}
105+
106+
protected function printDefects(array $defects, string $type): void
107+
{
108+
$count = \count($defects);
109+
110+
if ($count == 0) {
111+
return;
112+
}
113+
114+
if ($this->defectListPrinted) {
115+
$this->write("\n--\n\n");
116+
}
117+
118+
$msg = \sprintf("There %s %d %s%s:\n", ($count == 1) ? 'was' : 'were', $count, $type, ($count == 1) ? '' : 's');
119+
$msg = $this->setMessageColor($type, $msg);
120+
121+
$this->write($msg);
122+
123+
$i = 1;
124+
125+
if ($this->reverse) {
126+
$defects = \array_reverse($defects);
127+
}
128+
129+
foreach ($defects as $defect) {
130+
$this->printDefect($defect, $i++);
131+
}
132+
133+
$this->defectListPrinted = true;
134+
}
135+
136+
/**
137+
* @param string $type
138+
* @param $msg
139+
*
140+
* @return string
141+
*/
142+
protected function setMessageColor(string $type, $msg): string
143+
{
144+
// 30m == gray
145+
// 31m == red
146+
// 32m == green
147+
// 33m == yellow
148+
// 34m == blue (cyan)
149+
// 35m == magenta
150+
// 36m == cyan
151+
// 37m == white
152+
153+
$color = '37';
154+
$marker = '';
155+
156+
if (strpos($type, 'failure') !== false || strpos($type, 'error') !== false) {
157+
$color = '31';
158+
$marker = 'fail';
159+
} elseif (strpos($type, 'incomplete') !== false) {
160+
$color = '34';
161+
$marker = 'incomplete';
162+
} elseif (strpos($type, 'risky') !== false) {
163+
$color = '35';
164+
$marker = 'risky';
165+
} elseif (strpos($type, 'skipped') !== false) {
166+
$color = '33';
167+
$marker = 'skipped';
168+
}
169+
170+
// if user turned off colors, return msg as we are down
171+
if (!$this->colors) {
172+
return $msg;
173+
}
174+
175+
// otherwise, we have colors enabled and time to make it pretty
176+
$testMarker = $this->markers[$marker] ?? '';
177+
178+
return "\033[01;{$color}m{$testMarker}{$msg}\033[0m";
179+
}
180+
}
181+
}

0 commit comments

Comments
 (0)