It provides an api to create a temporary directory or file.
Directory of file will be deleted at the end of the test.
First will first register the plugin.
Edit the peridot.php, write the code to register.
use holyshared\peridot\temporary\TemporaryPlugin;
return function(EventEmitterInterface $emitter)
{
TemporaryPlugin::create()->registerTo($emitter);
};
Create a temporary directory, call the makeDirectory method.
Directory name is generated by UUID, use the id.
Permissions can be specified in the argument.
beforeEach(function() {
$this->temp = $this->makeDirectory(); //return holyshared\peridot\temporary\TemporaryDirectory instance
});
it('create temporary directory', function() {
expect($this->temp->exists())->toBeTrue();
});
or
beforeEach(function() {
$this->temp = $this->makeDirectory(0755);
});
it('create temporary directory', function() {
expect($this->temp->exists())->toBeTrue();
});
Create a temporary file, call the makeFile method.
File name is generated by UUID, use the id.
Permissions can be specified in the argument.
beforeEach(function() {
$this->temp = $this->makeFile(); //return holyshared\peridot\temporary\TemporaryFile instance
});
it('create temporary file', function() {
expect($this->temp->exists())->toBeTrue();
});
or
beforeEach(function() {
$this->temp = $this->makeFile(0755);
});
it('create temporary file', function() {
expect($this->temp->exists())->toBeTrue();
});
You can output the data to a temporary file in the write or writeln method of TemporaryFile instance.
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
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;
});
Run with the following command.
composer test