Architecture Testing

Architecture testing enables you to specify expectations that test whether your application adheres to a set of architectural rules, helping you maintain a clean and sustainable codebase. The expectations are determined by either Relative namespaces, fully qualified namespaces, or function names.

Expectations

toBeAbstract()

The toBeAbstract() method may be used to ensure that all classes within a given namespace are abstract.

1arch('app')
2 ->expect('App\Models')
3 ->toBeAbstract();

toBeClasses()

The toBeClasses() method may be used to ensure that all files within a given namespace are classes.

1arch('app')
2 ->expect('App\Models')
3 ->toBeClasses();

toBeEnums()

The toBeEnums() method may be used to ensure that all files within a given namespace are enums.

1arch('app')
2 ->expect('App\Enums')
3 ->toBeEnums();

toBeIntBackedEnums()

The toBeIntBackedEnums() method may be used to ensure that all enums within a specified namespace are int-backed.

1arch('app')
2 ->expect('App\Enums')
3 ->toBeIntBackedEnums();

toBeInterfaces()

The toBeInterfaces() method may be used to ensure that all files within a given namespace are interfaces.

1arch('app')
2 ->expect('App\Contracts')
3 ->toBeInterfaces();

toBeInvokable()

The toBeInvokable() method may be used to ensure that all files within a given namespace are invokable.

1arch('app')
2 ->expect('App\Actions')
3 ->toBeInvokable();

toBeTraits()

The toBeTraits() method may be used to ensure that all files within a given namespace are traits.

1arch('app')
2 ->expect('App\Concerns')
3 ->toBeTraits();

toBeFinal()

The toBeFinal() method may be used to ensure that all classes within a given namespace are final.

1arch('app')
2 ->expect('App\ValueObjects')
3 ->toBeFinal();

Note that, typically this expectation is used in combination with the classes() modifier to ensure that all classes within a given namespace are final.

1arch('app')
2 ->expect('App')
3 ->classes()
4 ->toBeFinal();

toBeReadonly()

The toBeReadonly() method may be used to ensure that certain classes are immutable and cannot be modified at runtime.

1arch('app')
2 ->expect('App\ValueObjects')
3 ->toBeReadonly();

Note that, typically this expectation is used in combination with the classes() modifier to ensure that all classes within a given namespace are readonly.

1arch('app')
2 ->expect('App')
3 ->classes()
4 ->toBeReadonly();

toBeStringBackedEnums()

The toBeStringBackedEnums() method may be used to ensure that all enums within a specified namespace are string-backed.

1arch('app')
2 ->expect('App\Enums')
3 ->toBeStringBackedEnums();

toBeUsed()

The not modifier, when combined with the toBeUsed() method, enables you to verify that certain classes or functions are not being utilized by your application.

1arch('globals')
2 ->expect(['dd', 'dump'])
3 ->not->toBeUsed();
4 
5arch('facades')
6 ->expect('Illuminate\Support\Facades')
7 ->not->toBeUsed();

toBeUsedIn()

By combining the not modifier with the toBeUsedIn() method, you can restrict specific classes and functions from being used within a given namespace.

1arch('globals')
2 ->expect('request')
3 ->not->toBeUsedIn('App\Domain');
4 
5arch('globals')
6 ->expect('Illuminate\Http')
7 ->not->toBeUsedIn('App\Domain');

toExtend()

The toExtend() method may be used to ensure that all classes within a given namespace extend a specific class.

1arch('app')
2 ->expect('App\Models')
3 ->toExtend('Illuminate\Database\Eloquent\Model');

toExtendNothing()

The toExtendNothing() method may be used to ensure that all classes within a given namespace do not extend any class.

1arch('app')
2 ->expect('App\ValueObjects')
3 ->toExtendNothing();

toImplement()

The toImplement() method may be used to ensure that all classes within a given namespace implement a specific interface.

1arch('app')
2 ->expect('App\Jobs')
3 ->toImplement('Illuminate\Contracts\Queue\ShouldQueue');

toImplementNothing()

The toImplementNothing() method may be used to ensure that all classes within a given namespace do not implement any interface.

1arch('app')
2 ->expect('App\ValueObjects')
3 ->toImplementNothing();

toHaveAttribute()

The toHaveAttribute() method may be used to ensure that a certain class has a specific attribute.

1arch('app')
2 ->expect('App\Console\Commands')
3 ->toHaveAttribute('Symfony\Component\Console\Attribute\AsCommand');

toHaveMethod()

The toHaveMethod() method may be used to ensure that a certain class has a specific method.

