Download the PHP package jcergolj/laravel-form-request-assertions without Composer
On this page you can find all versions of the php package jcergolj/laravel-form-request-assertions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jcergolj/laravel-form-request-assertions
More information about jcergolj/laravel-form-request-assertions
Files in jcergolj/laravel-form-request-assertions
Package laravel-form-request-assertions
Short Description Package for unit test laravel form request classes
License MIT
Informations about the package laravel-form-request-assertions
Package for unit testing Laravel form request classes.
Why
Colin DeCarlo gave a talk on Laracon online 21 about unit testing Laravel form requests classes. If you haven't seen his talk, I recommend that you watch it. He prefers testing form requests as a unit and not as feature tests.I like this approach too.
He asked Freek Van der Herten to convert his gist code to package. Granted, I am not Freek; however, I accepted the challenge, and I did it myself. So this package is just a wrapper for Colin's gist, and I added two methods from Jason's package for asserting that controller has the form request.
Installation
Required PHP >=8.0
Usage
Controller
web.php routes
Request
Add the trait to a unit test
After package installation add the TestableFormRequest
trait
Does the controller have the form request test?
or
Test failed Validation Rules
Test attribute has the rule
When dealing with more complicated rules, you might extract logic to dedicated custom rule class. In that instance you don't want to test the logic inside RequestTest class but rather in dedicated custom rule test class. Here you are only interested if the give attribute has/contains the custom rule.
Test assert subset of rules didn't fail
In some situations you might not care weather the whole request passed, but that only set of validation rules didn't fail.
ALERT this only checks that the rule didn't fail, it doesn't check that the rule was actually applied in the first place!
Test Form Request
Test data preparation
Test how data is prepared within the prepareForValidation
method of the FormRequest
.
Extending
If you need additional/custom assertions, you can easily extend the \Jcergolj\FormRequestAssertions\TestFormRequest
class.
-
Create a new class, for example:
\Tests\Support\TestFormRequest
extending the\Jcergolj\FormRequestAssertions\TestFormRequest
class. - Create a new trait, for example:
\Tests\Traits\TestableFormRequest
using the\Jcergolj\FormRequestAssertions\TestableFormRequest
trait. -
Overwrite the
\Jcergolj\FormRequestAssertions\TestableFormRequest::createNewTestFormRequest
method to return an instance of the class created in (1). - Use your custom trait instead of
\Jcergolj\FormRequestAssertions\TestableFormRequest
on your test classes
Available Methods
Contributors
A huge thanks go to Colin and Jason. I created a package from Colin's gist and I copied two methods from Jason's package.
Colin DeCarlo |
Jason McCreary |
Janez Cergolj |