Optional Path¶
Considering you have a complex structure with optional path into it. You can use the method ifPathIsReadable to make some test optional:
(new Tester(null))
->ifPathIsReadable(
'notExistingPath',
function (Tester $tester) {
//Will not be call with current data to test
}
);
This obviously make more sense with a combination of each. In this more complex example lets say you receive a list of users object that don’t have the same properties available:
$users = [
(object)[
'firstName' => 'Martin',
'active' => true,
'referral' => 'Google'
],
(object)[
'firstName' => 'Julie',
'active' => false
]
];
(new Tester($users))
->each(
function (Tester $tester) {
$tester->path('firstName')->assertInternalType('string');
$tester->path('active')->assertInternalType('boolean');
$tester->ifPathIsReadable(
'referral',
function (Tester $tester) {
$tester->assertInternalType('string');
}
);
}
);