jasonmccreary / laravel-test-assertions

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

Feature Request: Form Request Assertions #17

Closed gofish543 closed 4 years ago

gofish543 commented 4 years ago

Problem: Testing form requests in Laravel is difficult and annoying.

Solution: I have a little Trait FormRequestAssertions that results in the following test structure...

$this->makeFormRequest(FormRequest::class, route('...'), 'POST');

// Validate Authorization
$this->assertAuthorized();

// Validate rules
// Name is required
$this->updateFormData($data, $files)
        ->assertIsNotValid('name');

// Name exists
$this->updateFormData([
    'name' => $this->faker->company,
])->assertIsValid('name');

This solution allows for form requests tests that target specific validation rule(s) along with the authorization function.

I want to ask if you would like this functionality within this package as this seems like the best place for it; a nice collection of PHPUnit assertions for Laravel. If so, I'd be happy to submit the trait within a PR for your review.

(Maybe bundle in the https://laracasts.com/series/phpunit-testing-in-laravel/episodes/12) as well for mail assertions? I've got a trait build that can be submitted as well

jasonmccreary commented 4 years ago

Sure. This package already has a createFormRequest method. But maybe you can improve it with the additional parameters for the route and request type.

I only ask that you may as much as possible optional to optimize for the simple use case. Or better yet, use the pending object pattern similar to factory or sending requests in Laravel.

I also like the form request assertions. I have a few, but not any validated ones.

My suggestion would be to make "atomic" PRs for each of these. That way we can tweak and review individually, instead of a one big PR with a massive amount of changes.

gofish543 commented 4 years ago

I have not forgotten about this. I've gotten swamped with school work, but plan on coming back to this in about a week!

gofish543 commented 4 years ago

If I get more available time, I'll come back to this.