laravel-form-request-assertions icon indicating copy to clipboard operation
laravel-form-request-assertions copied to clipboard

Feature Request: Ability to assert `prepareForValidation` behavior

Open jacob418 opened this issue 10 months ago • 2 comments

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.

jacob418 avatar Apr 19 '24 09:04 jacob418

Is this still an issue?

jcergolj avatar Apr 25 '24 15:04 jcergolj

Yes, I will send a PR soon.

jacob418 avatar Apr 26 '24 06:04 jacob418

I think this is solved by #7 ->close

jacob418 avatar May 30 '24 10:05 jacob418