-
-
Save adamwathan/c1e10a0ec564e2cf4b4c91294f9886ab to your computer and use it in GitHub Desktop.
<?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.'; | |
} | |
} |
<?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 @oranges13 did you get the solution?
@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:
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use GuzzleHttp\Client;
class Recaptcha implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$client = new Client();
$response = $client->post('https://www.google.com/recaptcha/api/siteverify',
['form_params' => [
'secret' => env('RECAPTCHA_SECRET_KEY'),
'response' => $value,
]]);
$body = json_decode((string) $response->getBody());
return $body->success;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'We were unable to verify your input, please try again!';
}
}
In our controller store method, we validate the request using the rule:
use App\Rules\Recaptcha; // Make sure to include your rule at the top of your controller
// Inside the store method call the rule
$this->validate(request(), [
'name' => 'required',
'email' => 'required|email',
'activity' => 'required',
'g-recaptcha-response' => ['required', app(Recaptcha::class)],
]);
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.
$factory->state(App\Form::class, 'recaptcha', function() {
return [
'g-recaptcha-response' => 'test',
];
});
In our test, we are using Mockery and mocking the rule class defined above:
<?php
namespace Tests\Feature\Form;
use App\Form;
use App\Rules\Recaptcha;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class FormTest extends TestCase
{
use RefreshDatabase; use WithFaker;
public function setUp() {
parent::setUp();
$this->mockCaptcha(); // Call your mock function
}
// Test that the form validates with a full set of data
public function test_form_validates_full_input(){
$full_form = factory(Form::class)->states('full','recaptcha')->raw();
$this->post(route('forms.store'), $full_form)->assertRedirect(route('home'));
$this->assertCount(1, Form::all());
}
/**
* Recaptcha mock so validation will pass
* @see https://crnkovic.me/simple-and-reusable-recaptcha-validation-in-laravel/
*/
protected function mockCaptcha() {
$this->app->singleton(Recaptcha::class, function() {
return \Mockery::mock(Recaptcha::class, function($mock) {
$mock->shouldReceive('passes')->andReturn(true);
});
});
}
}
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.
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
]);
If I am using this custom rule in a controller, how can I write a test to mock the validation so it passes? (Real world example is reCaptcha -- I have a key that is sent to google's servers and tested when a form is submitted, but I obviously can't do that in a unit test, but I need my test to pass).
Example in my controller:
where
Recaptcha
is my custom validator.In my test I'm calling
$this->post(route('forms.store', $form_data))
but without the recaptcha key the test fails. Any suggestions or examples for controller testing with custom validations?