holyshared / peridot-temporary-plugin
为 peridot-php 提供临时文件/目录插件的插件
1.0.5
2015-12-31 06:01 UTC
Requires
- php: >=5.5.0
- peridot-php/peridot: ~1.16
- ramsey/uuid: ~3.1
Requires (Dev)
- cloak/peridot-cloak-plugin: ~2.0
- cloak/robo-coveralls-kit: ~2.1
- codegyre/robo: ~0.6
- expect/peridot-expect-plugin: ~3.0
- holyshared/robo-peridot: ~2.0
- peridot-php/peridot-dot-reporter: ~1.0
- phpspec/prophecy: ~1.5
README
它提供了一个创建临时目录或文件的 API。
测试结束时,目录将被删除。
基本用法
首先注册插件。
编辑 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 实例的 write 或 writeln 方法中将数据输出到临时文件。
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