How to let PHPUnit test property initialization in PHP7.4?

為{幸葍}努か 提交于 2021-02-11 12:09:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!