feat: adjust overrides

This commit is contained in:
Nuno Maduro
2024-01-05 14:37:13 +00:00
parent f69a3cf832
commit 727a427837
6 changed files with 112 additions and 134 deletions

View File

@ -32,11 +32,19 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Runner\Filter;
use Exception;
use Pest\Contracts\HasPrintableTestCaseName;
use PHPUnit\Framework\SelfDescribing;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestSuite;
@ -44,30 +52,41 @@ use RecursiveFilterIterator;
use RecursiveIterator;
use function end;
use function implode;
use function preg_match;
use function sprintf;
use function str_replace;
/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class NameFilterIterator extends RecursiveFilterIterator
abstract class NameFilterIterator extends RecursiveFilterIterator
{
private ?string $filter = null;
private ?int $filterMin = null;
private ?int $filterMax = null;
/**
* @psalm-var non-empty-string
*/
private readonly string $regularExpression;
/**
* @throws Exception
* @psalm-var ?int
*/
private readonly ?int $dataSetMinimum;
/**
* @psalm-var ?int
*/
private readonly ?int $dataSetMaximum;
/**
* @psalm-param RecursiveIterator<int, Test> $iterator
* @psalm-param non-empty-string $filter
*/
public function __construct(RecursiveIterator $iterator, string $filter)
{
parent::__construct($iterator);
$this->setFilter($filter);
$preparedFilter = $this->prepareFilter($filter);
$this->regularExpression = $preparedFilter['regularExpression'];
$this->dataSetMinimum = $preparedFilter['dataSetMinimum'];
$this->dataSetMaximum = $preparedFilter['dataSetMaximum'];
}
public function accept(): bool
@ -78,95 +97,26 @@ final class NameFilterIterator extends RecursiveFilterIterator
return true;
}
$tmp = $this->describe($test);
if ($tmp[0] !== '') {
$name = implode('::', $tmp);
} else {
$name = $tmp[1];
if (! $test instanceof TestCase) {
return false;
}
$accepted = @preg_match($this->filter, $name, $matches);
$name = $test::class.'::'.$test->nameWithDataSet();
if ($accepted && isset($this->filterMax)) {
$accepted = @preg_match($this->regularExpression, $name, $matches) === 1;
if ($accepted && isset($this->dataSetMaximum)) {
$set = end($matches);
$accepted = $set >= $this->filterMin && $set <= $this->filterMax;
$accepted = $set >= $this->dataSetMinimum && $set <= $this->dataSetMaximum;
}
return (bool) $accepted;
return $accepted;
}
/**
* @throws Exception
* @psalm-param non-empty-string $filter
*
* @psalm-return array{regularExpression: non-empty-string, dataSetMinimum: ?int, dataSetMaximum: ?int}
*/
private function setFilter(string $filter): void
{
if (@preg_match($filter, '') === false) {
// Handles:
// * testAssertEqualsSucceeds#4
// * testAssertEqualsSucceeds#4-8
if (preg_match('/^(.*?)#(\d+)(?:-(\d+))?$/', $filter, $matches)) {
if (isset($matches[3]) && $matches[2] < $matches[3]) {
$filter = sprintf(
'%s.*with dataset #(\d+)$',
$matches[1]
);
$this->filterMin = (int) $matches[2];
$this->filterMax = (int) $matches[3];
} else {
$filter = sprintf(
'%s.*with dataset #%s$',
$matches[1],
$matches[2]
);
}
} // Handles:
// * testDetermineJsonError@JSON_ERROR_NONE
// * testDetermineJsonError@JSON.*
elseif (preg_match('/^(.*?)@(.+)$/', $filter, $matches)) {
$filter = sprintf(
'%s.*with dataset "%s"$',
$matches[1],
$matches[2]
);
}
// Escape delimiters in regular expression. Do NOT use preg_quote,
// to keep magic characters.
$filter = sprintf(
'/%s/i',
str_replace(
'/',
'\\/',
$filter
)
);
}
$this->filter = $filter;
}
/**
* @psalm-return array{0: string, 1: string}
*/
private function describe(Test $test): array
{
if ($test instanceof HasPrintableTestCaseName) {
return [
$test::getPrintableTestCaseName(),
$test->getPrintableTestCaseMethodName(),
];
}
if ($test instanceof TestCase) {
return [$test::class, $test->nameWithDataSet()];
}
if ($test instanceof SelfDescribing) {
return ['', $test->toString()];
}
return ['', $test::class];
}
abstract protected function prepareFilter(string $filter): array;
}

View File

@ -81,11 +81,6 @@ final class DefaultResultCache implements ResultCache
*/
private array $defects = [];
/**
* @psalm-var array<string, TestStatus>
*/
private array $currentDefects = [];
/**
* @psalm-var array<string, float>
*/
@ -102,10 +97,11 @@ final class DefaultResultCache implements ResultCache
public function setStatus(string $id, TestStatus $status): void
{
if ($status->isFailure() || $status->isError()) {
$this->currentDefects[$id] = $status;
$this->defects[$id] = $status;
if ($status->isSuccess()) {
return;
}
$this->defects[$id] = $status;
}
public function status(string $id): TestStatus
@ -115,10 +111,6 @@ final class DefaultResultCache implements ResultCache
public function setTime(string $id, float $time): void
{
if (! isset($this->currentDefects[$id])) {
unset($this->defects[$id]);
}
$this->times[$id] = $time;
}
@ -135,7 +127,7 @@ final class DefaultResultCache implements ResultCache
$data = json_decode(
file_get_contents($this->cacheFilename),
true
true,
);
if ($data === null) {
@ -183,7 +175,7 @@ final class DefaultResultCache implements ResultCache
file_put_contents(
$this->cacheFilename,
json_encode($data),
LOCK_EX
LOCK_EX,
);
}