An open API service indexing awesome lists of open source software.

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

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.

[![Build Status](https://travis-ci.org/holyshared/peridot-temporary-plugin.svg?branch=master)](https://travis-ci.org/holyshared/peridot-temporary-plugin)
[![HHVM Status](http://hhvm.h4cc.de/badge/holyshared/peridot-temporary-plugin.svg)](http://hhvm.h4cc.de/package/holyshared/peridot-temporary-plugin)
[![Coverage Status](https://coveralls.io/repos/holyshared/peridot-temporary-plugin/badge.svg?branch=master)](https://coveralls.io/r/holyshared/peridot-temporary-plugin?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/holyshared/peridot-temporary-plugin/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/holyshared/peridot-temporary-plugin/?branch=master)
[![Dependency Status](https://www.versioneye.com/user/projects/551fbda0971f781c4800034f/badge.svg?style=flat)](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