首页 > 解决方案 > 为简单功能寻找可能的代码覆盖路径

问题描述

我很难尝试覆盖特定类方法的代码覆盖的所有路径:

public function getTotalOfType(String $type) : float
{
    $total = 0;

    foreach($this->getCartItemsOfType($type) as $cartitem)
    {
        $total += $cartitem->getAmount();
    }

    return $total;
}

getCartItemsOfType()只会返回一个数组 - 空的或填充购物车项目。

我目前的报道指出,我已经覆盖了两条路径,但缺少第三条。支线覆盖率100%。我的断言如下。我省略了设置:

...
@test
@covers Cart::getTotalOfType()
...

$this->assertEquals(10, $cart->getTotalOfType('shoes')); //passes
$this->assertEquals(20, $cart->getTotalOfType('jumper')); //passes
$this->assertEquals(0, $cart->getTotalOfType('does_not_exist')); //passes as type does not exist
$this->assertEquals(0, $cart->getTotalOfType('')); //passes - empty string returns 0

我错过了一种可能的排列,但似乎无法弄清楚是什么。

此功能可能的路径排列是什么?

以下是我的代码覆盖率报告的结果:

在此处输入图像描述

在此处输入图像描述

标签: phpphpunitcode-coverage

解决方案


推荐阅读