I have laravel rules validation and i want test it all give rules with one method and dataProvider using phpunit
For example i have rules required|string|max:255
i want test required
string and
max:255` rules without writing test method separately
But i only can do it for required
rules only i want test the second rules like digit_between
for identity field but first_name
field doesnt have digit_between
but is string
- here my example rules code
{ return [ 'identity_number' => 'required|digits_between:1,255|unique:borrowers', 'first_name' => 'required|string|max:255', 'last_name' => 'required|string|max:255', 'birthdate' => 'required|date', ]; }
2.testing code :
/** * Test store validation. * * @return void * * @group user * @dataProvider requiredStoreValidationProvider */ public function testStoreValidation($formInput, $formInputValue) { $response = $this->actingAs($this->user) ->post(route('borrower.store'), [ $formInput => $formInputValue, ]); $response->assertSessionHasErrorsIn('validation', $formInput); } /** * Required store data validation provider. * * @return string[][] */ public function requiredStoreValidationProvider() { return [ ['identity_number', ''], ['first_name', ''], ['last_name', ''], ['birthdate', ''], ]; }
Advertisement
Answer
Each index is a one test scenario, So the first scenario is to test the required validation, the second scenario would be digits_between:1,255, We set 256 for identity_number, because you have defined a range number, and the rest as you can see.
/** * Required store data validation provider. * * @return string[][] */ public function requiredStoreValidationProvider() { return [ ['identity_number', ''], ['identity_number', 256], ['first_name', ''], ['first_name', 123], ['first_name', str_random(256)], ['last_name', ''], ['last_name', 123], ['last_name', str_random(256)], ['birthdate', ''], ]; }