Skip to content

Commit 64c7560

Browse files
committed
printer example is now compatible PHPUnit 8.5 and 9.x
1 parent 28bf31c commit 64c7560

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

examples/bootstrap.printer.php

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

3-
$baseDir = dirname(__DIR__);
4-
$vendorDir = $baseDir . '/vendor';
5-
$extraDir = $baseDir . '/extra';
6-
7-
require_once $vendorDir . '/autoload.php';
3+
require_once dirname(__DIR__) . '/vendor/autoload.php';
84

95
use Bartlett\LoggerTestListenerTrait;
106

7+
use PHPUnit\Framework\TestListener;
8+
use PHPUnit\Framework\TestResult;
9+
use PHPUnit\Runner\Version;
10+
use PHPUnit\Util\Getopt as GetOptUtil;
11+
use PHPUnit\Util\Printer;
12+
1113
use Psr\Log\LoggerAwareTrait;
1214
use Psr\Log\AbstractLogger;
1315
use Psr\Log\LogLevel;
1416

15-
use PHPUnit\Framework\TestListener;
16-
use PHPUnit\Util\Getopt as GetOptUtil;
17-
use PHPUnit\Util\Printer as PrinterUtil;
17+
use SebastianBergmann\CliParser\Parser as CliParser;
1818

1919
/**
2020
* Helper to detect phpunit switches, due to lack of implementation in custom printer classes
2121
* @see https://github.com/sebastianbergmann/phpunit/issues/1674
2222
*/
2323
trait GetOpt
2424
{
25-
public function isVerbose()
25+
public function isVerbose(): bool
2626
{
27-
list ($opts, $non_opts) = GetOptUtil::getopt(
28-
$_SERVER['argv'],
29-
'd:c:hv'
30-
);
27+
list ($opts, $non_opts) = $this->parseCliArguments();
28+
3129
$key = array_search('--verbose', $non_opts);
3230
if ($key === false) {
3331
foreach ($opts as $opt) {
@@ -41,15 +39,23 @@ public function isVerbose()
4139
return is_int($key);
4240
}
4341

44-
public function isDebug()
42+
public function isDebug(): bool
4543
{
46-
list ($opts, $non_opts) = GetOptUtil::getopt(
47-
$_SERVER['argv'],
48-
'd:c:hv'
49-
);
44+
list ($opts, $non_opts) = $this->parseCliArguments();
45+
5046
$key = array_search('--debug', $non_opts);
5147
return is_int($key);
5248
}
49+
50+
private function parseCliArguments(): array
51+
{
52+
$parameters = $_SERVER['argv'];
53+
$shortOptions = 'd:c:hv';
54+
if (version_compare(Version::id(), '9.3.8', 'ge')) {
55+
return (new CliParser)->parse($parameters, $shortOptions);
56+
}
57+
return GetOptUtil::parse($parameters, $shortOptions);
58+
}
5359
}
5460

5561
class Psr3ConsoleLogger extends AbstractLogger
@@ -96,12 +102,10 @@ public function log($level, $message, array $context = array())
96102
}
97103
}
98104

99-
class ResultPrinter extends PrinterUtil implements TestListener
105+
abstract class BaseResultPrinter extends Printer
100106
{
101107
use LoggerTestListenerTrait, LoggerAwareTrait, GetOpt;
102108

103-
protected $numAssertions = 0;
104-
105109
/**
106110
* {@inheritDoc}
107111
*/
@@ -116,6 +120,24 @@ public function __construct($out = null)
116120
} else {
117121
$level = LogLevel::NOTICE;
118122
}
119-
$this->setLogger(new \Psr3ConsoleLogger('PHPUnitPrinterLogger', $level));
123+
$this->setLogger(new Psr3ConsoleLogger('PHPUnitPrinterLogger', $level));
124+
}
125+
}
126+
127+
/**
128+
* Since PHPUnit 9.0.0, PHPUnit\TextUI\ResultPrinter became an interface
129+
* [#4024](https://github.com/sebastianbergmann/phpunit/issues/4024)
130+
*/
131+
if (version_compare(Version::id(), '9.0.0', 'ge')) {
132+
final class ResultPrinter extends BaseResultPrinter implements \PHPUnit\TextUI\ResultPrinter
133+
{
134+
public function printResult(TestResult $result): void
135+
{
136+
// none implementation to avoid default printing behavior
137+
}
138+
}
139+
} else {
140+
final class ResultPrinter extends BaseResultPrinter implements TestListener
141+
{
120142
}
121143
}

0 commit comments

Comments
 (0)