【发布时间】:2014-04-14 14:04:39
【问题描述】:
我正在尝试减少程序的依赖性并使其更易于测试。我这样做的一个例子是在我的一个类的__construct() 方法中。以前,它使用一个文件名,然后 __construct() 方法会在该文件名上使用 file_get_contents() 将内容保存到一个属性中:
public function __construct($name){
$this->name = $name;
$this->contents = file_get_contents($name);
}
为了减少对文件系统的依赖,我将其替换为:
public function __construct(SplFileObject $file){
$this->name = $file->getFilename();
$this->contents = '';
while(!$file->eof()){
$this->contents .= $file->fgets();
}
}
我相信这更容易测试,因为我可以模拟一个 SplFileObject(可以设置为包含我想要的任何内容)并将其传递。到目前为止,我看到的示例涉及执行类似这样的操作:
$stub = $this->getMock('SplFileObject');
$stub->expects($this->any())
->method('fgets')
->will($this->returnValue('contents of file'));
但是SplFileObject 的模拟fgets 方法需要更复杂——它需要遍历每一行内容,并在到达末尾时停止。
目前我有一个可行的解决方案 - 我刚刚创建了一个名为 MockSplFileObject 的全新类,它覆盖了这些方法:
class MockSplFileObject extends SplFileObject{
public $maxLines;
public $filename;
public $contents;
public $currentLine = 1;
public function __construct($filename, $contents){
$this->filename = $filename;
$this->contents = explode("\n",$contents);
return true;
}
public function eof(){
if($this->currentLine == count($this->contents)+1){
return true;
}
return false;
}
public function fgets(){
$line = $this->contents[$this->currentLine-1];
$this->currentLine++;
return $line."\n";
}
public function getFilename(){
return $this->filename;
}
}
然后我使用它而不是调用 PHPUnit 的 getMock() 函数。我的问题是:这是一种合法的做事方式吗?或者有没有更好的方法来模拟更复杂的方法?
【问题讨论】:
标签: php unit-testing testing phpunit