Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/Expectations/OppositeExpectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,9 +78,24 @@ public function toUse(array|string $targets): ArchExpectation
/** @var Expectation<array<int, string>|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<array<int, string>|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);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion tests/.snapshots/success.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Tests: 1 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1546 passed (3387 assertions)
22 changes: 22 additions & 0 deletions tests/Features/Expect/toUse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;
use Tests\Fixtures\Arch\ToUse\CleanClass;
use Tests\Fixtures\Arch\ToUse\Dependencies\Dependency;
use Tests\Fixtures\Arch\ToUse\UsesDependency;

test('single target fails when it uses the dependency', function (): void {
expect(UsesDependency::class)->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);
});
7 changes: 7 additions & 0 deletions tests/Fixtures/Arch/ToUse/CleanClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToUse;

final class CleanClass {}
7 changes: 7 additions & 0 deletions tests/Fixtures/Arch/ToUse/Dependencies/Dependency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToUse\Dependencies;

final class Dependency {}
15 changes: 15 additions & 0 deletions tests/Fixtures/Arch/ToUse/UsesDependency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToUse;

use Tests\Fixtures\Arch\ToUse\Dependencies\Dependency;

final class UsesDependency
{
public function make(): Dependency
{
return new Dependency;
}
}
4 changes: 2 additions & 2 deletions tests/Visual/Parallel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
$file = file_get_contents(__FILE__);
$file = preg_replace(
'/\$expected = \'.*?\';/',
"\$expected = '1 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1525 passed (3322 assertions)';",
"\$expected = '1 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1529 passed (3334 assertions)';",
$file,
);
file_put_contents(__FILE__, $file);
}

$expected = '1 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1525 passed (3322 assertions)';
$expected = '1 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1529 passed (3334 assertions)';

expect($output)
->toContain("Tests: {$expected}")
Expand Down