Reusable Test Callable¶
Some time you want to execute the same set of test on different source of data. You have a method that return you a user and one that return you a list of users. You can simply create a class that as all the test inside of it.
<?php\nnamespace Your\Project\Name;\nuse Draw\DataTester\Tester;
class UserDataTester
{
public function __invoke(Tester $tester)
{
$tester->path('firstName')->assertInternalType('string');
$tester->path('active')->assertInternalType('boolean');
$tester->ifPathIsReadable(
'referral',
function (Tester $tester) {
$tester->assertInternalType('string');
}
);
}
}
And now you can use it to test the data of one user:
$user = (object)[
'firstName' => 'Martin',
'active' => true,
'referral' => 'Google'
];
(new Tester($user))
->test(new UserDataTester());
Or with each in case of a list of users:
$users = [
(object)[
'firstName' => 'Martin',
'active' => true,
'referral' => 'Google'
],
(object)[
'firstName' => 'Julie',
'active' => false
]
];
(new Tester($users))
->each(new UserDataTester());