diff --git a/src/PendingArchExpectation.php b/src/PendingArchExpectation.php index f09c603..207aa55 100644 --- a/src/PendingArchExpectation.php +++ b/src/PendingArchExpectation.php @@ -72,6 +72,36 @@ public function enums(): self return $this; } + /** + * Filters the given "targets" by only classes implementing the given interface. + */ + public function implementing(string $interface): self + { + $this->excludeCallbacks[] = fn (ObjectDescription $object): bool => ! in_array($interface, class_implements($object->name)); + + return $this; + } + + /** + * Filters the given "targets" by only classes extending the given class. + */ + public function extending(string $parentClass): self + { + $this->excludeCallbacks[] = fn (ObjectDescription $object): bool => ! is_subclass_of($object->name, $parentClass); + + return $this; + } + + /** + * Filters the given "targets" by only classes using the given trait. + */ + public function usingTrait(string $trait): self + { + $this->excludeCallbacks[] = fn (ObjectDescription $object): bool => ! in_array($trait, class_uses($object->name)); + + return $this; + } + /** * Creates an opposite expectation. */ diff --git a/tests/Fixtures/Controllers/UserController.php b/tests/Fixtures/Controllers/UserController.php index 75df8ed..c8e6f4f 100644 --- a/tests/Fixtures/Controllers/UserController.php +++ b/tests/Fixtures/Controllers/UserController.php @@ -4,9 +4,12 @@ use Tests\Fixtures\Contracts\Controllers\Indexable; use Tests\Fixtures\Controller; +use Tests\Fixtures\HasResponses; class UserController extends Controller implements Indexable { + use HasResponses; + public function index(): array { return [ diff --git a/tests/Fixtures/HasResponses.php b/tests/Fixtures/HasResponses.php new file mode 100644 index 0000000..e6f70a2 --- /dev/null +++ b/tests/Fixtures/HasResponses.php @@ -0,0 +1,5 @@ +extending(Controller::class) + ->toExtend(Controller::class); +}); + +test('only classes implementing interfaces are tested', function (): void { + expect('Tests\Fixtures\Controllers') + ->implementing(Indexable::class) + ->toImplement(Indexable::class); +}); + +test('only class using traits are tested', function (): void { + expect('Tests\Fixtures\Controllers') + ->usingTrait(HasResponses::class) + ->toUseTrait(HasResponses::class); +});