feat: initial work on presets

This commit is contained in:
Nuno Maduro
2024-06-08 20:54:46 +01:00
parent c7bcb6eb7b
commit 60b1e63c23
9 changed files with 156 additions and 6 deletions

28
src/ArchPresets/Base.php Normal file
View File

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Pest\ArchPresets;
use Pest\Arch\Contracts\ArchExpectation;
use Pest\Contracts\ArchPreset;
use Pest\PendingCalls\TestCall;
/**
* @internal
*/
final class Base implements ArchPreset
{
/**
* Boots the arch preset.
*
* @param array<string> $baseNamespace
*/
public function boot(TestCall $testCall, array $baseNamespace): TestCall|ArchExpectation
{
return $testCall
->expect(['dd', 'dump', 'ray', 'die', 'var_dump', 'sleep'])
->not
->toBeUsed();
}
}

View File

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Pest\ArchPresets;
use Pest\Arch\Contracts\ArchExpectation;
use Pest\Contracts\ArchPreset;
use Pest\PendingCalls\TestCall;
/**
* @internal
*/
final class Strict implements ArchPreset
{
/**
* Boots the arch preset.
*
* @param array<string> $baseNamespaces
*/
public function boot(TestCall $testCall, array $baseNamespaces): TestCall|ArchExpectation
{
return $testCall
->expect($baseNamespaces)
->each
->toUseStrictTypes();
}
}

View File

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Pest\Contracts;
use Pest\Arch\Contracts\ArchExpectation;
use Pest\PendingCalls\TestCall;
/**
* @internal
*/
interface ArchPreset
{
/**
* Boots the arch preset.
*
* @param array<int, string> $baseNamespaces
*/
public function boot(TestCall $testCall, array $baseNamespaces): TestCall|ArchExpectation;
}

View File

@ -12,6 +12,7 @@ use Pest\Factories\TestCaseMethodFactory;
use Pest\Mutate\Decorators\TestCallDecorator as MutationTestCallDecorator;
use Pest\PendingCalls\Concerns\Describable;
use Pest\Plugins\Only;
use Pest\Preset;
use Pest\Support\Backtrace;
use Pest\Support\Exporter;
use Pest\Support\HigherOrderCallables;
@ -521,4 +522,12 @@ final class TestCall
$testCase->attributes = array_merge($testCase->attributes, $this->testCaseFactoryAttributes);
}
}
/**
* Uses the given preset on the test.
*/
public function preset(): Preset
{
return new Preset($this);
}
}

58
src/Preset.php Normal file
View File

@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace Pest;
use Pest\Arch\Contracts\ArchExpectation;
use Pest\Arch\Support\Composer;
use Pest\PendingCalls\TestCall;
/**
* @internal
*/
final class Preset
{
/**
* The application / package base namespaces.
*/
private static ?array $baseNamespaces = null;
/**
* Creates a new preset instance.
*/
public function __construct(private readonly TestCall $testCall)
{
//
}
/**
* Uses the Pest base preset and returns the test call instance.
*/
public function base(): TestCall|ArchExpectation
{
return (new ArchPresets\Base)->boot($this->testCall, $this->baseNamespaces());
}
/**
* Uses the Pest strict preset and returns the test call instance.
*/
public function strict(): TestCall
{
(new ArchPresets\Strict)->boot($this->testCall, $this->baseNamespaces());
return $this->testCall;
}
/**
* Get the base namespaces for the application / package.
*/
private function baseNamespaces(): array
{
if (self::$baseNamespaces === null) {
self::$baseNamespaces = Composer::userNamespaces();
}
return self::$baseNamespaces;
}
}