feat(presets): reworks code

This commit is contained in:
Nuno Maduro
2024-08-07 11:08:29 +01:00
parent 17058d1709
commit 9353015691
10 changed files with 80 additions and 27 deletions

View File

@ -5,21 +5,27 @@ declare(strict_types=1);
namespace Pest\ArchPresets;
use Pest\Arch\Contracts\ArchExpectation;
use Pest\Expectation;
/**
* @internal
*/
abstract class AbstractPreset
{
/**
* The expectations.
*
* @var array<int, ArchExpectation>
*/
protected array $expectations = [];
/**
* Creates a new preset instance.
*
* @param array<int, string> $userNamespaces
* @param array<int, ArchExpectation> $expectations
*/
final public function __construct(// @phpstan-ignore-line
protected array $userNamespaces,
protected array $expectations = [],
final public function __construct(
private readonly array $userNamespaces,
) {
//
}
@ -44,6 +50,20 @@ abstract class AbstractPreset
);
}
/**
* Runs the given callback for each namespace.
*
* @param callable(Expectation<string|null>): ArchExpectation ...$callbacks
*/
final public function eachUserNamespace(callable ...$callbacks): void
{
foreach ($this->userNamespaces as $namespace) {
foreach ($callbacks as $callback) {
$this->expectations[] = $callback(expect($namespace));
}
}
}
/**
* Flushes the expectations.
*/