mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
feat(presets): refactors code
This commit is contained in:
54
src/ArchPresets/AbstractPreset.php
Normal file
54
src/ArchPresets/AbstractPreset.php
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pest\ArchPresets;
|
||||||
|
|
||||||
|
use Pest\Arch\Contracts\ArchExpectation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
abstract class AbstractPreset
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 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 = [],
|
||||||
|
) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the arch preset.
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
abstract public function execute(): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ignores the given "targets" or "dependencies".
|
||||||
|
*
|
||||||
|
* @param array<int, string>|string $targetsOrDependencies
|
||||||
|
*/
|
||||||
|
final public function ignoring(array|string $targetsOrDependencies): void
|
||||||
|
{
|
||||||
|
$this->expectations = array_map(
|
||||||
|
fn (ArchExpectation $expectation): \Pest\Arch\Contracts\ArchExpectation => $expectation->ignoring($targetsOrDependencies),
|
||||||
|
$this->expectations,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flushes the expectations.
|
||||||
|
*/
|
||||||
|
final public function flush(): void
|
||||||
|
{
|
||||||
|
$this->expectations = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -4,24 +4,17 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest\ArchPresets;
|
namespace Pest\ArchPresets;
|
||||||
|
|
||||||
use Pest\Arch\Contracts\ArchExpectation;
|
|
||||||
use Pest\Contracts\ArchPreset;
|
|
||||||
use Pest\PendingCalls\TestCall;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
final class Base implements ArchPreset
|
final class Base extends AbstractPreset
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Boots the arch preset.
|
* Executes the arch preset.
|
||||||
*
|
|
||||||
* @param array<string> $baseNamespace
|
|
||||||
*/
|
*/
|
||||||
public function boot(TestCall $testCall, array $baseNamespace): TestCall|ArchExpectation
|
public function execute(): void
|
||||||
{
|
{
|
||||||
return $testCall // @phpstan-ignore-line
|
$this->expectations[] = expect(['dd', 'dump', 'ray', 'die', 'var_dump', 'sleep', 'eval', 'ini_set'])
|
||||||
->expect(['dd', 'dump', 'ray', 'die', 'var_dump', 'sleep', 'eval', 'ini_set'])
|
|
||||||
->not
|
->not
|
||||||
->toBeUsed();
|
->toBeUsed();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,25 +4,19 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest\ArchPresets;
|
namespace Pest\ArchPresets;
|
||||||
|
|
||||||
use Pest\Arch\Contracts\ArchExpectation;
|
|
||||||
use Pest\Contracts\ArchPreset;
|
|
||||||
use Pest\PendingCalls\TestCall;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
final class Strict implements ArchPreset
|
final class Strict extends AbstractPreset
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Boots the arch preset.
|
* Executes the arch preset.
|
||||||
*
|
|
||||||
* @param array<string> $baseNamespaces
|
|
||||||
*/
|
*/
|
||||||
public function boot(TestCall $testCall, array $baseNamespaces): TestCall|ArchExpectation
|
public function execute(): void
|
||||||
{
|
{
|
||||||
return $testCall // @phpstan-ignore-line
|
foreach ($this->userNamespaces as $namespace) {
|
||||||
->expect($baseNamespaces)
|
$this->expectations[] = expect($namespace)
|
||||||
->each
|
|
||||||
->toUseStrictTypes();
|
->toUseStrictTypes();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,18 +4,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest\Contracts;
|
namespace Pest\Contracts;
|
||||||
|
|
||||||
use Pest\Arch\Contracts\ArchExpectation;
|
|
||||||
use Pest\PendingCalls\TestCall;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
interface ArchPreset
|
interface ArchPreset
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Boots the arch preset.
|
|
||||||
*
|
|
||||||
* @param array<int, string> $baseNamespaces
|
|
||||||
*/
|
|
||||||
public function boot(TestCall $testCall, array $baseNamespaces): TestCall|ArchExpectation;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,9 +4,12 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pest;
|
namespace Pest;
|
||||||
|
|
||||||
use Pest\Arch\Contracts\ArchExpectation;
|
|
||||||
use Pest\Arch\Support\Composer;
|
use Pest\Arch\Support\Composer;
|
||||||
|
use Pest\ArchPresets\AbstractPreset;
|
||||||
|
use Pest\ArchPresets\Base;
|
||||||
|
use Pest\ArchPresets\Strict;
|
||||||
use Pest\PendingCalls\TestCall;
|
use Pest\PendingCalls\TestCall;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
@ -31,19 +34,44 @@ final class Preset
|
|||||||
/**
|
/**
|
||||||
* Uses the Pest base preset and returns the test call instance.
|
* Uses the Pest base preset and returns the test call instance.
|
||||||
*/
|
*/
|
||||||
public function base(): TestCall|ArchExpectation
|
public function base(): Base
|
||||||
{
|
{
|
||||||
return (new ArchPresets\Base)->boot($this->testCall, $this->baseNamespaces());
|
return $this->executePreset(new Base($this->baseNamespaces()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uses the Pest strict preset and returns the test call instance.
|
* Uses the Pest strict preset and returns the test call instance.
|
||||||
*/
|
*/
|
||||||
public function strict(): TestCall
|
public function strict(): Strict
|
||||||
{
|
{
|
||||||
(new ArchPresets\Strict)->boot($this->testCall, $this->baseNamespaces());
|
return $this->executePreset(new Strict($this->baseNamespaces()));
|
||||||
|
}
|
||||||
|
|
||||||
return $this->testCall;
|
/**
|
||||||
|
* Executes the given preset.
|
||||||
|
*
|
||||||
|
* @template TPreset of AbstractPreset
|
||||||
|
*
|
||||||
|
* @param TPreset $preset
|
||||||
|
* @return TPreset
|
||||||
|
*/
|
||||||
|
private function executePreset(AbstractPreset $preset): AbstractPreset
|
||||||
|
{
|
||||||
|
if ((fn (): ?string => $this->description)->call($this->testCall) === null) {
|
||||||
|
$description = strtolower((new \ReflectionClass($preset))->getShortName());
|
||||||
|
|
||||||
|
(fn (): string => $this->description = sprintf('arch "%s" preset', $description))->call($this->testCall);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->baseNamespaces();
|
||||||
|
|
||||||
|
$preset->execute();
|
||||||
|
|
||||||
|
$this->testCall->testCaseMethod->closure = (function () use ($preset): void {
|
||||||
|
$preset->flush();
|
||||||
|
})->bindTo(new stdClass);
|
||||||
|
|
||||||
|
return $preset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
PASS Tests\Arch
|
PASS Tests\Arch
|
||||||
✓ expect ['dd', 'dump', 'ray', …] → not → toBeUsed → ignoring 'Pest\Expectation'
|
✓ arch "base" preset
|
||||||
✓ expect ['Pest'] → each → toUseStrictTypes
|
✓ arch "strict" preset
|
||||||
✓ globals
|
✓ globals
|
||||||
✓ dependencies
|
✓ dependencies
|
||||||
✓ contracts
|
✓ contracts
|
||||||
@ -1268,7 +1268,7 @@
|
|||||||
✓ closure was bound to CustomTestCase
|
✓ closure was bound to CustomTestCase
|
||||||
|
|
||||||
PASS Tests\Playground
|
PASS Tests\Playground
|
||||||
✓ expect ['Pest'] → each → toUseStrictTypes
|
✓ basic
|
||||||
|
|
||||||
PASS Tests\Plugins\Traits
|
PASS Tests\Plugins\Traits
|
||||||
✓ it allows global uses
|
✓ it allows global uses
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
arch()->preset()->strict();
|
test('basic', function () {
|
||||||
|
expect(true)->toBeTrue();
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user