feat: rewrites --dirty support

This commit is contained in:
Nuno Maduro
2023-01-10 22:23:52 +00:00
parent f6676118ac
commit 15931e2418
6 changed files with 102 additions and 84 deletions

View File

@ -29,11 +29,9 @@ final class TestRepository
private array $uses = [];
/**
* @param array<int, TestCaseFilter> $testCaseFilters
* @var array<int, TestCaseFilter>
*/
public function __construct(private readonly array $testCaseFilters = [])
{
}
private array $filters = [];
/**
* Counts the number of test cases.
@ -56,16 +54,16 @@ final class TestRepository
$testCases = $this->testCases;
}
return array_values(array_map(static fn(TestCaseFactory $factory): string => $factory->filename, $testCases));
return array_values(array_map(static fn (TestCaseFactory $factory): string => $factory->filename, $testCases));
}
/**
* Uses the given `$testCaseClass` on the given `$paths`.
*
* @param array<int, string> $classOrTraits
* @param array<int, string> $groups
* @param array<int, string> $paths
* @param array<int, Closure> $hooks
* @param array<int, string> $classOrTraits
* @param array<int, string> $groups
* @param array<int, string> $paths
* @param array<int, Closure> $hooks
*/
public function use(array $classOrTraits, array $groups, array $paths, array $hooks): void
{
@ -92,6 +90,14 @@ final class TestRepository
}
}
/**
* Filters the test cases using the given filter.
*/
public function filter(TestCaseFilter $filter): void
{
$this->filters[] = $filter;
}
/**
* Gets the test case factory from the given filename.
*/
@ -105,7 +111,7 @@ final class TestRepository
*/
public function set(TestCaseMethodFactory $method): void
{
if (!array_key_exists($method->filename, $this->testCases)) {
if (! array_key_exists($method->filename, $this->testCases)) {
$this->testCases[$method->filename] = new TestCaseFactory($method->filename);
}
@ -117,17 +123,17 @@ final class TestRepository
*/
public function makeIfNeeded(string $filename): void
{
if (!array_key_exists($filename, $this->testCases)) {
if (! array_key_exists($filename, $this->testCases)) {
return;
}
$canLoad = array_reduce(
$this->testCaseFilters,
fn(bool $carry, TestCaseFilter $filter): bool => $carry && $filter->canLoad($filename),
$accepted = array_reduce(
$this->filters,
fn (bool $carry, TestCaseFilter $filter): bool => $carry && $filter->accept($filename),
true,
);
if ($canLoad) {
if ($accepted) {
$this->make($this->testCases[$filename]);
}
}
@ -137,12 +143,12 @@ final class TestRepository
*/
private function make(TestCaseFactory $testCase): void
{
$startsWith = static fn(string $target, string $directory): bool => Str::startsWith($target, $directory . DIRECTORY_SEPARATOR);
$startsWith = static fn (string $target, string $directory): bool => Str::startsWith($target, $directory.DIRECTORY_SEPARATOR);
foreach ($this->uses as $path => $uses) {
[$classOrTraits, $groups, $hooks] = $uses;
if ((!is_dir($path) && $testCase->filename === $path) || (is_dir($path) && $startsWith($testCase->filename, $path))) {
if ((! is_dir($path) && $testCase->filename === $path) || (is_dir($path) && $startsWith($testCase->filename, $path))) {
foreach ($classOrTraits as $class) {
/** @var string $class */
if (class_exists($class)) {