Skip to content

Commit 097357a

Browse files
committed
chore: prepare for phpunit-9 support
1 parent d2edd5a commit 097357a

6 files changed

+245
-38
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ sandbox/
1515
scripts/
1616
*.taskpaper
1717
NOTES.md
18-
.phpunit.result.cache
18+
.phpunit.result.cache
19+
phpunit-printer.yml

README.md

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
1-
# CD PHPUnit Pretty Result Printer
2-
#### Version 0.26
1+
# CD PHPUnit Pretty Result Printer
32

3+
Version 0.26
44
Extend the default PHPUnit Result Printer with a modern, pretty printer!
55

6-
[PHPUnit Pretty Result Printer -- Packagist](https://packagist.org/packages/codedungeon/phpunit-result-printer)
6+
---
7+
8+
📦 [PHPUnit Pretty Result Printer -- Packagist](https://packagist.org/packages/codedungeon/phpunit-result-printer)
79

810
## Installation
911

1012
Installation is provided via composer and can be done with the following command, the current version requires PHP 7.1 or greater:
1113

1214
```bash
13-
$ composer require --dev codedungeon/phpunit-result-printer
15+
> composer require --dev codedungeon/phpunit-result-printer
1416
```
1517

1618
### Execute Initialization Script (Optional)
19+
1720
The following steps are optional, but will provide zero configuration for implementing **phpunit-pretty-result-printer**
1821

1922
- Adds `printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer"` to `phpunit.xml` file
2023
- Copies default `phpunit-printer.yml` to project root for easier customization
2124

22-
2325
```bash
24-
$ php ./vendor/codedungeon/phpunit-result-printer/src/init.php
26+
> php ./vendor/codedungeon/phpunit-result-printer/src/init.php
2527
```
2628

2729
#### Manual Configuration
30+
2831
Alternately, if you wish to configure **phpunit-pretty-result-printer** manually, you will need to update your `phpunit.xml` file as follows
2932

3033
```xml
@@ -37,58 +40,59 @@ Alternately, if you wish to configure **phpunit-pretty-result-printer** manually
3740
Or from Command-Line:
3841

3942
```bash
40-
$ phpunit --printer=Codedungeon\\PHPUnitPrettyResultPrinter\\Printer
43+
> phpunit --printer=Codedungeon\\PHPUnitPrettyResultPrinter\\Printer
4144
```
4245

4346
### Laravel 5.3 with PHP version 7.0.x
4447

4548
If you are using PHP 7.0.x, you will need to use a compatible version of PHPUnit Result Printer (version 0.8.x)
4649

4750
```bash
48-
$ composer require --dev codedungeon/phpunit-result-printer:^0.8
51+
> composer require --dev codedungeon/phpunit-result-printer:^0.8
4952
```
5053

5154
### AnyBar Integration
5255

53-
If you have AnyBar installed, it will be enabled by default. You can disable using `cd-printer-anybar-enabled` option (see below)
56+
If you have AnyBar installed, it will be enabled by default. You can disable using `cd-printer-anybar-enabled` option (see below)
5457

5558
[https://github.com/tonsky/AnyBar](https://github.com/tonsky/AnyBar)
5659

57-
**Anybar is off by default, thus you will need to set the `cd-printer-anybar` option in the `phpunit-printer.yml` to `true` if you wish to use Anybar.
60+
**Anybar is off by default, thus you will need to set the `cd-printer-anybar` option in the `phpunit-printer.yml` to `true` if you wish to use Anybar.
5861
This has been done to address issues with using CI tools such as travis (please see [Issue 122](https://github.com/mikeerickson/phpunit-pretty-result-printer/issues/122) for details) **
5962

60-
_NOTE: AnyBar is only available with PHPUnit 7.1 or greater.
61-
If you need support for previous versions, please let us know. We are slowly deprecating versions before 7.1._
63+
_NOTE: AnyBar is only available with PHPUnit 7.1 or greater.
64+
If you need support for previous versions, please let us know. We are slowly deprecating versions before 7.1._
6265

6366
### Configuration Options
6467

65-
* Create a `phpunit-printer.yml` file in your application root to override default (or anywhere use up the parent tree. It will search recursively up the tree until a configuration file is found. If not found, default configuration will be used).
66-
The following options are available (along with their default values):
68+
- Create a `phpunit-printer.yml` file in your application root to override default (or anywhere use up the parent tree. It will search recursively up the tree until a configuration file is found. If not found, default configuration will be used).
69+
The following options are available (along with their default values):
70+
71+
#### Options
6772

68-
#### Options ####
73+
| **Property Name** | **Default** | **Description** |
74+
| --------------------------- | ----------- | -------------------------------------------------------------------- |
75+
| `cd-printer-hide-class` | false | Hides the display of the test class name |
76+
| `cd-printer-simple-output` | false | Uses the default PHPUnit markers (but still uses Printer) |
77+
| `cd-printer-show-config` | true | Show path to used configuration file |
78+
| `cd-printer-hide-namespace` | true | Hide test class namespaces (will only show print class name) |
79+
| `cd-printer-anybar` | true | Enable AnyBar (if anybar is not installed, settings will be ignored) |
80+
| `cd-printer-anybar-port` | 1738 | Define AnyBar port number |
6981

70-
| **Property Name** | **Default** | **Description**
71-
| ------------------|-------------|----------------|
72-
| `cd-printer-hide-class` | false | Hides the display of the test class name
73-
| `cd-printer-simple-output`| false | Uses the default PHPUnit markers (but still uses Printer)
74-
| `cd-printer-show-config`| true | Show path to used configuration file
75-
| `cd-printer-hide-namespace`| true |Hide test class namespaces (will only show print class name)
76-
| `cd-printer-anybar`| true |Enable AnyBar (if anybar is not installed, settings will be ignored)
77-
| `cd-printer-anybar-port`| 1738 |Define AnyBar port number
82+
#### Markers
7883

79-
#### Markers ###
8084
You can customize the markers which are used for `success`, `fail`, `error`, `skipped`, `incomplete` by modifying the `phpunit-printer.yml` file.
8185

82-
| **Marker** | **Value** *
83-
|---------------|----------|
84-
| cd-pass | "✔ " |
85-
| cd-fail | "✖ " |
86-
| cd-error | "⚈ " |
87-
| cd-skipped | "→ " |
88-
| cd-incomplete | "∅ " |
89-
| cd-risky | "⌽ " |
86+
| **Marker** | **Value** \* |
87+
| ------------- | ------------ |
88+
| cd-pass | "✔ " |
89+
| cd-fail | "✖ " |
90+
| cd-error | "⚈ " |
91+
| cd-skipped | "→ " |
92+
| cd-incomplete | "∅ " |
93+
| cd-risky | "⌽ " |
9094

91-
_* Notice space after each marker. This makes the output a little more visually appealing, thus keep that in mind when creating your own custom markers_
95+
_\* Notice space after each marker. This makes the output a little more visually appealing, thus keep that in mind when creating your own custom markers_
9296

9397
## License
9498

phpunit-printer.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
options:
2+
cd-printer-hide-class: false
3+
cd-printer-simple-output: false
4+
cd-printer-show-config: true
5+
cd-printer-hide-namespace: true
6+
cd-printer-anybar: true
7+
cd-printer-anybar-port: 1738
8+
markers:
9+
cd-pass: ""
10+
cd-fail: ""
11+
cd-error: ""
12+
cd-skipped: "=> "
13+
cd-incomplete: ""
14+
cd-risky: ""

src/PrinterTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ trait PrinterTrait
7171
*/
7272
private $defaultMarkers = [];
7373

74+
private $anyBarEnabled = false;
75+
76+
private $anyBarPort = null;
77+
7478
/**
7579
* {@inheritdoc}
7680
*/

src/ResultPrinter80.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PHPUnit\TextUI\ResultPrinter;
1111
use PHPUnit\Framework\TestFailure;
1212

13-
$low = version_compare(Version::series(), '7.1', '>=');
13+
$low = version_compare(Version::series(), '8.0', '>=');
1414
$high = true; // version_compare(Version::series(),'7.1.99','<=');
1515

1616
if ($low && $high) {
@@ -83,7 +83,7 @@ protected function formatExceptionMsg($exceptionMessage): string
8383
return "$exceptionMessage";
8484
}
8585

86-
protected function printDefectTrace(TestFailure $defect):void
86+
protected function printDefectTrace(TestFailure $defect): void
8787
{
8888
$this->write($this->formatExceptionMsg($defect->getExceptionAsString()));
8989
$trace = Filter::getFilteredStacktrace(
@@ -96,8 +96,8 @@ protected function printDefectTrace(TestFailure $defect):void
9696
while ($exception) {
9797
$this->write(
9898
"\nCaused by\n" .
99-
TestFailure::exceptionToString($exception) . "\n" .
100-
Filter::getFilteredStacktrace($exception)
99+
TestFailure::exceptionToString($exception) . "\n" .
100+
Filter::getFilteredStacktrace($exception)
101101
);
102102
$exception = $exception->getPrevious();
103103
}

src/ResultPrinter90.php

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

0 commit comments

Comments
 (0)