Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add modifiers for filtering classes by trait, interface, and inheritance in arch tests #19

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions src/PendingArchExpectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/Fixtures/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixtures/HasResponses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Tests\Fixtures;

trait HasResponses {}
23 changes: 23 additions & 0 deletions tests/Modifiers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Tests\Fixtures\Contracts\Controllers\Indexable;
use Tests\Fixtures\Controller;
use Tests\Fixtures\HasResponses;

test('only classes extending Controller are tested', function (): void {
expect('Tests\Fixtures\Controllers')
->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)
yondifon marked this conversation as resolved.
Show resolved Hide resolved
->toUseTrait(HasResponses::class);
});