问题
One of the code style changes in our application when adopting PHP7.4 typed properties was to move from:
if (null === $object->value) { ... }
to
if (empty($object->value)) { ... }
Even when a typed property is nullable, the first if-statement will throw an Error
.
The next step was on writing tests. Where using empty()
on checking whether a typed property is initialized works, the PHPUnit implementation on assertEmpty
crashes with the same error.
Obviously assertFalse(isset($obj->value)
would work, but what assertion should get used to check if a property was not instantiated? Simplification of the test we would want:
function testInstantiatedAfterCall()
{
$obj = new Object('http://example.com');
$obj->changeContainer('Example Container');
$this->assertNull($obj->value);
// or
$this->assertEmpty($obj->value);
}
来源:https://stackoverflow.com/questions/60112627/how-to-let-phpunit-test-property-initialization-in-php7-4