PHP 每晚 : Parameter must be an array or an object that implements Countable

标签 php unit-testing phpunit travis-ci

我看到了 UT failuresa 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/

相关文章:

javascript - PHP/MongoDB 查询协助

django - 在 VSCode 测试资源管理器中运行 Django 测试?

php - 如何在 PHP 单元测试中模拟在构造函数中调用的方法?

PHPUnit 代码覆盖率和异常

php - cURL PHP 内部错误,为什么?

javascript - 使用 PHP 将特定数据从 angularJS 应用程序发送到 json 文件

javascript - 在 JavaScript 中为表单元素创建键值对

ruby-on-rails - 带有载波的 Rails 3 测试 fixture ?

java - Mockito Mock 运行 Autowired 类对象

php - Codeception 测试运行两次