Skip to content

Commit cfb694d

Browse files
assertchrismikeerickson
authored andcommitted
Feature/remove class warnings (#75)
* Move Printer functionality into trait * Conditionally load and define classes for the appropriate PHPUnit version * Remove unused imports * Use correct ResultPrinter for 71
1 parent b46b2ac commit cfb694d

6 files changed

+397
-361
lines changed

src/Printer.php

Lines changed: 25 additions & 296 deletions
Original file line numberDiff line numberDiff line change
@@ -2,319 +2,48 @@
22

33
namespace Codedungeon\PHPUnitPrettyResultPrinter;
44

5-
use Noodlehaus\Config;
6-
use Codedungeon\PHPCliColors\Color;
5+
use PHPUnit_TextUI_ResultPrinter;
76

8-
include 'ResultPrinter5.php';
9-
include 'ResultPrinter6.php';
10-
include 'ResultPrinter70.php';
11-
include 'ResultPrinter71.php';
7+
if (class_exists(PHPUnit_TextUI_ResultPrinter::class)) {
8+
require __DIR__ . '/ResultPrinter5.php';
129

13-
class Printer extends _ResultPrinter
14-
{
15-
protected static $init = false;
16-
/**
17-
* @var string
18-
*/
19-
public $className = '';
20-
/**
21-
* @var string
22-
*/
23-
private $lastClassName = '';
24-
/**
25-
* @var int
26-
*/
27-
private $maxClassNameLength = 50;
28-
/**
29-
* @var int
30-
*/
31-
private $maxNumberOfColumns;
32-
/**
33-
* @var
34-
*/
35-
private $hideClassName;
36-
/**
37-
* @var
38-
*/
39-
private $simpleOutput;
40-
/**
41-
* @var Config
42-
*/
43-
private $configuration;
44-
/**
45-
* @var string
46-
*/
47-
private $configFileName;
48-
private $printerOptions;
49-
private $showConfig;
50-
private $passMark;
51-
private $failMark;
52-
53-
/**
54-
* {@inheritdoc}
55-
*/
56-
public function __construct(
57-
$out = null,
58-
$verbose = false,
59-
$colors = self::COLOR_DEFAULT,
60-
$debug = false,
61-
$numberOfColumns = 80
62-
) {
63-
parent::__construct($out, $verbose, $colors, $debug, $numberOfColumns);
64-
65-
$this->configFileName = $this->getConfigurationFile('phpunit-printer.yml');
66-
$this->colorsTool = new Color();
67-
$this->configuration = new Config($this->configFileName);
68-
69-
$this->maxNumberOfColumns = $this->getWidth();
70-
$this->maxClassNameLength = min((int) ($this->maxNumberOfColumns / 2), $this->maxClassNameLength);
71-
72-
// setup module options
73-
$this->printerOptions = $this->configuration->all();
74-
$this->hideClassName = $this->configuration->get('options.cd-printer-hide-class');
75-
$this->simpleOutput = $this->configuration->get('options.cd-printer-simple-output');
76-
$this->showConfig = $this->configuration->get('options.cd-printer-show-config');
77-
78-
$this->passMark = $this->configuration->get('marks.cd-pass');
79-
$this->failMark = $this->configuration->get('marks.cd-fail');
80-
81-
$this->init();
82-
}
83-
84-
/**
85-
* @param string $configFileName
86-
*
87-
* @return string
88-
*/
89-
public function getConfigurationFile($configFileName = 'phpunit-printer.yml')
90-
{
91-
$defaultConfigFilename = $this->getPackageRoot() . DIRECTORY_SEPARATOR . $configFileName;
92-
93-
$configPath = getcwd();
94-
$filename = '';
95-
96-
$continue = true;
97-
while (!file_exists($filename) && $continue) {
98-
$filename = $configPath . DIRECTORY_SEPARATOR . $configFileName;
99-
if (($this->isWindows() && strlen($configPath) === 3) || $configPath === '/') {
100-
$filename = $defaultConfigFilename;
101-
$continue = false;
102-
}
103-
$configPath = \dirname($configPath);
104-
}
105-
106-
return $filename;
107-
}
108-
109-
/**
110-
* @return string | returns package root
111-
*/
112-
private function getPackageRoot()
113-
{
114-
return \dirname(__FILE__, 2);
115-
}
116-
117-
/**
118-
* @return bool
119-
*/
120-
private function isWindows()
121-
{
122-
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
123-
}
124-
125-
/**
126-
* Gets the terminal width.
127-
*
128-
* @return int
129-
*/
130-
private function getWidth()
131-
{
132-
$width = 0;
133-
if ($this->isWindows()) {
134-
return 96; // create a default width to be used on windows
135-
}
136-
137-
exec('stty size 2>/dev/null', $out, $exit);
138-
139-
// 'stty size' output example: 36 120
140-
if (\count($out) > 0) {
141-
$width = (int) explode(' ', array_pop($out))[1];
142-
}
143-
144-
// handle CircleCI case (probably the same with TravisCI as well)
145-
if ($width === 0) {
146-
$width = 96;
147-
}
148-
149-
return $width;
150-
}
151-
152-
protected function init()
10+
class Printer extends ResultPrinter5
15311
{
154-
if (!self::$init) {
155-
$version = $this->version();
156-
$name = $this->packageName();
157-
echo PHP_EOL;
158-
echo $this->colorsTool->green() . "${name} ${version} by Codedungeon and contributors." . PHP_EOL;
159-
echo $this->colorsTool->reset();
160-
161-
if ($this->showConfig) {
162-
echo $this->colorsTool->white() . 'Configuration: ';
163-
echo $this->colorsTool->white() . $this->configFileName;
164-
echo $this->colorsTool->reset();
165-
echo PHP_EOL . PHP_EOL;
166-
}
167-
168-
self::$init = true;
169-
}
12+
use PrinterTrait;
17013
}
14+
}
17115

172-
public function version()
173-
{
174-
$content = file_get_contents($this->getPackageRoot() . DIRECTORY_SEPARATOR . 'composer.json');
175-
if ($content) {
176-
$content = json_decode($content, true);
177-
178-
return $content['version'];
179-
}
180-
181-
return 'n/a';
182-
}
183-
184-
/**
185-
* @return string
186-
*/
187-
public function packageName()
188-
{
189-
$content = file_get_contents($this->getPackageRoot() . DIRECTORY_SEPARATOR . 'composer.json');
190-
if ($content) {
191-
$content = json_decode($content, true);
192-
193-
return $content['description'];
194-
}
195-
196-
return 'n/a';
197-
}
16+
use PHPUnit\Runner\Version;
19817

199-
protected function writeProgressEx($progress)
200-
{
201-
if (!$this->debug) {
202-
$this->printClassName();
203-
}
204-
$this->printTestCaseStatus('', $progress);
205-
}
18+
if (version_compare(Version::series(), '6.99.99', '<=')) {
19+
require __DIR__ . '/ResultPrinter6.php';
20620

207-
/**
208-
* Prints the Class Name if it has changed.
209-
*/
210-
protected function printClassName()
21+
class Printer extends ResultPrinter6
21122
{
212-
if ($this->hideClassName) {
213-
return;
214-
}
215-
if ($this->lastClassName === $this->className) {
216-
return;
217-
}
218-
219-
echo PHP_EOL;
220-
$className = $this->formatClassName($this->className);
221-
$this->colorsTool ? $this->writeWithColor('fg-cyan,bold', $className, false) : $this->write($className);
222-
$this->column = \strlen($className) + 1;
223-
$this->lastClassName = $this->className;
23+
use PrinterTrait;
22424
}
25+
}
22526

226-
/**
227-
* @param string $className
228-
*
229-
* @return string
230-
*/
231-
private function formatClassName($className)
232-
{
233-
$prefix = ' ==> ';
234-
$ellipsis = '...';
235-
$suffix = ' ';
236-
$formattedClassName = $prefix . $className . $suffix;
237-
238-
if (\strlen($formattedClassName) <= $this->maxClassNameLength) {
239-
return $this->fillWithWhitespace($formattedClassName);
240-
}
27+
$low = version_compare(Version::series(), '7.0', '>=');
28+
$high = version_compare(Version::series(), '7.0.99', '<=');
24129

242-
// maxLength of class, minus leading (...) and trailing space
243-
$maxLength = $this->maxClassNameLength - \strlen($prefix . $ellipsis . $suffix);
30+
if ($low && $high) {
31+
require __DIR__ . '/ResultPrinter70.php';
24432

245-
// substring class name, providing space for ellipsis and one space at end
246-
// this result should be combined to equal $this->maxClassNameLength
247-
return $prefix . $ellipsis . substr($className, \strlen($className) - $maxLength, $maxLength) . $suffix;
248-
}
249-
250-
/**
251-
* @param string $className
252-
*
253-
* @return string;
254-
*/
255-
private function fillWithWhitespace($className)
33+
class Printer extends ResultPrinter70
25634
{
257-
return str_pad($className, $this->maxClassNameLength);
35+
use PrinterTrait;
25836
}
37+
}
25938

260-
/**
261-
* @param string $color
262-
* @param string $buffer Result of the Test Case => . F S I R
263-
*/
264-
private function printTestCaseStatus($color, $buffer)
265-
{
266-
if ($this->column >= $this->maxNumberOfColumns) {
267-
$this->writeNewLine();
268-
$padding = $this->maxClassNameLength;
269-
$this->column = $padding;
270-
echo str_pad(' ', $padding);
271-
}
272-
273-
switch (strtoupper($buffer)) {
274-
case '.':
275-
$color = 'fg-green,bold';
276-
$buffer = $this->simpleOutput ? '.' : mb_convert_encoding("\x27\x13", 'UTF-8', 'UTF-16BE');
277-
$buffer .= (!$this->debug) ? '' : ' Passed';
278-
break;
279-
case 'S':
280-
$color = 'fg-yellow,bold';
281-
$buffer = $this->simpleOutput ? 'S' : mb_convert_encoding("\x27\xA6", 'UTF-8', 'UTF-16BE');
282-
$buffer .= !$this->debug ? '' : ' Skipped';
283-
break;
284-
case 'I':
285-
$color = 'fg-blue,bold';
286-
$buffer = $this->simpleOutput ? 'I' : '';
287-
$buffer .= !$this->debug ? '' : ' Incomplete';
288-
break;
289-
case 'F':
290-
$color = 'fg-red,bold';
291-
$buffer = $this->simpleOutput ? 'F' : mb_convert_encoding("\x27\x16", 'UTF-8', 'UTF-16BE');
292-
$buffer .= (!$this->debug) ? '' : ' Fail';
293-
break;
294-
case 'E':
295-
$color = 'fg-red,bold';
296-
$buffer = $this->simpleOutput ? 'E' : '';
297-
$buffer .= !$this->debug ? '' : ' Error';
298-
break;
299-
}
39+
$low = version_compare(Version::series(), '7.1', '>=');
40+
$high = true; // version_compare(Version::series(),'7.1.99','<=');
30041

301-
$buffer .= ' ';
302-
echo parent::formatWithColor($color, $buffer);
303-
if ($this->debug) {
304-
$this->writeNewLine();
305-
}
306-
$this->column += 2;
307-
}
42+
if ($low && $high) {
43+
require __DIR__ . '/ResultPrinter71.php';
30844

309-
/**
310-
* {@inheritdoc}
311-
*/
312-
protected function writeProgressWithColorEx($color, $buffer)
45+
class Printer extends ResultPrinter71
31346
{
314-
if (!$this->debug) {
315-
$this->printClassName();
316-
}
317-
318-
$this->printTestCaseStatus($color, $buffer);
47+
use PrinterTrait;
31948
}
32049
}

0 commit comments

Comments
 (0)