If you're looking for an elegant and efficient way to test your Laravel application, PestPHP is the ideal tool. Built on top of PHPUnit, Pest offers a fluent and modern syntax, inspired by frameworks like Jest and RSpec, making tests more readable and enjoyable to write.​


What is PestPHP?

Pest is a testing framework for PHP that simplifies writing tests by offering expressive syntax and powerful features. It's fully compatible with PHPUnit, allowing you to enjoy the best of both worlds. With Pest, you can write unit, integration, architecture tests, and more, in a more intuitive and productive way.​


Architecture Testing with Pest

One of Pest's most impressive features is the ability to perform architecture tests. These tests allow you to define rules for your code structure, ensuring it follows established standards and best practices. For example, you can ensure that all classes in a certain namespace are abstract or that models are used only in specific repositories.


Benefits of Architecture Tests

  • Automating Standards: Ensures that code follows defined standards without relying solely on manual reviews.

  • Consistency: Maintains a consistent project structure, making maintenance and scalability easier.

  • Early Problem Detection: Identifies architectural violations early in the development process.​


Examples of Architecture Tests

Ensure all models extend Model

use Illuminate\Database\Eloquent\Model;

arch()
    ->expect('App\Models')
    ->toBeClasses()
    ->toExtend(Model::class);

This test ensures that all classes in the App\Models namespace are classes that extend Illuminate\Database\Eloquent\Model.​


Avoid using debugging functions in production

arch()
    ->expect(['dd', 'dump', 'ray'])
    ->not->toBeUsed();

This test checks if debugging functions like dd, dump, and ray are not being used in the code, preventing them from being accidentally left in production.​


Ensure interfaces are in the correct namespace

arch()
    ->expect('App\Contracts')
    ->toBeInterfaces();

This test ensures that all classes in the App\Contracts namespace are interfaces.​


Verify controllers extend Laravel's base class

use Illuminate\Routing\Controller;

arch()
    ->expect('App\Http\Controllers')
    ->toBeClasses()
    ->toExtend(Controller::class);

This test ensures that all controllers in the App\Http\Controllers namespace extend the base class Illuminate\Routing\Controller.​


Prevent direct use of models in API controllers

arch()
    ->expect('App\Models')
    ->not->toBeUsedIn('App\Http\Controllers\Api');

This test checks if models are not being used directly in API controllers, promoting the use of services or repositories for data access.​


Ensure classes have a specific suffix

arch()
    ->expect('App\Services')
    ->toHaveSuffix('Service');

This test ensures that all classes in the App\Services namespace have the suffix Service, promoting consistent naming.​


Conclusion

PestPHP is a powerful tool that can transform the way you write tests in Laravel applications. With architecture tests, you ensure that your code remains clean, organized, and adheres to best practices. By implementing these tests, you can automate the enforcement of architectural standards, maintain consistency across your project, and detect potential issues early in the development process.

Embracing PestPHP not only enhances your testing strategy but also contributes to the overall health and maintainability of your codebase. Start integrating PestPHP into your Laravel projects today and experience the benefits of a more structured and reliable application architecture.