diff --git a/src/Expectations/OppositeExpectation.php b/src/Expectations/OppositeExpectation.php index d1693cb68..408d89ddb 100644 --- a/src/Expectations/OppositeExpectation.php +++ b/src/Expectations/OppositeExpectation.php @@ -12,7 +12,6 @@ use Pest\Arch\Expectations\ToUse; use Pest\Arch\GroupArchExpectation; use Pest\Arch\PendingArchExpectation; -use Pest\Arch\SingleArchExpectation; use Pest\Arch\Support\FileLineFinder; use Pest\Exceptions\InvalidExpectation; use Pest\Exceptions\MissingDependency; @@ -79,9 +78,24 @@ 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 () => $this->throwExpectationFailedException('toUse', $target), - ), is_string($targets) ? [$targets] : $targets)); + $targets = is_string($targets) ? [$targets] : $targets; + + $values = is_array($original->value) ? $original->value : [$original->value]; + + $expectations = []; + + foreach ($values as $value) { + /** @var Expectation|string> $expectation */ + $expectation = new Expectation($value); + + foreach ($targets as $target) { + $expectations[] = ToUse::make($expectation, $target)->opposite( + fn () => $this->throwExpectationFailedException('toUse', $target), + ); + } + } + + return GroupArchExpectation::fromExpectations($original, $expectations); } /** diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index f6f24f941..39cd71fbe 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -1146,6 +1146,12 @@ ✓ it can handle a non-defined exception ✓ it can handle a class not found Error + PASS Tests\Features\Expect\toUse + ✓ single target fails when it uses the dependency + ✓ multi target fails when any target uses the dependency + ✓ multi target fails regardless of the violating target position + ✓ multi target passes when no target uses the dependency + PASS Tests\Features\Expect\toUseStrictEquality ✓ missing strict equality ✓ has strict equality @@ -2186,4 +2192,4 @@ ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') - Tests: 1 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1542 passed (3375 assertions) \ No newline at end of file + Tests: 1 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1546 passed (3387 assertions) \ No newline at end of file diff --git a/tests/Features/Expect/toUse.php b/tests/Features/Expect/toUse.php new file mode 100644 index 000000000..b9f450ece --- /dev/null +++ b/tests/Features/Expect/toUse.php @@ -0,0 +1,22 @@ +not->toUse(Dependency::class); +})->throws(ExpectationFailedException::class); + +test('multi target fails when any target uses the dependency', function (): void { + expect([UsesDependency::class, CleanClass::class])->not->toUse(Dependency::class); +})->throws(ExpectationFailedException::class); + +test('multi target fails regardless of the violating target position', function (): void { + expect([CleanClass::class, UsesDependency::class])->not->toUse(Dependency::class); +})->throws(ExpectationFailedException::class); + +test('multi target passes when no target uses the dependency', function (): void { + expect([CleanClass::class])->not->toUse(Dependency::class); +}); diff --git a/tests/Fixtures/Arch/ToUse/CleanClass.php b/tests/Fixtures/Arch/ToUse/CleanClass.php new file mode 100644 index 000000000..c402e6ae9 --- /dev/null +++ b/tests/Fixtures/Arch/ToUse/CleanClass.php @@ -0,0 +1,7 @@ +toContain("Tests: {$expected}")