feat: custom presets

This commit is contained in:
Nuno Maduro
2024-09-04 20:53:33 +01:00
parent a7ca7afe4e
commit dd20323ca7
12 changed files with 158 additions and 28 deletions

View File

@ -15,7 +15,7 @@ abstract class AbstractPreset // @pest-arch-ignore-line
/**
* The expectations.
*
* @var array<int, ArchExpectation>
* @var array<int, Expectation<mixed>|ArchExpectation>
*/
protected array $expectations = [];
@ -24,7 +24,7 @@ abstract class AbstractPreset // @pest-arch-ignore-line
*
* @param array<int, string> $userNamespaces
*/
final public function __construct(
public function __construct(
private readonly array $userNamespaces,
) {
//
@ -45,7 +45,7 @@ abstract class AbstractPreset // @pest-arch-ignore-line
final public function ignoring(array|string $targetsOrDependencies): void
{
$this->expectations = array_map(
fn (ArchExpectation $expectation): \Pest\Arch\Contracts\ArchExpectation => $expectation->ignoring($targetsOrDependencies),
fn (ArchExpectation|Expectation $expectation): Expectation|ArchExpectation => $expectation instanceof ArchExpectation ? $expectation->ignoring($targetsOrDependencies) : $expectation,
$this->expectations,
);
}

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Pest\ArchPresets;
use Closure;
use Pest\Arch\Contracts\ArchExpectation;
use Pest\Expectation;
/**
* @internal
*/
final class Custom extends AbstractPreset
{
/**
* Creates a new preset instance.
*
* @param array<int, string> $userNamespaces
* @param Closure(array<int, string>): array<Expectation<mixed>|ArchExpectation> $execute
*/
public function __construct(
private readonly array $userNamespaces,
private readonly string $name,
private readonly Closure $execute,
) {
parent::__construct($userNamespaces);
}
/**
* Returns the name of the preset.
*/
public function name(): string
{
return $this->name;
}
/**
* Executes the arch preset.
*/
public function execute(): void
{
$this->expectations = ($this->execute)($this->userNamespaces);
}
}