如何测试用phpunit调用抽象方法的具体方法

时间:2015-01-21 15:51:37

标签: php unit-testing phpunit abstract-class

我有一个具体方法的抽象类。所以我想测试那些具体的方法。

这是我的抽象类:

abstract class File {

    private $debug_filename_pattern = 'DELETE_ME_%s.debug';
    private $filename;
    private $filepath;

    abstract public function buildFilename();

    public function __construct($debug = false) {
        $filename = $this->buildFilename();
        if ($debug) {
            $filename = sprintf($this->debug_filename_pattern, $filename);
        }
        $this->filename = $filename;
        $this->buildFilepath();
    }

    private function buildFilepath() {
        $this->filepath = ini_get('upload_tmp_dir') . DIRECTORY_SEPARATOR . $this->filename;
    }
}

我在phpunit documentation中阅读了关于测试抽象类的部分,然后我想出了那个测试:

final class FileTest extends \PHPUnit_Framework_TestCase {

    public function test() {
        $stub = $this->getMockForAbstractClass('MyBundle\File', [true]);
        $stub->expects($this->atLeastOnce())
                ->method('buildFilename')
                ->withAnyParameters()
                ->will($this->returnValue('test.log'));
        $this->assertEquals('C:\xampp\tmp\DELETE_ME_test.log.debug', $stub->getFilePath());
    }

}

但它不起作用。我的断言总是返回它失败并显示以下错误信息:

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'C:\xampp\tmp\DELETE_ME_test.log.debug'
+'C:\xampp\tmp\DELETE_ME_.debug'

我知道我的模拟对象已实例化,然后我为buildFilename方法添加了一个模拟。让我的测试总是失败。

有没有办法在实例化之前模拟我的抽象方法?我应该重构我的抽象类吗?

1 个答案:

答案 0 :(得分:1)

我认为您无法按照自己的方式设置模拟。在->getMock()时调用构造方法。然后你试图在事后设定期望。

一般来说,我发现当某些东西变得难以测试时,就像这种情况一样,这表明设计存在问题。我认为你遇到的问题是你在构造函数中做的太多了。

您正在进行各种繁重的工作以确定对象构造的文件路径。为什么不改变它以便在你致电getFilePath时发生。你的课最终看起来像这样:

abstract class File {

    private $debug_filename_pattern = 'DELETE_ME_%s.debug';
    private $filename;
    private $filepath;
    protected $debug;

    abstract public function buildFilename();

    public function __construct($debug = false) {
        $this->debug = $debug;
    }

    private function buildFilepath() {
        $filename = $this->buildFilename();
        if ($this->debug) {
            $filename = sprintf($this->debug_filename_pattern, $filename);
        }
        $this->filename = $filename;
        $this->filepath = ini_get('upload_tmp_dir') . DIRECTORY_SEPARATOR . $this->filename;
    }

    public function getFilePath() {
        if(!this->filepath) {
            $this->buildFilepath();
        }

        return $this->filepath;
    }
}

现在进行测试以确保路径仅构建一次,只需再次添加断言。

final class FileTest extends \PHPUnit_Framework_TestCase {

    public function test() {
        $stub = $this->getMockForAbstractClass('MyBundle\File', [true]);
        $stub->expects($this->once())
                ->method('buildFilename')
                ->withAnyParameters()
                ->will($this->returnValue('test.log'));
        $this->assertEquals('C:\xampp\tmp\DELETE_ME_test.log.debug', $stub->getFilePath());
        $this->assertEquals('C:\xampp\tmp\DELETE_ME_test.log.debug', $stub->getFilePath());
    }

}