【发布时间】:2021-04-16 13:51:57
【问题描述】:
我将 Xdebug 版本从 2.9.3 升级到 3.0.1,在使用 --code-coverage 运行单元测试后,我发现代码覆盖率结果存在一些差异。 假设有两种环境:
- 环境 1:PHP 7.3.16、Xdebug 2.9.3、php-code-coverage 8.0.2、PHPUnit 9.0.2
- 环境 2:PHP 7.3.25、Xdebug 3.0.1、php-code-coverage 8.0.2、PHPUnit 9.0.2
代码示例1:
public function getAllOptions(): array
{
$this->_options = [
[
'label' => _'label 1,
'value' => 'value 1'
],
[
'label' => _'label 2,
'value' => 'value 2'
]
];
return $this->_options;
}
代码示例 1 的结果:
- 环境 1:100% 的代码覆盖率
- 环境 2:第二行 ($this->_options =) 被标记为未覆盖
代码示例2:
private function moveImportedFileToArchive(Operation $operation): void
{
$sourceFilePath = $this->_filesystem->getDirectoryWrite(DirectoryList::ROOT)->getAbsolutePath()
. $operation->getFileInfo()['file_path']
. '/'
. $operation->getFileInfo()['file_name'];
// @codingStandardsIgnoreStart
$fileName = basename($sourceFilePath);
// @codingStandardsIgnoreEnd
$archiveFileName = (int)gmdate('U') . '_' . $fileName;
$archivePath = static::ARCHIVE_DIR . $archiveFileName;
$this->_varDirectory->renameFile($sourceFilePath, $archivePath);
}
代码示例 2 的结果:
- 环境 1:100% 的代码覆盖率
- 环境 2:第四行 (.'/') 被标记为未覆盖
我不知道是什么导致了这些差异。 它们通常发生在将值分配给变量的地方(通常与将数组分配给变量或将变量分配给数组或将数组分配给数组有关)
【问题讨论】:
标签: php unit-testing phpunit code-coverage xdebug