PHPUnit测试fopen / fwrite链

Gin*_*ane 2 php phpunit exception

在一个项目中,我发现了以下代码行:

protected function save($content, $path)
{
    // ...
    if (($handler = @fopen($path, 'w')) === false) {
        throw new Exception('...');
    }

    // ...
    if (@fwrite($handler, $content) === false) {
        throw new Exception('...');
    }

    // ...
    @fclose($handler);
}
Run Code Online (Sandbox Code Playgroud)

我想用PHPUnit测试此方法,但是我对正确的测试用例有些困惑。如果我传递的不正确$path$path具有错误权限的正确信息(例如0444),那么一切都会在第一个例外处停止。如果我$path以正确的权限传递了正确的权限,那么PHP也将能够写入该文件,并且不会达到第二个异常。

那么有没有办法在不重写此方法的情况下测试第二个异常?

还是最好同时检查fopen和同时检查fwrite一种情况并为两者都使用一种例外情况?

还是最好的选择是将此方法分为两种-一种用于打开,一种用于书写-分别测试?

l-x*_*l-x 5

达到目标的最佳方法是使用模拟文件系统。我建议使用vfsStream

$ composer require mikey179/vfsStream
Run Code Online (Sandbox Code Playgroud)

首先,我要提到的是,如果使用无效参数调用此函数,则fread仅返回false。如果发生任何其他错误,它将返回已写入的字节数。因此,您将不得不添加另一项检查:

class SomeClass {
    public function save($content, $path)
    {
        // ...
        if (($handler = @fopen($path, 'w')) === false) {
            throw new Exception('...');
        }

        $result = @fwrite($handler, $content);

        // ...
        if ($result === false) { // this will only happen when passing invalid arguments to fwrite
            throw new Exception('...');
        }

        // ...
        if ($result < strlen($content)) { // additional check if all bytes could have been written to disk
            throw new Exception('...');
        }

        // ...
        @fclose($handler);
    }
}
Run Code Online (Sandbox Code Playgroud)

该方法的测试用例如下所示:

class SomeClassTest extends \PHPUnit_Framework_TestCase {

    /**
     * @var vfsStreamDirectory
     */
    private $fs_mock;

    /**
     * @var vfsStreamFile
     */
    private $file_mock;

    /**
     * @var $sut System under test
     */
    private $sut;

    public function setUp() {
        $this->fs_mock = vfsStream::setup();
        $this->file_mock = new vfsStreamFile('filename.ext');
        $this->fs_mock->addChild($this->file_mock);

        $this->sut = new SomeClass();
    }

    public function testSaveThrowsExceptionOnMissingWritePermissionOnFile() {
        $this->expectException(\Exception::class);

        $this->file_mock->chmod(0);
        $this->sut->save(
            'content',
            $this->file_mock->url()
        );
    }

    public function testSaveThrowsExceptionOnMissingWritePermissionOnDirectory() {
        $this->expectException(\Exception::class);

        $this->fs_mock->chmod(0);
        $this->sut->save(
            'content',
            $this->fs_mock->url().'/new_file.ext'
        );
    }

    public function testSaveThrowsExceptionOnInvalidContentType() {
        $this->expectException(\Exception::class);

        $this->fs_mock->chmod(0);
        $this->sut->save(
            $this,
            $this->file_mock->url()
        );
    }

    public function testSaveThrowsExceptionOnDiskFull() {
        $this->expectException(\Exception::class);

        $this->fs_mock->chmod(0777); // to be sure
        $this->file_mock->chmod(0777); // to be sure

        vfsStream::setQuota(1); // set disk quota to 1 byte

        $this->sut->save(
            'content',
            $this->file_mock->url()
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望我能帮忙...

  • @Jazzzzzz 从安装作曲家开始 - 你不会回头 (4认同)