fikrimi's avatar

Should I create test data collection for validation testing ?

Consider that I am new to phpUnit testing and I am using laravel. this topic is on Laravel Validation.

I am now having some kind of request validation testing, that I want my request to be tested against the correct laravel validation given for that request (required, digits, email, and such). I use phpunit @dataprovider and currently have something like this:

// @dataprovider
return [
            'student_id-required'             => ['parent_as', ''],
            'student_birth_date-required'     => ['parent_as', ''],
            'nik-length-16-1'                 => ['nik', '182309812093812093809'],
            'nik-length-16-2'                 => ['nik', '1234'],
            'student_birth_date-format-ymd-1' => ['student_birth_date', '17-08-1945'],
            'student_birth_date-format-ymd-2' => ['student_birth_date', '19450817'],
            'email-not-email-1'               => ['email', 'something@@somedomain'],
            'email-not-email-2'               => ['email', 'something aja'],
            'email-not-email-3'               => ['email', 'http://www.google.com'],
];

this will tested against the request, I can get that something like "student_id" or "nik" is not in generally available on our application, but "email" or "date_format" maybe it is general.

Given that, should I for future make the generic validation be on its own test helper, some kind of general test data class that have all the invalid value for given validation?

or, should I change my approach to validation testing ?

0 likes
1 reply
LaryAI's avatar
Level 58

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.

Please or to participate in this conversation.