I am performing validation tests for numerous fields on an object, each of which has numerous validation rules (required
, integer
, unique
etc.). Rather than writing a test function for each field
and rule
, I have a data provider
which is supplied to a generic
test function.
A contrived example of the data provider
:
public function validationErrorsProvider()
{
return [
[ 'field', 'input', 'error message' ],
[ 'field', 'input', 'error message' ],
[ 'field', 'input', 'error message' ],
];
}
The data provider
is then used by a generic test method which asserts that for field
with input
it sees the error message
.
/**
* @test
* @dataProvider validationErrorsProvider
*/
public function will_fail_with_errors_when_input_is_invalid($field, $input, $error)
{
// do some stuff
}
This works fine but the data provider
is becoming increasingly large as I add further fields and rules to be tested (20 - 30 elements in the validationErrorsProvider
array). It appears more complicated that some of my tests.
Is this just something that happens, or is there an alternative? I have considered splitting the data provider
into multiple providers
, each holding just the data for a given field
. This doesn't do anything other than make the data provider
appear smaller.