Skip to content

Commit aaa7543

Browse files
Fixes #74, added marker customization in .yml files
1 parent cfb694d commit aaa7543

File tree

4 files changed

+120
-87
lines changed

4 files changed

+120
-87
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codedungeon/phpunit-result-printer",
3-
"version": "0.14.0",
3+
"version": "0.15.0",
44
"description": "PHPUnit Pretty Result Printer",
55
"keywords": ["phpunit", "printer", "result-printer", "composer", "package", "testing"],
66
"license": "MIT",

phpunit-printer.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ options:
22
cd-printer-hide-class: false
33
cd-printer-simple-output: false
44
cd-printer-show-config: true
5-
marks:
6-
cd-pass: "✔️"
7-
cd-fail: ""
5+
markers:
6+
cd-pass: ""
7+
cd-fail: ""
88
cd-error: ""
9-
cd-skipped: ""
10-
cd-incomplete: ""
9+
cd-skipped: ""
10+
cd-incomplete: ""

src/Printer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Codedungeon\PHPUnitPrettyResultPrinter;
44

55
use PHPUnit_TextUI_ResultPrinter;
6+
use PHPUnit\Runner\Version;
67

78
if (class_exists(PHPUnit_TextUI_ResultPrinter::class)) {
89
require __DIR__ . '/ResultPrinter5.php';
@@ -13,8 +14,6 @@ class Printer extends ResultPrinter5
1314
}
1415
}
1516

