pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.62k stars 358 forks source link

[Bug]: Laravel preset not accounting for static middleware function in controller #1252

Closed danielh-official closed 1 month ago

danielh-official commented 1 month ago

What Happened

Laravel 11 allows you to specify middleware in a controller using a public static middleware function: https://laravel.com/docs/11.x/controllers#controller-middleware

However, the Laravel preset says that the only functions allowed to exist on a controller are the following:

Screenshot 2024-09-13 at 9 43 16 AM

How to Reproduce

  1. Create a new Laravel repo
  2. Create an ArchitectureTest.php file in the Unit folder
  3. Add arch()->preset()->laravel(); to it
  4. Create a DummyController.php in the App/Http/Controllers folder and add the following code:
    public static function middleware(): array
    {
        return [];
    }

Sample Repository

No response

Pest Version

3.0.1

PHP Version

8.2

Operation System

macOS

Notes

No response

danielh-official commented 1 month ago

Would it be possible to implement preset overriding functionality?

So, you still have the preset, but if there's something in the latest Laravel release that doesn't match convention, rather than waiting for the package to be updated, a developer can write:

arch()->preset()->laravel()
    ->setExpectation('App\Http\Controllers')
    ->not->toHavePublicMethodsBesides(['__construct', '__invoke', 'index', 'show', 'create', 'store', 'edit', 'update', 'destroy', 'middleware']);

And it would override the previous expectation that tackles that part of the Controller architecture.

CamKem commented 1 month ago

I mean is it more than 1 controller? An easy fix would just be to ignore it for now?

arch()->preset()->laravel()->ignoring(FeedController::class);

A better fix would be to fork the Pest repo & submit a PR to add the middleware method in the array, IMO given it's a framework convention, it should be added unless laravel removes this functionality.