refactor: PHP 8 features

This commit is contained in:
Nuno Maduro
2021-10-24 18:29:59 +01:00
parent e8c2fe6e35
commit 2b687a7269
43 changed files with 283 additions and 635 deletions

View File

@ -16,7 +16,6 @@ use function class_exists;
use DOMDocument;
use DOMElement;
use Exception;
use function get_class;
use function method_exists;
use Pest\Concerns\Testable;
use PHPUnit\Framework\AssertionFailedError;
@ -42,65 +41,50 @@ use function trim;
*/
final class JUnit extends Printer implements TestListener
{
/**
* @var DOMDocument
*/
private $document;
private DOMDocument $document;
private DOMElement $root;
/**
* @var DOMElement
* @var array<int, DOMElement>
*/
private $root;
/**
* @var DOMElement[]
*/
private $testSuites = [];
private array $testSuites = [];
/**
* @var int[]
*/
private $testSuiteTests = [0];
private array $testSuiteTests = [0];
/**
* @var int[]
*/
private $testSuiteAssertions = [0];
private array $testSuiteAssertions = [0];
/**
* @var int[]
*/
private $testSuiteErrors = [0];
private array $testSuiteErrors = [0];
/**
* @var int[]
*/
private $testSuiteWarnings = [0];
private array $testSuiteWarnings = [0];
/**
* @var int[]
*/
private $testSuiteFailures = [0];
private array $testSuiteFailures = [0];
/**
* @var int[]
*/
private $testSuiteSkipped = [0];
private array $testSuiteSkipped = [0];
/**
* @var int[]|float[]
*/
private $testSuiteTimes = [0];
private array $testSuiteTimes = [0];
/**
* @var int
*/
private $testSuiteLevel = 0;
private int $testSuiteLevel = 0;
/**
* @var DOMElement|null
*/
private $currentTestCase;
private ?DOMElement $currentTestCase = null;
public function __construct(string $out)
{
@ -190,7 +174,7 @@ final class JUnit extends Printer implements TestListener
}
$testSuite->setAttribute('file', $fileName);
} catch (ReflectionException $e) {
} catch (ReflectionException) {
// @ignoreException
}
}
@ -313,7 +297,7 @@ final class JUnit extends Printer implements TestListener
$testCase->setAttribute('class', $test->getPrintableTestCaseName());
$testCase->setAttribute('classname', str_replace('\\', '.', $test->getPrintableTestCaseName()));
// @phpstan-ignore-next-line
$testCase->setAttribute('file', $test->__getFileName());
$testCase->setAttribute('file', $test->__getFilename());
}
$this->currentTestCase = $testCase;
@ -409,7 +393,7 @@ final class JUnit extends Printer implements TestListener
if ($t instanceof ExceptionWrapper) {
$fault->setAttribute('type', $t->getClassName());
} else {
$fault->setAttribute('type', get_class($t));
$fault->setAttribute('type', $t::class);
}
$this->currentTestCase->appendChild($fault);

View File

@ -16,9 +16,9 @@ use PHPUnit\Framework\TestResult;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Framework\Warning;
use PHPUnit\TextUI\DefaultResultPrinter;
use PHPUnit\TextUI\XmlConfiguration\Logging\TeamCity as BaseTeamCity;
use function round;
use function str_replace;
use function strlen;
use Throwable;
final class TeamCity extends DefaultResultPrinter
@ -34,22 +34,19 @@ final class TeamCity extends DefaultResultPrinter
private const TEST_STARTED = 'testStarted';
private const TEST_FINISHED = 'testFinished';
/** @var int */
private $flowId;
private ?int $flowId = null;
/** @var bool */
private $isSummaryTestCountPrinted = false;
private bool $isSummaryTestCountPrinted = false;
/** @var \PHPUnit\Util\Log\TeamCity */
private $phpunitTeamCity;
private BaseTeamCity $phpunitTeamCity;
/**
* @param resource|string|null $out
* Creates a new printer instance.
*/
public function __construct($out, bool $verbose, string $colors)
public function __construct(resource|string|null $out, bool $verbose, string $colors)
{
parent::__construct($out, $verbose, $colors);
$this->phpunitTeamCity = new \PHPUnit\Util\Log\TeamCity($out, $verbose, $colors);
$this->phpunitTeamCity = new BaseTeamCity($out, $verbose, $colors);
$this->logo();
}
@ -74,9 +71,7 @@ final class TeamCity extends DefaultResultPrinter
'passed' => ['count' => $this->successfulTestCount($result), 'color' => 'fg-green'],
];
$filteredResults = array_filter($results, function ($item): bool {
return $item['count'] > 0;
});
$filteredResults = array_filter($results, fn ($item): bool => $item['count'] > 0);
foreach ($filteredResults as $key => $info) {
$this->writeWithColor($info['color'], $info['count'] . " $key", false);
@ -203,7 +198,7 @@ final class TeamCity extends DefaultResultPrinter
*/
private static function isPestTestSuite(TestSuite $suite): bool
{
return strncmp($suite->getName(), 'P\\', strlen('P\\')) === 0;
return str_starts_with($suite->getName(), 'P\\');
}
/**