PHPUnit单括号线揭开

PHPUnit Single bracket line uncovered

任何人都可以告诉我为什么在最后一次休息后这一行;保持未覆盖状态,因此该方法的覆盖率为 0%!

private function isADateParameter($fieldName, $fieldValue){
    if(!is_array($fieldValue)){
        $this->addPayloadError('filter',320, $fieldName. 'is a date field and needs an array filter like {"gt":"2015-05-01"}');
        return false;
    }
    foreach ($fieldValue as $criteria => $date){
        $ar_date = explode('-', $date);
        switch(false){
            case in_array($criteria, ['gt', 'lt', 'gte', 'lte']):
                $this->addPayloadError('filter',321, $fieldName. ' supports only constraints gt, lt, gte or lte');
                break;
            case preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date):
                $this->addPayloadError('filter',322, $fieldName. ' supports only date in format YYYY-MM-DD');
                break;
            case checkdate($ar_date[1], $ar_date[2], $ar_date[0]):
                $this->addPayloadError('filter',323, $date. ' is not a real date');
                break;
        } //uncovered red line in html report
    }
    if(isset($this->payloadErrors['filter']) && count($this->payloadErrors['filter']) > 0){
        return false;
    }
    return true;
}

参见this bug report,特别是关闭注释:

This is not a bug. In theory "$value" could be something else than 1, 2 or 3 and in that case the switch() would not leave due to one of the values. Xdebug can not make assumptions on what "doSomething()" returns an has no way to know that all that ever is returned than 1, 2 or 3. The last } of each function and method includes an implicit "return NULL;" - which PHP sees as executable code. Because there is a possible code path where the } is not hit, it is marked as "not executed".

在你的例子中,你有三个 case 语句,我想你已经为它们编写了测试,但是还有第三个隐含的路径,你可能会或可能不会测试 - 如果 none 三种情况匹配?

tl;dr: 向开关添加一个 default 案例,并确保您的测试执行它。或者不要忽略那条覆盖线。