我看到了 UT failures在 a PHP webapp每晚在 Travis CI 中针对 PHP 运行:
$ php --version
PHP 7.2.0-dev (cli) (built: Dec 4 2016 22:49:34) ( ZTS )
这是失败的测试用例:
$payments = PaymentsHelper::refunds('DE0000000001', '2016-04-01', '2017-04-01');
$this->assertNotNull($payments);
$this->assertEquals(0, count($payments));
测试失败
1) PaymentsHelperTest::test_refunds_within_lifetime
count(): Parameter must be an array or an object that implements Countable
目前正在测试的代码包含调试日志,显示 refunds()
的返回值确实是一个数组:
Array
(
[0] => Payment Object
(
...
)
)
我是否每晚都在 PHP 中遇到错误?
最佳答案
我进一步调试了这个问题并意识到错误发生在代码的早期,其中依赖项返回 NULL
并且代码调用 count(NULL)
:
$bonds = $bond_factory->find_all(/* ... */);
if (count($bonds) > 0)
{
//...
}
我决定用空检查来保护对 count()
的调用:
$bonds = $bond_factory->find_all(/* ... */);
if (!is_null($bonds) && count($bonds) > 0)
{
//...
}
PHP 7.2.0-dev 现在显然对其输入不那么宽容了。
关于PHP 每晚 : Parameter must be an array or an object that implements Countable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40976906/