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']); } /**