holyshared/peridot-temporary-plugin

为 peridot-php 提供临时文件/目录插件的插件

1.0.5 2015-12-31 06:01 UTC

This package is auto-updated.

Last update: 2024-09-15 15:21:16 UTC


README

它提供了一个创建临时目录或文件的 API。
测试结束时,目录将被删除。

Build Status HHVM Status Coverage Status Scrutinizer Code Quality Dependency Status

基本用法

首先注册插件。
编辑 peridot.php,写入注册代码。

use holyshared\peridot\temporary\TemporaryPlugin;

return function(EventEmitterInterface $emitter)
{
    TemporaryPlugin::create()->registerTo($emitter);
};

创建临时目录

创建临时目录,调用 makeDirectory 方法。
目录名称由 UUID 生成,使用其 ID。

可以在参数中指定权限。

beforeEach(function() {
    $this->temp = $this->makeDirectory(); //return holyshared\peridot\temporary\TemporaryDirectory instance
});
it('create temporary directory', function() {
    expect($this->temp->exists())->toBeTrue();
});

beforeEach(function() {
    $this->temp = $this->makeDirectory(0755);
});
it('create temporary directory', function() {
    expect($this->temp->exists())->toBeTrue();
});

创建临时文件

创建临时文件,调用 makeFile 方法。
文件名称由 UUID 生成,使用其 ID。

可以在参数中指定权限。

beforeEach(function() {
    $this->temp = $this->makeFile(); //return holyshared\peridot\temporary\TemporaryFile instance
});
it('create temporary file', function() {
    expect($this->temp->exists())->toBeTrue();
});

beforeEach(function() {
    $this->temp = $this->makeFile(0755);
});
it('create temporary file', function() {
    expect($this->temp->exists())->toBeTrue();
});

写入临时文件

可以在 TemporaryFile 实例的 writewriteln 方法中将数据输出到临时文件。

beforeEach(function() {
    $this->tempDirectory = $this->makeDirectory();
    $this->tempFile = $this->tempDirectory->createNewFile('report.txt');

    $this->tempFile->writeln('Hello world!!');
    $this->tempFile->writeln('Hello world!!');
});
afterEach(function() {
    $this->cleanUpTemporary();
});

beforeEach(function() {
    $tempDirectory = $this->makeDirectory();
    $tempFilePath = $tempDirectory->resolvePath('report.txt'); //File not created!!

    $tempFile = new SplFileObject($tempFilePath, 'w');
    $tempFile->fwrite('Hello world!!');
    $tempFile->fwrite('Hello world!!');
    $tempFile = null;
});

运行测试

使用以下命令运行。

composer test