考虑以下场景(这不是生产代码):
class MyClass {
public function myMethod() {
// create a directory
$path = sys_get_temp_dir() . '/' . md5(rand());
if(!mkdir($path)) {
throw new Exception("mkdir() failed.");
}
// create a file in that folder
$myFile = fopen("$path/myFile.txt", "w");
if(!$myFile) {
throw new Exception("Cannot open file handle.");
}
}
}
对,那有什么问题?代码覆盖率报告此行未涵盖:
throw new Exception("Cannot open file handle.");
这是正确的,但是由于我在逻辑上创建了上面的文件夹,因此似乎不可能fopen()
失败(可能在极端情况下除外,例如 100% 的磁盘)。
我可以忽略代码覆盖率中的代码,但那是一种作弊。有什么方法可以模拟文件系统,以便它可以识别myFile.txt
和模拟无法创建文件的文件系统?