Adds initial implementation of --dirty option.

This commit is contained in:
Luke Downing
2022-12-05 08:59:42 +00:00
parent d77715b0fe
commit d29d68a2c2
5 changed files with 109 additions and 29 deletions

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pest\Repositories;
use Closure;
use Pest\Contracts\TestCaseFilter;
use Pest\Exceptions\TestCaseAlreadyInUse;
use Pest\Exceptions\TestCaseClassOrTraitNotFound;
use Pest\Factories\TestCaseFactory;
@ -27,6 +28,13 @@ final class TestRepository
*/
private array $uses = [];
/**
* @param array<int, TestCaseFilter> $testCaseFilters
*/
public function __construct(private readonly array $testCaseFilters = [])
{
}
/**
* Counts the number of test cases.
*/
@ -42,22 +50,22 @@ final class TestRepository
*/
public function getFilenames(): array
{
$testCases = array_filter($this->testCases, static fn (TestCaseFactory $testCase) => $testCase->methodsUsingOnly() !== []);
$testCases = array_filter($this->testCases, static fn(TestCaseFactory $testCase) => $testCase->methodsUsingOnly() !== []);
if ($testCases === []) {
$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
{
@ -97,7 +105,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);
}
@ -109,7 +117,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),
true,
);
if ($canLoad) {
$this->make($this->testCases[$filename]);
}
}
@ -119,12 +137,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)) {