You aren't really testing your own code, you are testing the validation rules Laravel provides.
Well, no. You’re testing how your application applies the validation rules Laravel provides.
Yes, it comes in handy when you remove a validation rule unintended but how often would that happen?
But it might happen. Just today I’ve literally had a test pick up an instance where I somehow managed to add a typo to a parameter. You don’t get burgled every day but you still buy home insurance, right…?
For the “repetitive” tests, you can leverage PHPUnit data providers. This allows you to re-run the same test case with different parameters each time. I use them in the case you highlighted: lots of fields having a required validation rule:
class CreateArticleTest extends TestCase
{
/**
* @dataProvider requiredFieldsDataProvider
*/
public function testFieldIsRequired($field)
{
$this->createArticle([$field => ''])
->assertValidationError($field);
}
public function requiredFieldsDataProvider()
{
return [
['headline'],
['summary'],
['body'],
];
}
private function createArticle(array $overrides = [])
{
$defaults = [
'headline' => 'Test Headline',
'summary' => 'Test summary',
'body' => 'Test body',
];
return $this->post('/articles', array_merge($defaults, $overrides));
}
}
This will run the testFieldIsRequired case three times, once with each of the items from the data provider. I simply set the specified key to be an empty string, and assert that I get a validation failure.