jasonmccreary / laravel-test-assertions

A set of helpful assertions when testing Laravel applications.
321 stars 34 forks source link

Asserting a route uses a form request doesn't work with first class callables #43

Closed phily245 closed 1 year ago

phily245 commented 1 year ago

I came across this when using rector with the LevetSetList::UPTO_PHP_82 rule to upgrade my code from PHP 8.1 to 8.2.

Rector converted my routes from this format:

 Route::post('/', [SensorController::class, 'store'])
            ->name('store');

To use a first-class callable:

Route::post('/', new (SensorController())->store(...))
            ->name('store');

My pest test then failed:

it('validates using the correct form request', function () {
    $this->assertRouteUsesFormRequest('sensors.store', SensorRequest::class);
});

Upon closer inspection, inside assertRouteUsesFormRequest it looks for the controller action at https://github.com/jasonmccreary/laravel-test-assertions/blob/master/src/Traits/AdditionalAssertions.php#L14-L16, which is not set when using a first class callable, so the test then failed.

jasonmccreary commented 1 year ago

Probably not something I'll fix until Laravel is proven to support/use this syntax.

phily245 commented 1 year ago

As a note for anyone else experiencing this, I fixed this by adding the following to my rector.php file:

    $rectorConfig->skip([
        FirstClassCallableRector::class => [
            __DIR__ . '/routes',
        ],
    ]);