From 60b1e63c23f1e9eb070a2113b2ec8e6b1b2edd1c Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Sat, 8 Jun 2024 20:54:46 +0100 Subject: [PATCH] feat: initial work on presets --- src/ArchPresets/Base.php | 28 +++++++++++++++++ src/ArchPresets/Strict.php | 28 +++++++++++++++++ src/Contracts/ArchPreset.php | 21 +++++++++++++ src/PendingCalls/TestCall.php | 9 ++++++ src/Preset.php | 58 +++++++++++++++++++++++++++++++++++ tests/.snapshots/success.txt | 6 ++-- tests/Arch.php | 6 ++++ tests/Playground.php | 4 +-- tests/Visual/Parallel.php | 2 +- 9 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 src/ArchPresets/Base.php create mode 100644 src/ArchPresets/Strict.php create mode 100644 src/Contracts/ArchPreset.php create mode 100644 src/Preset.php diff --git a/src/ArchPresets/Base.php b/src/ArchPresets/Base.php new file mode 100644 index 00000000..2ee5d5a9 --- /dev/null +++ b/src/ArchPresets/Base.php @@ -0,0 +1,28 @@ + $baseNamespace + */ + public function boot(TestCall $testCall, array $baseNamespace): TestCall|ArchExpectation + { + return $testCall + ->expect(['dd', 'dump', 'ray', 'die', 'var_dump', 'sleep']) + ->not + ->toBeUsed(); + } +} diff --git a/src/ArchPresets/Strict.php b/src/ArchPresets/Strict.php new file mode 100644 index 00000000..c4fbe196 --- /dev/null +++ b/src/ArchPresets/Strict.php @@ -0,0 +1,28 @@ + $baseNamespaces + */ + public function boot(TestCall $testCall, array $baseNamespaces): TestCall|ArchExpectation + { + return $testCall + ->expect($baseNamespaces) + ->each + ->toUseStrictTypes(); + } +} diff --git a/src/Contracts/ArchPreset.php b/src/Contracts/ArchPreset.php new file mode 100644 index 00000000..24a250d3 --- /dev/null +++ b/src/Contracts/ArchPreset.php @@ -0,0 +1,21 @@ + $baseNamespaces + */ + public function boot(TestCall $testCall, array $baseNamespaces): TestCall|ArchExpectation; +} diff --git a/src/PendingCalls/TestCall.php b/src/PendingCalls/TestCall.php index 5422347f..fb8f72c8 100644 --- a/src/PendingCalls/TestCall.php +++ b/src/PendingCalls/TestCall.php @@ -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); + } } diff --git a/src/Preset.php b/src/Preset.php new file mode 100644 index 00000000..de01bf88 --- /dev/null +++ b/src/Preset.php @@ -0,0 +1,58 @@ +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; + } +} diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 6a99b1d9..b00c7c9e 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1,5 +1,7 @@ PASS Tests\Arch + ✓ expect ['dd', 'dump', 'ray', …] → not → toBeUsed → ignoring 'Pest\Expectation' + ✓ expect ['Pest'] → each → toUseStrictTypes ✓ globals ✓ dependencies ✓ contracts @@ -1266,7 +1268,7 @@ ✓ closure was bound to CustomTestCase PASS Tests\Playground - ✓ basic + ✓ expect ['Pest'] → each → toUseStrictTypes PASS Tests\Plugins\Traits ✓ it allows global uses @@ -1461,4 +1463,4 @@ WARN Tests\Visual\Version - visual snapshot of help command output - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 24 skipped, 1042 passed (2563 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 24 skipped, 1044 passed (2565 assertions) \ No newline at end of file diff --git a/tests/Arch.php b/tests/Arch.php index 7c508cf2..e63b998c 100644 --- a/tests/Arch.php +++ b/tests/Arch.php @@ -2,6 +2,10 @@ use Pest\Expectation; +arch()->preset()->base()->ignoring(Expectation::class); + +arch()->preset()->strict(); + arch('globals') ->expect(['dd', 'dump', 'ray', 'die', 'var_dump', 'sleep']) ->not->toBeUsed() @@ -30,4 +34,6 @@ arch('contracts') 'NunoMaduro\Collision\Contracts', 'Pest\Factories\TestCaseMethodFactory', 'Symfony\Component\Console', + 'Pest\Arch\Contracts', + 'Pest\PendingCalls', ])->toBeInterfaces(); diff --git a/tests/Playground.php b/tests/Playground.php index be30ae7d..dbc1a3ae 100644 --- a/tests/Playground.php +++ b/tests/Playground.php @@ -1,5 +1,3 @@ toBeTrue(); -}); +arch()->preset()->strict(); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 28f83d63..cdc756c4 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -16,7 +16,7 @@ $run = function () { test('parallel', function () use ($run) { expect($run('--exclude-group=integration')) - ->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 1028 passed (2531 assertions)') + ->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 1030 passed (2533 assertions)') ->toContain('Parallel: 3 processes'); })->skipOnWindows();