You are familiar with Pest PHP testing framework. If not, refer to Pest PHP documentation..
Why test validation rules?
To ensure that your validation rules are working correctly. Testing ensures that your application is not accepting invalid data, which could have security or performance implications.
To detect bugs in your validation rules. With tests, you can catch bugs early enough and prevent them from problems in production.
To improve the quality of your code. When you test your validation rules, you are forced to think about how they work and how they should be tested.
Testing user registration
Update your PHPUnit
I prefer using the sqlite :memory: database while testing my application for simplicity. Not only does it make tests run faster, but also it does not require an additional database setup. Update the phpunit.xml file as follows.
To ensure that your database is automatically migrated during tests, update Pest.php file
Registration logic
Suppose you have a registration route,
The route point to your controller which validate the request, create a new user and redirect to home page:
Where the content of lang/en/messages.php files is
Testing with pest php datasets
Start by creating a new feature test using the command below. A new file should be created under tests/Feature/Feature/RegistrationControllerTest.php
The easiest way to test multiple validations is to use pest php data sets. The code for RegistrationControllerTest.php file would look like this.
Note: The order of the validation rules matched the order in the RegistrationController.php
I like to customise the validation message by creating lang/en/validation.php file and adding the following content:
Testing output
Run the test by running
The output should look like this:
Code explained
About getLongName() and getATakenEmail()
These are custom helper function i created to simplify the code. They are re-usable and convenient. You can learn more about helper functions from pest documentation
Datasets structure
Consider the following dataset subset
name not too long is an optional human friendly name of the dataset. I use it because to improve readability of the cli output.
validation-rules is the name of the dataset.
fn() => getLongName() it is recommended to use closure function when you get data that involves computation or database.
__(key: 'validation.custom.name.max') a short Laravel function for retrieving translation. This ensure that you test that the correct message is returned to the user.
Customising the validation messages
While it is not mandatory to customise the error messages, as UI/UX designer, I understand the need for clear, concise, and helpful error messages.
Conclusion
Pest datasets are an excellent tool for testing Laravel validation rules. I hope that you have learned a thing or two about testing validation rules. You can get the source code of this guide from my GitHub repository
Do you need help adding test to your Laravel application? Contact me and we can work it out.