From 79a192ed0bd43754b9c5962bdc28a98ba673922e Mon Sep 17 00:00:00 2001 From: Malico Date: Thu, 17 Oct 2024 12:45:22 +0100 Subject: [PATCH 1/2] Add modifiers for filtering classes by trait, interface, and inheritance in arch tests --- src/PendingArchExpectation.php | 30 +++++++++++++++++++ tests/Fixtures/Controllers/UserController.php | 3 ++ tests/Fixtures/HasResponses.php | 5 ++++ tests/Modifiers.php | 23 ++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 tests/Fixtures/HasResponses.php create mode 100644 tests/Modifiers.php 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(Tests\Fixtures\HasResponses::class) + ->toUseTrait(HasResponses::class); +}); From 2c489e064d7fd3af0d03850498097c27c0b6782f Mon Sep 17 00:00:00 2001 From: ndifon Date: Thu, 17 Oct 2024 17:53:11 +0100 Subject: [PATCH 2/2] clean up --- tests/Modifiers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Modifiers.php b/tests/Modifiers.php index f8a0c9c..a062cc0 100644 --- a/tests/Modifiers.php +++ b/tests/Modifiers.php @@ -18,6 +18,6 @@ test('only class using traits are tested', function (): void { expect('Tests\Fixtures\Controllers') - ->usingTrait(Tests\Fixtures\HasResponses::class) + ->usingTrait(HasResponses::class) ->toUseTrait(HasResponses::class); });