diff --git a/src/Expectations/OppositeExpectation.php b/src/Expectations/OppositeExpectation.php index 397e6b8a9..9a8cf14a6 100644 --- a/src/Expectations/OppositeExpectation.php +++ b/src/Expectations/OppositeExpectation.php @@ -72,9 +72,25 @@ public function toUse(array|string $targets): ArchExpectation /** @var Expectation|string> $original */ $original = $this->original; - return GroupArchExpectation::fromExpectations($original, array_map(fn (string $target): SingleArchExpectation => ToUse::make($original, $target)->opposite( - fn (): never => $this->throwExpectationFailedException('toUse', $target), - ), is_string($targets) ? [$targets] : $targets)); + /** @var array $subjects */ + $subjects = is_string($original->value) ? [$original->value] : $original->value; + $dependencies = is_string($targets) ? [$targets] : $targets; + + /** @var array $expectations */ + $expectations = []; + + foreach ($subjects as $subject) { + $subjectExpectation = new Expectation($original->value); + $subjectExpectation->value = $subject; + + foreach ($dependencies as $dependency) { + $expectations[] = ToUse::make($subjectExpectation, $dependency)->opposite( + fn (): never => $this->throwExpectationFailedException('toUse', $dependency), + ); + } + } + + return GroupArchExpectation::fromExpectations($original, $expectations); } public function toHaveFileSystemPermissions(string $permissions): ArchExpectation diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 39bd3f9e4..261131f3f 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -435,6 +435,14 @@ PASS Tests\Features\Expect\not ✓ not property calls + PASS Tests\Features\Expect\notToUse + ✓ single target negation fails when the target uses the dependency + ✓ single target negation passes when the target does not use the dependency + ✓ multi target negation fails when any target uses the dependency + ✓ multi target negation passes when no target uses the dependency + ✓ multi target negation fails when any target uses any listed dependency + ✓ positive to use still fails when one target skips the dependency + PASS Tests\Features\Expect\pipes ✓ pipe is applied and can stop pipeline ✓ pipe is run and can let the pipeline keep going @@ -2321,4 +2329,4 @@ ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 37 skipped, 1648 passed (3649 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 37 skipped, 1654 passed (3673 assertions) diff --git a/tests/Features/Expect/notToUse.php b/tests/Features/Expect/notToUse.php new file mode 100644 index 000000000..9229ef99c --- /dev/null +++ b/tests/Features/Expect/notToUse.php @@ -0,0 +1,38 @@ +throws(ExpectationFailedException::class) + ->expect(UsesDependency::class) + ->not->toUse(Dependency::class); + +test('single target negation passes when the target does not use the dependency') + ->expect(Clean::class) + ->not->toUse(Dependency::class); + +test('multi target negation fails when any target uses the dependency') + ->throws(ExpectationFailedException::class) + ->expect([UsesDependency::class, Clean::class]) + ->not->toUse(Dependency::class); + +test('multi target negation passes when no target uses the dependency') + ->expect([Clean::class, AlsoClean::class]) + ->not->toUse(Dependency::class); + +test('multi target negation fails when any target uses any listed dependency') + ->throws(ExpectationFailedException::class) + ->expect([UsesDependency::class, Clean::class]) + ->not->toUse([Dependency::class, OtherDependency::class]); + +test('positive to use still fails when one target skips the dependency') + ->throws(ExpectationFailedException::class) + ->expect([UsesDependency::class, Clean::class]) + ->toUse(Dependency::class); diff --git a/tests/Fixtures/Arch/NotToUse/AlsoClean.php b/tests/Fixtures/Arch/NotToUse/AlsoClean.php new file mode 100644 index 000000000..0f903344a --- /dev/null +++ b/tests/Fixtures/Arch/NotToUse/AlsoClean.php @@ -0,0 +1,7 @@ +toContain("Tests: {$expected}")