Created
December 1, 2017 23:55
-
-
Save adamwathan/c1e10a0ec564e2cf4b4c91294f9886ab to your computer and use it in GitHub Desktop.
Unit Testing Custom Validation Rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Rules; | |
use Illuminate\Contracts\Validation\Rule; | |
class Uppercase implements Rule | |
{ | |
public function passes($attribute, $value) | |
{ | |
return strtoupper($value) === $value; | |
} | |
public function message() | |
{ | |
return 'The :attribute must be uppercase.'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Feature\Rules; | |
use Tests\TestCase; | |
use App\Rules\Uppercase; | |
use Illuminate\Foundation\Testing\RefreshDatabase; | |
class UppercaseTest extends TestCase | |
{ | |
/** @test */ | |
function uppercase_strings_pass() | |
{ | |
$rule = new Uppercase; | |
$this->assertTrue($rule->passes('attribute', 'SHOULDPASS')); | |
} | |
/** @test */ | |
function mixed_strings_fail() | |
{ | |
$rule = new Uppercase; | |
$this->assertFalse($rule->passes('attribute', 'ShouldNotPass')); | |
} | |
} |
hi bro
i hava doubts how to implement unit testing in laravel 8
Example for custom boolean validator
class Boolean implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === null) {
$fail(
'The :attribute must be boolean type. Accepted values are: true, false, 1, 0, "true", "false", "1", "0", "yes", "no", "off", "on".'
);
}
}
}
Test case with Pest
it('should pass for boolean', function ($value) {
$validator = new Validator(resolve('translator'), ['test_parameter' => $value], ['test_parameter' => new Boolean]);
expect($validator->passes())->toBeTrue();
})->with([
true,
false,
1,
0,
'true',
'false',
'1',
'0',
'yes',
'no',
'off',
'on',
null // as no value is passed, it should be considered as boolean
]);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@natsu90 Unfortunately it looks like the guide we used is no longer available, but I will link it here in case it comes back up in the future.
https://crnkovic.me/simple-and-reusable-recaptcha-validation-in-laravel/
However, I will also include the rule and our test code so you can see what we did to accomplish recaptcha testing in Laravel.
Create a rule for the recaptcha validation:
In our controller store method, we validate the request using the rule:
We added a recaptcha state for our Form factory to use in tests. This ensures when you send your faked model data through your controller it will pass the validation.
In our test, we are using Mockery and mocking the rule class defined above:
So to summarize, the rule and your controller validation handle the recaptcha checking for actual functionality (you will still need to set up the recaptcha on the frontend using their implementation instructions. This link does not cover testing but does cover basic implementation in Laravel.)
In your test, you create a Faked model, with the value of 'test' as the recaptcha key.
The mock doesn't actually send a request to google during your test, it ensures that the rule method
passes
gets called, and then confirms the validation for the controller method so your test passes.