From c0d9f739b30a8b4af5a8bf0725df4da9969e0af1 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Mon, 12 Dec 2022 13:44:39 +0000 Subject: [PATCH 01/12] chore: fixes script name --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ace8c975..425ed877 100644 --- a/composer.json +++ b/composer.json @@ -77,7 +77,7 @@ "test:integration": "php bin/pest --colors=always --group=integration -v", "update:snapshots": "REBUILD_SNAPSHOTS=true php bin/pest --colors=always", "test": [ - "@rest:refacto", + "@test:refacto", "@test:lint", "@test:types", "@test:unit", From 68bf8a2d26a71d9f3e0a5a9080082a73d02cc88f Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 13 Dec 2022 15:27:22 +0000 Subject: [PATCH 02/12] feat: adds arch related expectations --- composer.json | 1 + src/Expectation.php | 45 ++++++++++++++++++++++-- src/Expectations/OppositeExpectation.php | 44 ++++++++++++++++++++++- src/Mixins/Expectation.php | 4 +++ 4 files changed, 91 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 425ed877..320b58f1 100644 --- a/composer.json +++ b/composer.json @@ -51,6 +51,7 @@ }, "require-dev": { "pestphp/pest-dev-tools": "^2.1.0", + "pestphp/pest-plugin-arch": "^2.0.0", "symfony/process": "^6.2.0" }, "minimum-stability": "dev", diff --git a/src/Expectation.php b/src/Expectation.php index 2da24e75..21e2ee0e 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -6,6 +6,10 @@ namespace Pest; use BadMethodCallException; use Closure; +use Pest\Arch\ArchExpectation; +use Pest\Arch\Expectations\ToDependOn; +use Pest\Arch\Expectations\ToDependOnNothing; +use Pest\Arch\Expectations\ToOnlyDependOn; use Pest\Concerns\Extendable; use Pest\Concerns\Pipeable; use Pest\Concerns\Retrievable; @@ -24,7 +28,7 @@ use PHPUnit\Framework\ExpectationFailedException; * * @template TValue * - * @property Expectation $not Creates the opposite expectation. + * @property OppositeExpectation $not Creates the opposite expectation. * @property EachExpectation $each Creates an expectation on each element on the traversable value. * * @mixin Mixins\Expectation @@ -286,11 +290,15 @@ final class Expectation return new HigherOrderExpectation($this, call_user_func_array($this->value->$method(...), $parameters)); } - ExpectationPipeline::for($this->getExpectationClosure($method)) + $result = ExpectationPipeline::for($this->getExpectationClosure($method)) ->send(...$parameters) ->through($this->pipes($method, $this, Expectation::class)) ->run(); + if ($result !== null) { + return $result; + } + return $this; } @@ -350,4 +358,37 @@ final class Expectation { return new Any(); } + + /** + * Asserts that the layer depends (not exclusively) on the given layers. + * + * @param array|string $targets + * @return ArchExpectation + */ + public function toDependOn(array|string $targets): ArchExpectation + { + return ToDependOn::make($this, $targets); + } + + /** + * Asserts that the layer only depends on the given layers. + * + * @param array|string $targets + * @return ArchExpectation + */ + public function toOnlyDependOn(array|string $targets): ArchExpectation + { + return ToOnlyDependOn::make($this, $targets); + } + + /** + * Asserts that the layer is not allowed to depend on any other layer. + * + * @param array|string $targets + * @return ArchExpectation + */ + public function toDependOnNothing(): ArchExpectation + { + return ToDependOnNothing::make($this); + } } diff --git a/src/Expectations/OppositeExpectation.php b/src/Expectations/OppositeExpectation.php index 9a8fefbe..4780a8a2 100644 --- a/src/Expectations/OppositeExpectation.php +++ b/src/Expectations/OppositeExpectation.php @@ -4,6 +4,10 @@ declare(strict_types=1); namespace Pest\Expectations; +use Pest\Arch\ArchExpectation; +use Pest\Arch\Expectations\ToDependOn; +use Pest\Arch\Expectations\ToDependOnNothing; +use Pest\Arch\Expectations\ToOnlyDependOn; use Pest\Expectation; use Pest\Support\Arr; use PHPUnit\Framework\ExpectationFailedException; @@ -52,6 +56,44 @@ final class OppositeExpectation return $this->original; } + /** + * Asserts that the layer does not depend on the given layers. + * + * @param array|string $targets + * @return ArchExpectation + */ + public function toDependOn(array|string $targets): ArchExpectation + { + return ToDependOn::make($this->original, $targets)->opposite( + fn () => $this->throwExpectationFailedException('toDependOn', $targets), + ); + } + + /** + * Asserts that the layer does not only depends on the given layers. + * + * @param array|string $targets + * @return ArchExpectation + */ + public function toOnlyDependOn(array|string $targets): ArchExpectation + { + return ToOnlyDependOn::make($this->original, $targets)->opposite( + fn () => $this->throwExpectationFailedException('toOnlyDependOn', $targets), + ); + } + + /** + * Asserts that the layer is depends on at least one layer. + * + * @return ArchExpectation + */ + public function toDependOnNothing(): ArchExpectation + { + return ToDependOnNothing::make($this->original)->opposite( + fn () => $this->throwExpectationFailedException('toDependOnNothing'), + ); + } + /** * Handle dynamic method calls into the original expectation. * @@ -91,7 +133,7 @@ final class OppositeExpectation * * @param array $arguments */ - private function throwExpectationFailedException(string $name, array $arguments = []): never + public function throwExpectationFailedException(string $name, array $arguments = []): never { $exporter = new Exporter(); diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 886ffab2..3c444591 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -8,6 +8,10 @@ use BadMethodCallException; use Closure; use Error; use InvalidArgumentException; +use Pest\Arch\ArchExpectation; +use Pest\Arch\Expectations\ToDependOn; +use Pest\Arch\Expectations\ToDependOnNothing; +use Pest\Arch\Expectations\ToOnlyDependOn; use Pest\Exceptions\InvalidExpectationValue; use Pest\Matchers\Any; use Pest\Support\Arr; From e1e926076af9e2792efdfb53dcbe708ed0527755 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 14 Dec 2022 14:56:11 +0000 Subject: [PATCH 03/12] fix: types on arch expectations --- src/Expectation.php | 13 ++++--------- src/Expectations/OppositeExpectation.php | 12 +++++++----- src/Mixins/Expectation.php | 4 ---- src/Repositories/DatasetsRepository.php | 3 +-- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index 21e2ee0e..2277d95a 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -290,15 +290,11 @@ final class Expectation return new HigherOrderExpectation($this, call_user_func_array($this->value->$method(...), $parameters)); } - $result = ExpectationPipeline::for($this->getExpectationClosure($method)) + ExpectationPipeline::for($this->getExpectationClosure($method)) ->send(...$parameters) ->through($this->pipes($method, $this, Expectation::class)) ->run(); - if ($result !== null) { - return $result; - } - return $this; } @@ -363,7 +359,7 @@ final class Expectation * Asserts that the layer depends (not exclusively) on the given layers. * * @param array|string $targets - * @return ArchExpectation + * @return ArchExpectation */ public function toDependOn(array|string $targets): ArchExpectation { @@ -374,7 +370,7 @@ final class Expectation * Asserts that the layer only depends on the given layers. * * @param array|string $targets - * @return ArchExpectation + * @return ArchExpectation */ public function toOnlyDependOn(array|string $targets): ArchExpectation { @@ -384,8 +380,7 @@ final class Expectation /** * Asserts that the layer is not allowed to depend on any other layer. * - * @param array|string $targets - * @return ArchExpectation + * @return ArchExpectation */ public function toDependOnNothing(): ArchExpectation { diff --git a/src/Expectations/OppositeExpectation.php b/src/Expectations/OppositeExpectation.php index 4780a8a2..a59b13b1 100644 --- a/src/Expectations/OppositeExpectation.php +++ b/src/Expectations/OppositeExpectation.php @@ -60,7 +60,7 @@ final class OppositeExpectation * Asserts that the layer does not depend on the given layers. * * @param array|string $targets - * @return ArchExpectation + * @return ArchExpectation */ public function toDependOn(array|string $targets): ArchExpectation { @@ -73,7 +73,7 @@ final class OppositeExpectation * Asserts that the layer does not only depends on the given layers. * * @param array|string $targets - * @return ArchExpectation + * @return ArchExpectation */ public function toOnlyDependOn(array|string $targets): ArchExpectation { @@ -85,7 +85,7 @@ final class OppositeExpectation /** * Asserts that the layer is depends on at least one layer. * - * @return ArchExpectation + * @return ArchExpectation */ public function toDependOnNothing(): ArchExpectation { @@ -131,10 +131,12 @@ final class OppositeExpectation /** * Creates a new expectation failed exception with a nice readable message. * - * @param array $arguments + * @param array|string $arguments */ - public function throwExpectationFailedException(string $name, array $arguments = []): never + public function throwExpectationFailedException(string $name, array|string $arguments = []): never { + $arguments = is_array($arguments) ? $arguments : [$arguments]; + $exporter = new Exporter(); $toString = fn ($argument): string => $exporter->shortenedExport($argument); diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 3c444591..886ffab2 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -8,10 +8,6 @@ use BadMethodCallException; use Closure; use Error; use InvalidArgumentException; -use Pest\Arch\ArchExpectation; -use Pest\Arch\Expectations\ToDependOn; -use Pest\Arch\Expectations\ToDependOnNothing; -use Pest\Arch\Expectations\ToOnlyDependOn; use Pest\Exceptions\InvalidExpectationValue; use Pest\Matchers\Any; use Pest\Support\Arr; diff --git a/src/Repositories/DatasetsRepository.php b/src/Repositories/DatasetsRepository.php index 83eddac2..47f710ef 100644 --- a/src/Repositories/DatasetsRepository.php +++ b/src/Repositories/DatasetsRepository.php @@ -158,11 +158,10 @@ final class DatasetsRepository $datasets[$index] = iterator_to_array($datasets[$index], false); } - // @phpstan-ignore-next-line foreach ($datasets[$index] as $key => $values) { $values = is_array($values) ? $values : [$values]; $processedDataset[] = [ - 'label' => self::getDatasetDescription($key, $values), // @phpstan-ignore-line + 'label' => self::getDatasetDescription($key, $values), 'values' => $values, ]; } From 6d7fd66f829e18c88856ece5ae4eff311608302d Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 14 Dec 2022 14:56:26 +0000 Subject: [PATCH 04/12] tests: on opposite `throwExpectationFailedException` --- tests/Unit/Expectations/OppositeExpectation.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 tests/Unit/Expectations/OppositeExpectation.php diff --git a/tests/Unit/Expectations/OppositeExpectation.php b/tests/Unit/Expectations/OppositeExpectation.php new file mode 100644 index 00000000..448ebf6d --- /dev/null +++ b/tests/Unit/Expectations/OppositeExpectation.php @@ -0,0 +1,16 @@ +throwExpectationFailedException('toBe', 'bar'); +})->throws(ExpectationFailedException::class, "Expecting 'foo' not to be 'bar'."); + +it('throw expectation failed exception with array argument', function (): void { + $expectation = new OppositeExpectation(expect('foo')); + + $expectation->throwExpectationFailedException('toBe', ['bar']); +})->throws(ExpectationFailedException::class, "Expecting 'foo' not to be 'bar'."); From 3911cfec6df111b07c92b15ab825d41d8125cb81 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 14 Dec 2022 14:56:31 +0000 Subject: [PATCH 05/12] chore: updates snapshots --- tests/.snapshots/success.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index e47760bb..3c3a11d6 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -815,6 +815,10 @@ ✓ it show the actual dataset of multiple non-named datasets in their description ✓ it show the correct description for mixed named and not-named datasets + PASS Tests\Unit\Expectations\OppositeExpectation + ✓ it throw expectation failed exception with string argument + ✓ it throw expectation failed exception with array argument + PASS Tests\Unit\Plugins\Environment ✓ environment is set to CI when --ci option is used ✓ environment is set to Local when --ci option is not used @@ -890,4 +894,4 @@ PASS Tests\Visual\Version ✓ visual snapshot of help command output - Tests: 4 incomplete, 2 todos, 18 skipped, 619 passed (1529 assertions) \ No newline at end of file + Tests: 4 incomplete, 2 todos, 18 skipped, 621 passed (1533 assertions) \ No newline at end of file From 1c9c408cf386f18933a55b07c484d115486f7aae Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 15 Dec 2022 02:10:47 +0000 Subject: [PATCH 06/12] fix: style of memory plugin --- src/Plugins/Memory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugins/Memory.php b/src/Plugins/Memory.php index 5d29960e..0755f743 100644 --- a/src/Plugins/Memory.php +++ b/src/Plugins/Memory.php @@ -46,7 +46,7 @@ final class Memory implements AddsOutput, HandlesArguments { if ($this->enabled) { $this->output->writeln(sprintf( - ' Memory: %s MB', + ' Memory: %s MB', round(memory_get_usage(true) / 1000 ** 2, 3) )); } From 8aece3981dc4666857b588b20b5a7788a5d66932 Mon Sep 17 00:00:00 2001 From: Samuel Mwangi Date: Sat, 17 Dec 2022 22:06:21 +0300 Subject: [PATCH 07/12] Tests Badge Apply changes to the badge as per https://github.com/badges/shields/issues/8671 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4febffd5..151869b7 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@

PEST

- GitHub Workflow Status (master) + GitHub Workflow Status (master) Total Downloads Latest Version License From 8d018ea3f16eff9aaacd5765654ae25a3ee50b70 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 20 Dec 2022 22:42:09 +0000 Subject: [PATCH 08/12] chore: bumps termwind --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 320b58f1..9646056f 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "require": { "php": "^8.1.0", "nunomaduro/collision": "^7.0.0", - "nunomaduro/termwind": "^1.14.2", + "nunomaduro/termwind": "^1.15", "pestphp/pest-plugin": "^2.0.0", "phpunit/phpunit": "10.0.x-dev" }, From 9596274b14ae914e1100a67c5e5b3b914512fe27 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 20 Dec 2022 22:42:29 +0000 Subject: [PATCH 09/12] fix: test suite loader duplicating tests --- overrides/Runner/TestSuiteLoader.php | 14 ++++---------- src/Concerns/Retrievable.php | 1 - tests/.snapshots/success.txt | 5 ++++- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/overrides/Runner/TestSuiteLoader.php b/overrides/Runner/TestSuiteLoader.php index 0719288b..6e9df439 100644 --- a/overrides/Runner/TestSuiteLoader.php +++ b/overrides/Runner/TestSuiteLoader.php @@ -98,14 +98,14 @@ final class TestSuiteLoader self::$loadedClasses = array_merge($loadedClasses, self::$loadedClasses); - if (empty(self::$loadedClasses)) { + if (empty($loadedClasses)) { return $this->exceptionFor($suiteClassName, $suiteClassFile); } $testCaseFound = false; - foreach (self::$loadedClasses as $loadedClass) { - if (is_subclass_of($loadedClass, HasPrintableTestCaseName::class)) { + foreach (array_reverse($loadedClasses) as $loadedClass) { + if (is_subclass_of($loadedClass, HasPrintableTestCaseName::class) || is_subclass_of($loadedClass, TestCase::class)) { $suiteClassName = $loadedClass; $testCaseFound = true; @@ -115,13 +115,7 @@ final class TestSuiteLoader } if (! $testCaseFound) { - foreach (self::$loadedClasses as $loadedClass) { - if (is_subclass_of($loadedClass, TestCase::class)) { - $suiteClassName = $loadedClass; - - break; - } - } + return $this->exceptionFor($suiteClassName, $suiteClassFile); } if (! class_exists($suiteClassName, false)) { diff --git a/src/Concerns/Retrievable.php b/src/Concerns/Retrievable.php index a2716887..a95031b1 100644 --- a/src/Concerns/Retrievable.php +++ b/src/Concerns/Retrievable.php @@ -13,7 +13,6 @@ trait Retrievable * @template TRetrievableValue * * Safely retrieve the value at the given key from an object or array. - * * @template TRetrievableValue * * @param array|object $value diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 3c3a11d6..4df70c77 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -779,6 +779,9 @@ PASS Tests\PHPUnit\CustomAffixes\snakecasespec ✓ it runs file names like `snake_case_spec.php` + PASS Tests\CustomTestCase\ExecutedTest + ✓ that gets executed + PASS Tests\PHPUnit\CustomTestCase\UsesPerDirectory ✓ closure was bound to CustomTestCase @@ -894,4 +897,4 @@ PASS Tests\Visual\Version ✓ visual snapshot of help command output - Tests: 4 incomplete, 2 todos, 18 skipped, 621 passed (1533 assertions) \ No newline at end of file + Tests: 4 incomplete, 2 todos, 18 skipped, 622 passed (1509 assertions) \ No newline at end of file From b04207d9eaa1cfc5b92460cacc596f81487a5355 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 21 Dec 2022 00:09:38 +0000 Subject: [PATCH 10/12] feat: improves `not->toDependOn` --- src/Exceptions/InvalidExpectation.php | 25 ++++++++++++++++++++ src/Expectation.php | 6 +---- src/Expectations/OppositeExpectation.php | 29 +++++++++++------------- 3 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 src/Exceptions/InvalidExpectation.php diff --git a/src/Exceptions/InvalidExpectation.php b/src/Exceptions/InvalidExpectation.php new file mode 100644 index 00000000..c13cf1ff --- /dev/null +++ b/src/Exceptions/InvalidExpectation.php @@ -0,0 +1,25 @@ + $methods + * @throws self + */ + public static function fromMethods(array $methods): never + { + throw new self(sprintf('Expectation [%s] is not valid.', implode('->', $methods))); + } +} diff --git a/src/Expectation.php b/src/Expectation.php index 2277d95a..95b9b53f 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -6,7 +6,7 @@ namespace Pest; use BadMethodCallException; use Closure; -use Pest\Arch\ArchExpectation; +use Pest\Arch\Conctracts\ArchExpectation; use Pest\Arch\Expectations\ToDependOn; use Pest\Arch\Expectations\ToDependOnNothing; use Pest\Arch\Expectations\ToOnlyDependOn; @@ -359,7 +359,6 @@ final class Expectation * Asserts that the layer depends (not exclusively) on the given layers. * * @param array|string $targets - * @return ArchExpectation */ public function toDependOn(array|string $targets): ArchExpectation { @@ -370,7 +369,6 @@ final class Expectation * Asserts that the layer only depends on the given layers. * * @param array|string $targets - * @return ArchExpectation */ public function toOnlyDependOn(array|string $targets): ArchExpectation { @@ -379,8 +377,6 @@ final class Expectation /** * Asserts that the layer is not allowed to depend on any other layer. - * - * @return ArchExpectation */ public function toDependOnNothing(): ArchExpectation { diff --git a/src/Expectations/OppositeExpectation.php b/src/Expectations/OppositeExpectation.php index a59b13b1..c45ced4e 100644 --- a/src/Expectations/OppositeExpectation.php +++ b/src/Expectations/OppositeExpectation.php @@ -4,10 +4,13 @@ declare(strict_types=1); namespace Pest\Expectations; -use Pest\Arch\ArchExpectation; +use Pest\Arch\Contracts\ArchExpectation; use Pest\Arch\Expectations\ToDependOn; use Pest\Arch\Expectations\ToDependOnNothing; use Pest\Arch\Expectations\ToOnlyDependOn; +use Pest\Arch\GroupArchExpectation; +use Pest\Arch\SingleArchExpectation; +use Pest\Exceptions\InvalidExpectation; use Pest\Expectation; use Pest\Support\Arr; use PHPUnit\Framework\ExpectationFailedException; @@ -60,38 +63,32 @@ final class OppositeExpectation * Asserts that the layer does not depend on the given layers. * * @param array|string $targets - * @return ArchExpectation */ public function toDependOn(array|string $targets): ArchExpectation { - return ToDependOn::make($this->original, $targets)->opposite( - fn () => $this->throwExpectationFailedException('toDependOn', $targets), - ); + return GroupArchExpectation::fromExpectations(array_map(function (string $target) : SingleArchExpectation { + return ToDependOn::make($this->original, $target)->opposite( + fn () => $this->throwExpectationFailedException('toDependOn', $target), + ); + }, is_string($targets) ? [$targets] : $targets)); } /** * Asserts that the layer does not only depends on the given layers. * * @param array|string $targets - * @return ArchExpectation */ - public function toOnlyDependOn(array|string $targets): ArchExpectation + public function toOnlyDependOn(array|string $targets): never { - return ToOnlyDependOn::make($this->original, $targets)->opposite( - fn () => $this->throwExpectationFailedException('toOnlyDependOn', $targets), - ); + throw InvalidExpectation::fromMethods(['not', 'toOnlyDependOn']); } /** * Asserts that the layer is depends on at least one layer. - * - * @return ArchExpectation */ - public function toDependOnNothing(): ArchExpectation + public function toDependOnNothing(): never { - return ToDependOnNothing::make($this->original)->opposite( - fn () => $this->throwExpectationFailedException('toDependOnNothing'), - ); + throw InvalidExpectation::fromMethods(['not', 'toDependOnNothing']); } /** From 522504916b63db21376ce19df135aa708f4cccee Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 21 Dec 2022 00:11:55 +0000 Subject: [PATCH 11/12] fix: namespace import --- src/Expectation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Expectation.php b/src/Expectation.php index 95b9b53f..8283a870 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -6,7 +6,7 @@ namespace Pest; use BadMethodCallException; use Closure; -use Pest\Arch\Conctracts\ArchExpectation; +use Pest\Arch\Contracts\ArchExpectation; use Pest\Arch\Expectations\ToDependOn; use Pest\Arch\Expectations\ToDependOnNothing; use Pest\Arch\Expectations\ToOnlyDependOn; From 138bdf599b6cba1f6b2143179dead6e066ff9f58 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 21 Dec 2022 04:00:45 +0000 Subject: [PATCH 12/12] feat: adds `toOnlyBeUsedOn` --- src/Exceptions/InvalidExpectation.php | 3 +- src/Expectation.php | 37 +++++++++++++++--------- src/Expectations/OppositeExpectation.php | 32 +++++++++++--------- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/Exceptions/InvalidExpectation.php b/src/Exceptions/InvalidExpectation.php index c13cf1ff..544baac3 100644 --- a/src/Exceptions/InvalidExpectation.php +++ b/src/Exceptions/InvalidExpectation.php @@ -12,10 +12,11 @@ use Symfony\Component\Console\Exception\ExceptionInterface; /** * @internal */ -final class InvalidExpectation extends LogicException implements ExceptionInterface, RenderlessEditor, RenderlessTrace +final class InvalidExpectation extends LogicException implements ExceptionInterface, RenderlessEditor, RenderlessTrace { /** * @param array $methods + * * @throws self */ public static function fromMethods(array $methods): never diff --git a/src/Expectation.php b/src/Expectation.php index 8283a870..507e6915 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -9,6 +9,7 @@ use Closure; use Pest\Arch\Contracts\ArchExpectation; use Pest\Arch\Expectations\ToDependOn; use Pest\Arch\Expectations\ToDependOnNothing; +use Pest\Arch\Expectations\ToOnlyBeUsedOn; use Pest\Arch\Expectations\ToOnlyDependOn; use Pest\Concerns\Extendable; use Pest\Concerns\Pipeable; @@ -356,17 +357,35 @@ final class Expectation } /** - * Asserts that the layer depends (not exclusively) on the given layers. + * Asserts that the given expectation target depends on the given dependencies. * - * @param array|string $targets + * @param array|string $dependencies */ - public function toDependOn(array|string $targets): ArchExpectation + public function toDependOn(array|string $dependencies): ArchExpectation { - return ToDependOn::make($this, $targets); + return ToDependOn::make($this, $dependencies); } /** - * Asserts that the layer only depends on the given layers. + * Asserts that the given expectation target does not have any dependencies. + */ + public function toDependOnNothing(): ArchExpectation + { + return ToDependOnNothing::make($this); + } + + /** + * Asserts that the given expectation dependency is only depended on by the given targets. + * + * @param array|string $targets + */ + public function toOnlyBeUsedOn(array|string $targets): ArchExpectation + { + return ToOnlyBeUsedOn::make($this, $targets); + } + + /** + * Asserts that the given expectation target does "only" depend on the given dependencies. * * @param array|string $targets */ @@ -374,12 +393,4 @@ final class Expectation { return ToOnlyDependOn::make($this, $targets); } - - /** - * Asserts that the layer is not allowed to depend on any other layer. - */ - public function toDependOnNothing(): ArchExpectation - { - return ToDependOnNothing::make($this); - } } diff --git a/src/Expectations/OppositeExpectation.php b/src/Expectations/OppositeExpectation.php index c45ced4e..f73b3345 100644 --- a/src/Expectations/OppositeExpectation.php +++ b/src/Expectations/OppositeExpectation.php @@ -6,8 +6,6 @@ namespace Pest\Expectations; use Pest\Arch\Contracts\ArchExpectation; use Pest\Arch\Expectations\ToDependOn; -use Pest\Arch\Expectations\ToDependOnNothing; -use Pest\Arch\Expectations\ToOnlyDependOn; use Pest\Arch\GroupArchExpectation; use Pest\Arch\SingleArchExpectation; use Pest\Exceptions\InvalidExpectation; @@ -60,31 +58,39 @@ final class OppositeExpectation } /** - * Asserts that the layer does not depend on the given layers. + * Asserts that the given expectation target depends on the given dependencies. * - * @param array|string $targets + * @param array|string $dependencies */ - public function toDependOn(array|string $targets): ArchExpectation + public function toDependOn(array|string $dependencies): ArchExpectation { - return GroupArchExpectation::fromExpectations(array_map(function (string $target) : SingleArchExpectation { - return ToDependOn::make($this->original, $target)->opposite( - fn () => $this->throwExpectationFailedException('toDependOn', $target), - ); - }, is_string($targets) ? [$targets] : $targets)); + return GroupArchExpectation::fromExpectations(array_map(fn (string $target): SingleArchExpectation => ToDependOn::make($this->original, $target)->opposite( + fn () => $this->throwExpectationFailedException('toDependOn', $target), + ), is_string($dependencies) ? [$dependencies] : $dependencies)); } /** - * Asserts that the layer does not only depends on the given layers. + * Asserts that the given expectation dependency is only depended on by the given targets. * * @param array|string $targets */ - public function toOnlyDependOn(array|string $targets): never + public function toOnlyBeUsedOn(array|string $targets): never + { + throw InvalidExpectation::fromMethods(['not', 'toOnlyBeUsedOn']); + } + + /** + * Asserts that the given expectation target does "only" depend on the given dependencies. + * + * @param array|string $dependencies + */ + public function toOnlyDependOn(array|string $dependencies): never { throw InvalidExpectation::fromMethods(['not', 'toOnlyDependOn']); } /** - * Asserts that the layer is depends on at least one layer. + * Asserts that the given expectation target does not have any dependencies. */ public function toDependOnNothing(): never {