https://github.com/holyshared/peridot-temporary-plugin
Temporary file / directory plugin for peridot-php
https://github.com/holyshared/peridot-temporary-plugin
Last synced: over 1 year ago
JSON representation
Temporary file / directory plugin for peridot-php
- Host: GitHub
- URL: https://github.com/holyshared/peridot-temporary-plugin
- Owner: holyshared
- License: mit
- Created: 2015-03-18T04:33:54.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-12-31T06:03:38.000Z (over 10 years ago)
- Last Synced: 2025-02-17T13:45:18.896Z (over 1 year ago)
- Language: PHP
- Homepage:
- Size: 45.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
peridot-temporary-plugin
========================================================
It provides an api to create a temporary directory or file.
Directory of file will be deleted at the end of the test.
[](https://travis-ci.org/holyshared/peridot-temporary-plugin)
[](http://hhvm.h4cc.de/package/holyshared/peridot-temporary-plugin)
[](https://coveralls.io/r/holyshared/peridot-temporary-plugin?branch=master)
[](https://scrutinizer-ci.com/g/holyshared/peridot-temporary-plugin/?branch=master)
[](https://www.versioneye.com/user/projects/551fbda0971f781c4800034f)
Basic usage
--------------------------------------------------------
First will first register the plugin.
Edit the **peridot.php**, write the code to register.
```php
use holyshared\peridot\temporary\TemporaryPlugin;
return function(EventEmitterInterface $emitter)
{
TemporaryPlugin::create()->registerTo($emitter);
};
```
### Create a temporary directory
Create a temporary directory, call the **makeDirectory** method.
Directory name is generated by [UUID](http://tools.ietf.org/html/rfc4122.html), use the id.
Permissions can be specified in the argument.
```php
beforeEach(function() {
$this->temp = $this->makeDirectory(); //return holyshared\peridot\temporary\TemporaryDirectory instance
});
it('create temporary directory', function() {
expect($this->temp->exists())->toBeTrue();
});
```
or
```php
beforeEach(function() {
$this->temp = $this->makeDirectory(0755);
});
it('create temporary directory', function() {
expect($this->temp->exists())->toBeTrue();
});
```
### Create a temporary file
Create a temporary file, call the **makeFile** method.
File name is generated by [UUID](http://tools.ietf.org/html/rfc4122.html), use the id.
Permissions can be specified in the argument.
```php
beforeEach(function() {
$this->temp = $this->makeFile(); //return holyshared\peridot\temporary\TemporaryFile instance
});
it('create temporary file', function() {
expect($this->temp->exists())->toBeTrue();
});
```
or
```php
beforeEach(function() {
$this->temp = $this->makeFile(0755);
});
it('create temporary file', function() {
expect($this->temp->exists())->toBeTrue();
});
```
Write to a temporary file
--------------------------------------------------------
You can output the data to a temporary file in the **write** or **writeln** method of TemporaryFile instance.
```php
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();
});
```
or
```php
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;
});
```
Running tests
--------------------------------------------------------
Run with the following command.
composer test