1arch('app')
2 ->expect('App\Http\Controllers\HomeController')
3 ->toHaveMethod('index');

toHavePrefix()

The toHavePrefix() method may be used to ensure that all files within a given namespace have a specific prefix.

1arch('app')
2 ->expect('App\Helpers')
3 ->not->toHavePrefix('Helper');

toHaveSuffix()

The toHaveSuffix() method may be used to ensure that all files within a given namespace have a specific suffix.

1arch('app')
2 ->expect('App\Http\Controllers')
3 ->toHaveSuffix('Controller');

toHaveConstructor()

This toHaveConstructor() method may be used to ensure that all files within a given namespace have a __construct method.

1arch('app')
2 ->expect('App\ValueObjects')
3 ->toHaveConstructor();

toHaveDestructor()

This toHaveDestructor() method may be used to ensure that all files within a given namespace have a __destruct method.

1arch('app')
2 ->expect('App\ValueObjects')
3 ->toHaveDestructor();

toOnlyImplement()

The toOnlyImplement() method may be used to ensure that certain classes are restricted to implementing specific interfaces.

1arch('app')
2 ->expect('App\Responses')
3 ->toOnlyImplement('Illuminate\Contracts\Support\Responsable');

toOnlyUse()

The toOnlyUse() method may be used to guarantee that certain classes are restricted to utilizing specific functions or classes. For example, you may ensure your models are streamlined and solely dependent on the Illuminate\Database namespace, and not, for instance, dispatching queued jobs or events.

1arch('models')
2 ->expect('App\Models')
3 ->toOnlyUse('Illuminate\Database');

toOnlyBeUsedIn()

The toOnlyBeUsedIn() method enables you to limit the usage of a specific class or set of classes to only particular parts of your application. For instance, you can use this method to confirm that your models are only used by your repositories and not by controllers or service providers.

1arch('models')
2 ->expect('App\Models')
3 ->toOnlyBeUsedIn('App\Repositories');

toUse()

By combining the not modifier with the toUse() method, you can indicate that files within a given namespace should not use specific functions or classes.

1arch('globals')
2 ->expect('App\Domain')
3 ->not->toUse('request');
4 
5arch('globals')
6 ->expect('App\Domain')
7 ->not->toUse('Illuminate\Http');

toUseNothing()

If you want to indicate that particular namespaces or classes should not have any dependencies, you can utilize the toUseNothing() method.

1arch('value objects')
2 ->expect('App\ValueObjects')
3 ->toUseNothing();

toUseStrictTypes()

The toUseStrictTypes() method may be used to ensure that all files within a given namespace utilize strict types.

1arch('app')
2 ->expect('App')
3 ->toUseStrictTypes();

Modifiers

Sometimes, you may want to apply the given expectation but excluding certain namespaces or type of files. For that, you may use the following modifiers:

classes()

The classes() modifier allows you to restrict the expectation to only classes.

1arch('app')
2 ->expect('App')
3 ->classes()
4 ->toBeFinal();

enums()

The enums() modifier allows you to restrict the expectation to only enums.

1arch('app')
2 ->expect('App\Models')
3 ->enums()
4 ->toOnlyBeUsedIn('App\Models');

ignoring()

When defining your architecture rules, you can use the ignoring() method to exclude certain namespaces or classes that would otherwise be included in the rule definition.

1arch('facades')
2 ->expect('Illuminate\Support\Facades')
3 ->not->toBeUsed()
4 ->ignoring('App\Providers');

In some cases, certain components may not be regarded as "dependencies" as they are part of the native PHP library. To customize the definition of "native" code and exclude it during testing, Pest allows you to specify what to ignore.

For example, if you do not want to consider Laravel a "dependency", you can use the arch() method inside the beforeEach() function to disregard any code within the "Illuminate" namespace. This approach allows you to focus only on the actual dependencies of your application.

1// tests/Pest.php
2uses()->beforeEach(function () {
3 $this->arch()->ignore([
4 'Illuminate',
5 ])->ignoreGlobalFunctions();
6})->in('Feature');

interfaces()

The interfaces() modifier allows you to restrict the expectation to only interfaces.

1arch('app')
2 ->expect('App')
3 ->interfaces()
4 ->toExtend('App\Contracts\Contract');

traits()

The traits() modifier allows you to restrict the expectation to only traits.

1arch('app')
2 ->expect('App')
3 ->traits()
4 ->toExtend('App\Traits\Trait');

In this section, you have learned how to perform architectural testing, ensuring that your application or library's architecture meets a specified set of architectural requirements. Next, have you ever wondered how to test the performance of your code? Let's explore Stress Testing.