16-
use PHPUnit\Runner\Version;
17-
1817
if (version_compare(Version::series(), '6.99.99', '<=')) {
1918
require __DIR__ . '/ResultPrinter6.php';
2019

src/PrinterTrait.php

Lines changed: 113 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
use Noodlehaus\Config;
66
use Codedungeon\PHPCliColors\Color;
7+
use Noodlehaus\Exception\EmptyDirectoryException;
78

89
trait PrinterTrait
910
{
11+
/**
12+
* @var bool
13+
*/
1014
protected static $init = false;
1115
/**
1216
* @var string
@@ -40,10 +44,18 @@ trait PrinterTrait
4044
* @var string
4145
*/
4246
private $configFileName;
47+
/**
48+
* @var array|null
49+
*/
4350
private $printerOptions;
51+
/**
52+
* @var mixed|null
53+
*/
4454
private $showConfig;
45-
private $passMark;
46-
private $failMark;
55+
/**
56+
* @var array
57+
*/
58+
private $markers = [];
4759

4860
/**
4961
* {@inheritdoc}
@@ -59,19 +71,29 @@ public function __construct(
5971

6072
$this->configFileName = $this->getConfigurationFile('phpunit-printer.yml');
6173
$this->colorsTool = new Color();
62-
$this->configuration = new Config($this->configFileName);
74+
try {
75+
$this->configuration = new Config($this->configFileName);
76+
} catch (EmptyDirectoryException $e) {
77+
echo $this->colorsTool->red() . 'Unable to locate valid configuration file' . PHP_EOL;
78+
echo $this->colorsTool->reset();
79+
}
6380

6481
$this->maxNumberOfColumns = $this->getWidth();
65-
$this->maxClassNameLength = min((int) ($this->maxNumberOfColumns / 2), $this->maxClassNameLength);
82+
$this->maxClassNameLength = min((int)($this->maxNumberOfColumns / 2), $this->maxClassNameLength);
6683

6784
// setup module options
6885
$this->printerOptions = $this->configuration->all();
6986
$this->hideClassName = $this->configuration->get('options.cd-printer-hide-class');
7087
$this->simpleOutput = $this->configuration->get('options.cd-printer-simple-output');
7188
$this->showConfig = $this->configuration->get('options.cd-printer-show-config');
7289

73-
$this->passMark = $this->configuration->get('marks.cd-pass');
74-
$this->failMark = $this->configuration->get('marks.cd-fail');
90+
$this->markers = [
91+
'pass' => $this->configuration->get('markers.cd-pass'),
92+
'fail' => $this->configuration->get('markers.cd-fail'),
93+
'error' => $this->configuration->get('markers.cd-error'),
94+
'skipped' => $this->configuration->get('markers.cd-skipped'),
95+
'incomplete' => $this->configuration->get('markers.cd-incomplete'),
96+
];
7597

7698
$this->init();
7799
}
@@ -102,48 +124,38 @@ public function getConfigurationFile($configFileName = 'phpunit-printer.yml')
102124
}
103125

104126
/**
105-
* @return string | returns package root
127+
* @return string
106128
*/
107-
private function getPackageRoot()
129+
public function version()
108130
{
109-
return \dirname(__FILE__, 2);
110-
}
131+
$content = file_get_contents($this->getPackageRoot() . DIRECTORY_SEPARATOR . 'composer.json');
132+
if ($content) {
133+
$content = json_decode($content, true);
111134

112-
/**
113-
* @return bool
114-
*/
115-
private function isWindows()
116-
{
117-
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
135+
return $content['version'];
136+
}
137+
138+
return 'n/a';
118139
}
119140

120141
/**
121-
* Gets the terminal width.
122-
*
123-
* @return int
142+
* @return string
124143
*/
125-
private function getWidth()
144+
public function packageName()
126145
{
127-
$width = 0;
128-
if ($this->isWindows()) {
129-
return 96; // create a default width to be used on windows
130-
}
131-
132-
exec('stty size 2>/dev/null', $out, $exit);
133-
134-
// 'stty size' output example: 36 120
135-
if (\count($out) > 0) {
136-
$width = (int) explode(' ', array_pop($out))[1];
137-
}
146+
$content = file_get_contents($this->getPackageRoot() . DIRECTORY_SEPARATOR . 'composer.json');
147+
if ($content) {
148+
$content = json_decode($content, true);
138149

139-
// handle CircleCI case (probably the same with TravisCI as well)
140-
if ($width === 0) {
141-
$width = 96;
150+
return $content['description'];
142151
}
143152

144-
return $width;
153+
return 'n/a';
145154
}
146155

156+
/**
157+
*
158+
*/
147159
protected function init()
148160
{
149161
if (!self::$init) {
@@ -154,8 +166,11 @@ protected function init()
154166
echo $this->colorsTool->reset();
155167

156168
if ($this->showConfig) {
157-
echo $this->colorsTool->white() . 'Configuration: ';
158-
echo $this->colorsTool->white() . $this->configFileName;
169+
$home = getenv('HOME');
170+
$filename = str_replace($home, '~', $this->configFileName);
171+
172+
echo $this->colorsTool->yellow() . 'Configuration: ';
173+
echo $this->colorsTool->yellow() . $filename;
159174
echo $this->colorsTool->reset();
160175
echo PHP_EOL . PHP_EOL;
161176
}
@@ -164,33 +179,9 @@ protected function init()
164179
}
165180
}
166181

167-
public function version()
168-
{
169-
$content = file_get_contents($this->getPackageRoot() . DIRECTORY_SEPARATOR . 'composer.json');
170-
if ($content) {
171-
$content = json_decode($content, true);
172-
173-
return $content['version'];
174-
}
175-
176-
return 'n/a';
177-
}
178-
179182
/**
180-
* @return string
183+
* @param $progress
181184
*/
182-
public function packageName()
183-
{
184-
$content = file_get_contents($this->getPackageRoot() . DIRECTORY_SEPARATOR . 'composer.json');
185-
if ($content) {
186-
$content = json_decode($content, true);
187-
188-
return $content['description'];
189-
}
190-
191-
return 'n/a';
192-
}
193-
194185
protected function writeProgressEx($progress)
195186
{
196187
if (!$this->debug) {
@@ -218,6 +209,61 @@ protected function printClassName()
218209
$this->lastClassName = $this->className;
219210
}
220211

212+
/**
213+
* {@inheritdoc}
214+
*/
215+
protected function writeProgressWithColorEx($color, $buffer)
216+
{
217+
if (!$this->debug) {
218+
$this->printClassName();
219+
}
220+
221+
$this->printTestCaseStatus($color, $buffer);
222+
}
223+
224+
/**
225+
* @return string | returns package root
226+
*/
227+
private function getPackageRoot()
228+
{
229+
return \dirname(__FILE__, 2);
230+
}
231+
232+
/**
233+
* @return bool
234+
*/
235+
private function isWindows()
236+
{
237+
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
238+
}
239+
240+
/**
241+
* Gets the terminal width.
242+
*
243+
* @return int
244+
*/
245+
private function getWidth()
246+
{
247+
$width = 0;
248+
if ($this->isWindows()) {
249+
return 96; // create a default width to be used on windows
250+
}
251+
252+
exec('stty size 2>/dev/null', $out, $exit);
253+
254+
// 'stty size' output example: 36 120
255+
if (\count($out) > 0) {
256+
$width = (int)explode(' ', array_pop($out))[1];
257+
}
258+
259+
// handle CircleCI case (probably the same with TravisCI as well)
260+
if ($width === 0) {
261+
$width = 96;
262+
}
263+
264+
return $width;
265+
}
266+
221267
/**
222268
* @param string $className
223269
*
@@ -268,27 +314,27 @@ private function printTestCaseStatus($color, $buffer)
268314
switch (strtoupper($buffer)) {
269315
case '.':
270316
$color = 'fg-green,bold';
271-
$buffer = $this->simpleOutput ? '.' : mb_convert_encoding("\x27\x13", 'UTF-8', 'UTF-16BE');
317+
$buffer = $this->simpleOutput ? '.' : $this->markers['pass']; // mb_convert_encoding("\x27\x13", 'UTF-8', 'UTF-16BE');
272318
$buffer .= (!$this->debug) ? '' : ' Passed';
273319
break;
274320
case 'S':
275321
$color = 'fg-yellow,bold';
276-
$buffer = $this->simpleOutput ? 'S' : mb_convert_encoding("\x27\xA6", 'UTF-8', 'UTF-16BE');
322+
$buffer = $this->simpleOutput ? 'S' : $this->markers['skipped']; // mb_convert_encoding("\x27\xA6", 'UTF-8', 'UTF-16BE');
277323
$buffer .= !$this->debug ? '' : ' Skipped';
278324
break;
279325
case 'I':
280326
$color = 'fg-blue,bold';
281-
$buffer = $this->simpleOutput ? 'I' : '';
327+
$buffer = $this->simpleOutput ? 'I' : $this->markers['incomplete']; // 'ℹ';
282328
$buffer .= !$this->debug ? '' : ' Incomplete';
283329
break;
284330
case 'F':
285331
$color = 'fg-red,bold';
286-
$buffer = $this->simpleOutput ? 'F' : mb_convert_encoding("\x27\x16", 'UTF-8', 'UTF-16BE');
332+
$buffer = $this->simpleOutput ? 'F' : $this->markers['fail']; // mb_convert_encoding("\x27\x16", 'UTF-8', 'UTF-16BE');
287333
$buffer .= (!$this->debug) ? '' : ' Fail';
288334
break;
289335
case 'E':
290336
$color = 'fg-red,bold';
291-
$buffer = $this->simpleOutput ? 'E' : '';
337+
$buffer = $this->simpleOutput ? 'E' : $this->makers['error']; // '⚈';
292338
$buffer .= !$this->debug ? '' : ' Error';
293339
break;
294340
}
@@ -300,16 +346,4 @@ private function printTestCaseStatus($color, $buffer)
300346
}
301347
$this->column += 2;
302348
}
303-
304-
/**
305-
* {@inheritdoc}
306-
*/
307-
protected function writeProgressWithColorEx($color, $buffer)
308-
{
309-
if (!$this->debug) {
310-
$this->printClassName();
311-
}
312-
313-
$this->printTestCaseStatus($color, $buffer);
314-
}
315349
}

0 commit comments

Comments
 (0)