It's a good idea to have a separate test helper class for generic validation testing. This will make it easier to maintain and update the test data. You can create a separate class with static methods that return arrays of test data for each validation rule. For example:
class ValidationTestData
{
public static function required()
{
return [
'empty string' => ['', false],
'null' => [null, false],
'whitespace' => [' ', false],
'non-empty string' => ['foo', true],
'zero' => [0, true],
'false' => [false, true],
];
}
public static function email()
{
return [
'empty string' => ['', false],
'null' => [null, false],
'whitespace' => [' ', false],
'invalid email' => ['foo', false],
'valid email' => ['[email protected]', true],
];
}
// add more methods for other validation rules
}
Then in your test method, you can use the data provider to test each rule with the appropriate test data:
/**
* @dataProvider requiredProvider
*/
public function testRequired($value, $passes)
{
$validator = Validator::make(['field' => $value], ['field' => 'required']);
$this->assertEquals($passes, $validator->passes());
}
public function requiredProvider()
{
return ValidationTestData::required();
}
This approach will make it easier to add new test data for each validation rule, and to update the test data if the validation rules change.