Download the PHP package arielenter/laravel-phpunit-test-validation-rules without Composer
On this page you can find all versions of the php package arielenter/laravel-phpunit-test-validation-rules. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download arielenter/laravel-phpunit-test-validation-rules
More information about arielenter/laravel-phpunit-test-validation-rules
Files in arielenter/laravel-phpunit-test-validation-rules
Package laravel-phpunit-test-validation-rules
Short Description Assertion methods that help to test that the desired validation rule(s) are implemented in a given URL or route name.
License GPL-3.0
Informations about the package laravel-phpunit-test-validation-rules
Package for Laravel Phpunit validation rules testing.
Description
Trait to be used within TestCase’s tests. It provides assertions to check if a given validation rule is implemented in a given URL or route name for a given request method. One of its more attractive functionality is that it’s possible to test multiple validation rules on one assertion instruccion.
How it works
A desired validation rule is tested by submitting a provided invalid field value example to a given URL or route name using an established request method and asserting that the expected error message is returned from it. No need to provided the expected error thought. See Assertions Code In A Nutshell section to check in brief how exactly the code does this.
Installation
Usage
Say the following routes are in place:
web.php
It would be necessary to have the following tests to make sure all the desired validations are in place:
RoutesValidationTest.php
Argument ‘list’ array shape
Though I believe that the last example to test multiple rules in one assertion says more than a thousand words on it self, I decided it was still a good idea to include a PHPDoc explaining how the ‘list’ argument must be formatted:
Assetions Code In A Nutshell
A brief explanation on how this assertions were coded is that the following function is used to get the expected fail validation message:
And once the expected fail validation error message is known, it’s then used to check if said message is returned when submitting the invalid data to the given URL using an already existent TestCase request function like the following and using one of it’s also already existenting assertions:
The following is a quick example code that shows in a nutshell how the assertions were made.
AssertionsCodeInANutshellTest.php
Additional Information
Supported TestCase Request Methods
As described in the paragraphs above, Phpunit’s TestCase request methods are used to make the assertions, to this end, you can select which method you desire to be used by sending the ‘requestMethod’ argument with one of the following strings:
- get
- post
- put
- patch
- delete
- options
- getJson
- postJson
- putJson
- patchJson
- deleteJson
- optionsJson
The ‘requestMethod’ argument is flexible, meaning it’s case insensitive and other naming conventions could be used and it will work the same (example ‘post-json’ instead of ‘postJson’).
About TestCase ‘get’ Request Method
In case you are wondering, if you were to use the ‘get’ method or it’s json variant ‘getJson’, it won’t be necessary to include parameters on it’s URL, this would be handle by the assertion on its own.
Additional Arguments Explanation
Phpunit’s TestCase request methods have an optional argument ‘headers’, to this end, an optional ‘header’ argument is also available.
Phpunit’s TestCase Json type request methods have also an optional argument ‘options’, and for that specific reason ‘options’ is also available, thought it will be ignore if the given method is not a json type.
License
GNU General Public License (GPL) version 3