laravel-form-request-assertions
laravel-form-request-assertions copied to clipboard
Feature Request: Ability to assert `prepareForValidation` behavior
I would love to have the ability to test the behavior of the prepareForValidation
method. In particular it would be interesting to check, that the function changes the request-values/parameters in a desired way.
Edit:
I came up with a solution for this in my test case wich works but is not very pretty:
$emailUsername = \Str::random(20);
$email = strtolower($emailUsername) . '@example.com';
$emailUppercase = strtoupper($emailUsername) . '@example.com';
$requestTester = $this->createFormRequest(CreateRequest::class);
$request = null;
// Get the FormRequest instance
\Closure::fromCallable(function () use (&$request) {
$request = $this->request;
})->call($requestTester);
// Add data to the request
$request->merge(['email' => $emailUppercase]);
// Trigger prepare validation
\Closure::fromCallable(function () {
$this->prepareForValidation();
})->call($request);
// Assert result in request data
$this->assertEquals($request->input('email'), $email);
Aparently the prepareForValidation
method is never called when using the \Jcergolj\FormRequestAssertions\TestFormRequest::validate
and \Jcergolj\FormRequestAssertions\TestFormRequest::assertFails
methods. This leads to issues when you rely on this method to transform data before validation.
Is this still an issue?
Yes, I will send a PR soon.
I think this is solved by #7 ->close