How do you test multiple values for same Attribute ?
class Test {
private $_optionalValue = null;
function setValue(String $optionalValue)
{
$this->_optionalValue = $optionalValue;
}
}
So here, “$_optionalValue” could be NULL or User defined value, but when I check with phpunit like this :
$optionalValue = PHPUnit_Util_Class::getObjectAttribute($my_object, '_optionalValue');
$this->assertThat(
$optionalValue,
$this->logicalXor(
$this->assertNull($optionalValue),
$this->logicalAnd(
$this->assertAttributeInternalType('string', '_optionalValue', $optionalValue),
$this->assertRegExp('/[0-9]{2}:[0-9]{2}:[0-9]{2}/', (string) $optionalValue)
)
)
);
The Regexp assertion fails because $optionalValue isn’t a String (it’s null by default)
You’re testing a private property of an object which should be generally avoided because that’s an internal of the unit and you should not care about that.
If your unit needs to validate values of that class, it’s likely that generally a value of some kind should be validated.
You could therefore encapsulate the logic of the validation into a unit of it’s own, e.g. a validator:
You can then write unit-tests for the validator. You then know that your validator works and you can use it everywhere you like.