-
Notifications
You must be signed in to change notification settings - Fork 16
/
KoanPrinter.php
127 lines (109 loc) · 3.64 KB
/
KoanPrinter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace KoansLib;
use PHPUnit\Framework\TestResult;
use PHPUnit\Util\TestDox\CliTestDoxPrinter;
/**
* Class KoanPrinter
*
* Override the basic PHPUnit ResultPrinter to produce a more mindful output
*
* @package KoansLib
*/
class KoanPrinter extends CliTestDoxPrinter
{
public function printResult(TestResult $result): void
{
$this->printHeader();
$this->printFooter($result);
}
protected function printHeader(): void
{
$this->write("\n\n");
}
protected function printFooter(TestResult $result): void
{
if (\count($result) === 0) {
$this->writeWithColor(
'fg-black, bg-yellow',
'No tests executed!'
);
return;
}
if ($result->wasSuccessful() &&
$result->allHarmless() &&
$result->allCompletelyImplemented() &&
$result->noneSkipped()) {
$this->writeWithColor(
'fg-black, bg-green',
\sprintf(
'You have traversed the mountain path. You have found peace. (%d koan%s)',
\count($result),
(\count($result) == 1) ? '' : 's',
$this->numAssertions,
($this->numAssertions == 1) ? '' : 's'
)
);
} else {
if ($result->wasSuccessful()) {
$color = 'fg-black, bg-yellow';
if ($this->verbose || !$result->allHarmless()) {
$this->write("\n");
}
$this->writeWithColor(
$color,
'OK, but incomplete, skipped, or risky tests!'
);
} else {
$this->write("\n");
if ($result->errorCount()) {
$color = 'fg-white, bg-red';
$this->writeWithColor(
$color,
'Error: You have stumbled on your path, but you are capable of recovering!'
);
} elseif ($result->failureCount()) {
$color = 'fg-white, bg-blue';
$this->writeWithColor(
$color,
'You are making progress but have more to learn. Contemplate the message above further.'
);
} elseif ($result->warningCount()) {
$color = 'fg-white, bg-blue';
$this->writeWithColor(
$color,
'WARNINGS!'
);
}
}
$this->writeCountString($this->numTests, 'Total Koans', $color, true);
$this->writeCountString(count($result->passed()), 'Koans Completed', $color, true);
$this->writeCountString($result->errorCount(), 'Errors', $color);
$this->writeWithColor($color, '.');
}
}
/**
* Copied verbatim from ResultPrinter because it is private and the printFooter calls this.
*
* @param int $count
* @param string $name
* @param string $color
* @param bool $always
*/
private function writeCountString(int $count, string $name, string $color, bool $always = false): void
{
static $first = true;
if ($always || $count > 0) {
$this->writeWithColor(
$color,
\sprintf(
'%s%s: %d',
!$first ? ', ' : '',
$name,
$count
),
false
);
$first = false;
}
}
}