This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
vendor
composer.phar
composer.lock
.phpunit.result.cache
/tests/phpunit_report/

View File

@@ -0,0 +1,49 @@
language: php
php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4
sudo: false
cache:
directories:
- $HOME/.composer
matrix:
include:
- php: 5.6
env: COMPOSER_FLAGS="--prefer-lowest"
SYMFONY_VERSION=2.*
- php: 5.6
env: SYMFONY_VERSION=2.3.*
- php: 5.6
env: SYMFONY_VERSION=2.7.*
- php: 7.0
env: SYMFONY_VERSION=2.8.*@dev
- php: 7.0
env: SYMFONY_VERSION=3.0.*
- php: 7.0
env: SYMFONY_VERSION=3.0.*@dev
- php: 7.1
env: SYMFONY_VERSION=4.*
- php: 7.2
env: SYMFONY_VERSION=5.*
- php: 7.3
env: SYMFONY_VERSION=5.*
- php: 7.4
env: SYMFONY_VERSION=5.*
before_install:
- composer self-update
- if [ "COMPOSER_FLAGS" == "--prefer-lowest" ]; then composer require "roave/security-advisories" dev-master --no-update; fi;
- if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi;
install: composer update --prefer-source $COMPOSER_FLAGS
script:
- vendor/bin/simple-phpunit

View File

@@ -0,0 +1,34 @@
CHANGELOG
=========
* 2.2.0 (2016-03-05)
* Support Symfony with all 2.x LTS versions and 3.0, add big travis matrix (@patkar)
* Update php unit to old stable (@patkar)
* Test with lowest (security release) version and php 5.3 (@patkar)
* Support PHP 5.6, 7.0, and HHVM and use docker with cache on travis (@patkar)
* Composer cleanup, ignore lock file (@patkar)
* 2.1.1 (2013-10-10)
* Add a factory for the Manager
* 2.1.0 (2013-08-06)
* Use custom IOException instead of Symfony one.
* Add a TemporaryFilesystemInterface.
* Add optional directory prefix to createTemporaryDirectory.
* Add temporary files manager.
* 2.0.1 (2013-04-09)
* Add TemporaryFilesystem::createTemporaryFile method
* 2.0.0 (2013-04-08)
* Switch from inheritance to composition design
* Add TemporaryFilesystem::create method
* Add TemporaryFilesystem::createTemporaryDirectory method
* 1.0.0 (2012-11-02)
* First tagged release

View File

@@ -0,0 +1,21 @@
TemporaryFilesystem is released with MIT License :
Copyright (c) 2012 Alchemy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.

View File

@@ -0,0 +1,72 @@
# TemporaryFilesystem
TemporaryFilesystem propose an API for temprary filesystem based on [Symfony
Filesystem Component](https://github.com/symfony/filesystem).
[![Build Status](https://travis-ci.org/romainneutron/Temporary-Filesystem.png?branch=master)](https://travis-ci.org/romainneutron/Temporary-Filesystem)
## Usage
```php
use Neutron\TemporaryFilesystem\TemporaryFilesystem;
$fs = TemporaryFilesystem::create();
```
## API Examples :
### CreateTemporaryDirectory
CreateTemporaryDirectory creates a temporary directory with an optional mode :
```php
$tempDir = $fs->createTemporaryDirectory($mode = 0755);
```
### CreateTemporaryFile
CreateTemporaryFile creates an empty files in the temporary folder:
```php
$fs->createTemporaryFile();
// return an empty temporary files with a "thumb-"
// prefix, '.dcm' as suffix and 'jpg' as extension
$fs->createTemporaryFile('thumb-', '.dcm', 'jpg');
```
### CreateTemporaryFiles
CreateTemporaryFiles creates a set of empty files in the temporary folder:
```php
// return an array of 5 path to temporary files
$fs->createTemporaryFiles(5);
// return an array of 5 path to empty temporary files with a "thumb-"
// prefix, '.dcm' as suffix and 'jpg' as extension
$fs->createTemporaryFiles(20, 'thumb-', '.dcm', 'jpg');
```
This method is useful when dealing with libraries which encode images
depending on the filename extension.
### CreateEmptyFile
CreateEmptyFile creates an empty file in the specified folder:
```php
// return a path to an empty file inside the current working directory
$fs->createEmptyFile(getcwd());
// return a path to an empty file in the "/home/romain" directory. The file
// has "original." as prefix, ".raw" as suffix and "CR2" as extension.
$fs->createEmptyFile("/home/romain", 'original.', '.raw', 'CR2');
```
This method is particularly useful when dealing with concurrent process
writing in the same directory.
# License
Released under the MIT license

View File

@@ -0,0 +1,23 @@
{
"name": "neutron/temporary-filesystem",
"description": "Symfony filesystem extension to handle temporary files",
"license": "MIT",
"authors": [
{
"name": "Romain Neutron",
"email": "imprec@gmail.com"
}
],
"require": {
"php": "^5.6 || ^7.0",
"symfony/filesystem": "^2.3 || ^3.0 || ^4.0 || ^5.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^5.0.4"
},
"autoload": {
"psr-0": {
"Neutron": "src"
}
}
}

View File

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
verbose="false"
bootstrap="tests/bootstrap.php"
>
<logging>
<log type="coverage-html"
target="tests/phpunit_report/report"
lowUpperBound="35" highLowerBound="70"/>
</logging>
<php>
<ini name="display_errors" value="on"/>
<env name="SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT" value="true"/>
</php>
<testsuites>
<testsuite name="TemporaryFilesystem tests Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>

View File

@@ -0,0 +1,16 @@
<?php
/*
* This file is part of TemporaryFilesystem.
*
* (c) Romain Neutron <imprec@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Neutron\TemporaryFilesystem;
class IOException extends \RuntimeException
{
}

View File

@@ -0,0 +1,156 @@
<?php
/*
* This file is part of TemporaryFilesystem.
*
* (c) Romain Neutron <imprec@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Neutron\TemporaryFilesystem;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException as SfIOException;
class Manager implements TemporaryFilesystemInterface
{
/** @var Filesystem */
private $fs;
/** @var TemporaryFilesystem */
private $tmpFs;
/** @var array */
private $files = array();
const DEFAULT_SCOPE = '_tmp_fs_';
public function __construct(TemporaryFilesystemInterface $tmpFs, Filesystem $fs)
{
$this->fs = $fs;
$this->tmpFs = $tmpFs;
register_shutdown_function(array($this, 'clean'), null, false);
}
/**
* {@inheritdoc}
*/
public function createEmptyFile($basePath, $prefix = self::DEFAULT_SCOPE, $suffix = null, $extension = null, $maxTry = 65536)
{
$file = $this->tmpFs->createEmptyFile($basePath, $prefix, $suffix, $extension, $maxTry);
$this->add($file, $prefix);
return $file;
}
/**
* {@inheritdoc}
*/
public function createTemporaryDirectory($mode = 0777, $maxTry = 65536, $prefix = self::DEFAULT_SCOPE)
{
$dir = $this->tmpFs->createTemporaryDirectory($mode, $maxTry, $prefix);
$this->add($dir, $prefix);
return $dir;
}
/**
* {@inheritdoc}
*/
public function createTemporaryFile($prefix = self::DEFAULT_SCOPE, $suffix = null, $extension = null, $maxTry = 65536)
{
$file = $this->tmpFs->createTemporaryFile($prefix, $suffix, $extension, $maxTry);
$this->add($file, $prefix);
return $file;
}
/**
* {@inheritdoc}
*/
public function createTemporaryFiles($quantity = 1, $prefix = self::DEFAULT_SCOPE, $suffix = null, $extension = null, $maxTry = 65536)
{
$files = $this->tmpFs->createTemporaryFiles($quantity, $prefix, $suffix, $extension, $maxTry);
$this->add($files, $prefix);
return $files;
}
/**
* Adds file to be handled by the manager.
*
* @param string|array $files
* @param string $scope
*
* @return Manager
*/
public function add($files, $scope = self::DEFAULT_SCOPE)
{
if (!is_array($files)) {
$files = array($files);
}
if ('' === trim($scope)) {
$scope = self::DEFAULT_SCOPE;
}
if (!isset($this->files[$scope])) {
$this->files[$scope] = array();
}
$this->files[$scope] = array_merge($this->files[$scope], $files);
return $this;
}
/**
* Removes all managed files in a scope. If no scope provided, all scopes
* are cleared.
*
* @param string $scope
*
* @return Manager
*
* @throws IOException
*/
public function clean($scope = null, $throwException = true)
{
if (null !== $scope) {
$this->cleanScope($scope, $throwException);
} else {
foreach ($this->files as $scope => $files) {
$this->cleanScope($scope, $throwException);
}
}
return $this;
}
/**
* Factory for the Manager
*
* @return Manager
*/
public static function create()
{
$fs = new Filesystem();
return new static(new TemporaryFilesystem($fs), $fs);
}
private function cleanScope($scope, $throwException)
{
if (!isset($this->files[$scope])) {
return;
}
try {
$this->fs->remove($this->files[$scope]);
unset($this->files[$scope]);
} catch (SfIOException $e) {
unset($this->files[$scope]);
if ($throwException) {
throw new IOException('Unable to remove all the files', $e->getCode(), $e);
}
}
}
}

View File

@@ -0,0 +1,132 @@
<?php
/*
* This file is part of TemporaryFilesystem.
*
* (c) Romain Neutron <imprec@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Neutron\TemporaryFilesystem;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException as SfIOException;
class TemporaryFilesystem implements TemporaryFilesystemInterface
{
/** @var Filesystem */
private $filesystem;
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}
/**
* {@inheritdoc}
*/
public function createTemporaryDirectory($mode = 0777, $maxTry = 65536, $prefix = null)
{
$basePath = sys_get_temp_dir();
// Remove trailing slashes if present
$basePath = rtrim($basePath, DIRECTORY_SEPARATOR);
while ($maxTry > 0) {
$dir = $basePath . DIRECTORY_SEPARATOR
. $prefix . base_convert(mt_rand(0x19A100, 0x39AA3FF), 10, 36);
if (false === file_exists($dir)) {
try {
$this->filesystem->mkdir($dir, $mode);
} catch (SfIOException $e) {
throw new IOException('Unable to make directory', $e->getCode(), $e);
}
return $dir;
}
$maxTry --;
}
throw new IOException('Unable to generate a temporary directory');
}
/**
* {@inheritdoc}
*/
public function createTemporaryFiles($quantity = 1, $prefix = null, $suffix = null, $extension = null, $maxTry = 65536)
{
if ($quantity < 1) {
throw new \InvalidArgumentException('Invalid temporary files quantity');
}
$files = array();
while ($quantity > 0) {
$files[] = $this->createEmptyFile(sys_get_temp_dir(), $prefix, $suffix, $extension, $maxTry);
$quantity --;
}
return $files;
}
/**
* {@inheritdoc}
*/
public function createTemporaryFile($prefix = null, $suffix = null, $extension = null, $maxTry = 65536)
{
$files = $this->createTemporaryFiles(1, $prefix, $suffix, $extension, $maxTry);
return array_pop($files);
}
/**
* {@inheritdoc}
*/
public function createEmptyFile($basePath, $prefix = null, $suffix = null, $extension = null, $maxTry = 65536)
{
if (false === is_dir($basePath) || false === is_writeable($basePath)) {
throw new IOException(sprintf('`%s` should be a writeable directory', $basePath));
}
if ($suffix === null && $extension === null) {
if (false === $file = @tempnam($basePath, $prefix)) {
throw new IOException('Unable to generate a temporary filename');
}
return $file;
}
while ($maxTry > 0) {
$file = $basePath . DIRECTORY_SEPARATOR
. $prefix . base_convert(mt_rand(0x19A100, 0x39AA3FF), 10, 36) . $suffix
. ( $extension ? '.' . $extension : '');
if (false === file_exists($file)) {
try {
$this->filesystem->touch($file);
} catch (SfIOException $e) {
throw new IOException('Unable to touch file', $e->getCode(), $e);
}
return $file;
}
$maxTry --;
}
throw new IOException('Unable to generate a temporary filename');
}
/**
* Creates a TemporaryFilesystem
*
* @return TemporaryFilesystem
*/
public static function create()
{
return new static(new Filesystem());
}
}

View File

@@ -0,0 +1,84 @@
<?php
/*
* This file is part of TemporaryFilesystem.
*
* (c) Romain Neutron <imprec@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Neutron\TemporaryFilesystem;
interface TemporaryFilesystemInterface
{
/**
* Creates a temporary directory.
*
* @param octal $mode The directory mode
* @param integer $maxTry The maximum number of trials
* @param string $prefix The directory prefix
*
* @return string The name of the created directory
*
* @throws IOException In case the directory could not be created
*/
public function createTemporaryDirectory($mode = 0777, $maxTry = 65536, $prefix = null);
/**
* Creates an array of temporary files.
*
* Temporary files are created inside the system temporary folder. You must
* removed them manually at the end of use.
*
* @param integer $quantity The quantity of temporary files requested
* @param string $prefix The prefix of the files
* @param string $suffix The suffix of the files
* @param string $extension The extension of the files
* @param integer $maxTry The maximum number of trials to create one temporary file
*
* @return array An array of filenames
*
* @throws \InvalidArgumentException In case you provide a wrong argument
* @throws IOException In case of failure
*/
public function createTemporaryFiles($quantity = 1, $prefix = null, $suffix = null, $extension = null, $maxTry = 65536);
/**
* Creates a temporary file.
*
* Temporary files are created inside the system temporary folder. You must
* removed them manually at the end of use.
*
* @param string $prefix The prefix of the files
* @param string $suffix The suffix of the files
* @param string $extension The extension of the files
* @param integer $maxTry The maximum number of trials to create one temporary file
*
* @return array An array of filenames
*
* @throws \InvalidArgumentException In case you provide a wrong argument
* @throws IOException In case of failure
*/
public function createTemporaryFile($prefix = null, $suffix = null, $extension = null, $maxTry = 65536);
/**
* Create an empty file in the specified directory.
*
* The new file is created in the requested directory and will fit the
* the given parameters. Please note that the filename contains some
* random caracters.
*
* @param string $basePath The directory where to create the file
* @param string $prefix The prefix of the file
* @param string $suffix The suffix of the file
* @param string $extension The extension of the file
* @param integer $maxTry The maximum number of trials to create the file
*
* @return string The path of the created file
*
* @throws IOException in case of failure
*/
public function createEmptyFile($basePath, $prefix = null, $suffix = null, $extension = null, $maxTry = 65536);
}

View File

@@ -0,0 +1,302 @@
<?php
namespace Neutron\TemporaryFilesystem\Tests;
use Neutron\TemporaryFilesystem\Manager;
use PHPUnit\Framework\TestCase;
class ManagerTest extends TestCase
{
public function testCreateEmptyFileAndCleanScope()
{
$basePath = 'basePath';
$prefix = 'prefix';
$suffix = 'suffix';
$extension = 'extension';
$maxTry = 'maxtry';
$fs = $this->createFsMock();
$fs->expects($this->once())
->method('remove')
->with(array('/path/to/empty/file'));
$tmpFs = $this->createTmpFsMock();
$tmpFs->expects($this->once())
->method('createEmptyFile')
->with($basePath, $prefix, $suffix, $extension, $maxTry)
->will($this->returnValue('/path/to/empty/file'));
$manager = new Manager($tmpFs, $fs);
$this->assertEquals('/path/to/empty/file', $manager->createEmptyFile($basePath, $prefix, $suffix, $extension, $maxTry));
$manager->clean($prefix);
}
public function testCreateEmptyFileAndCleanOtherScope()
{
$basePath = 'basePath';
$prefix = 'prefix';
$suffix = 'suffix';
$extension = 'extension';
$maxTry = 'maxtry';
$fs = $this->createFsMock();
$fs->expects($this->never())
->method('remove');
$tmpFs = $this->createTmpFsMock();
$tmpFs->expects($this->once())
->method('createEmptyFile')
->with($basePath, $prefix, $suffix, $extension, $maxTry)
->will($this->returnValue('/path/to/empty/file'));
$manager = new Manager($tmpFs, $fs);
$this->assertEquals('/path/to/empty/file', $manager->createEmptyFile($basePath, $prefix, $suffix, $extension, $maxTry));
$manager->clean('other prefix');
}
public function testCreateEmptyFileAndCleanScopes()
{
$basePath = 'basePath';
$prefix = 'prefix';
$suffix = 'suffix';
$extension = 'extension';
$maxTry = 'maxtry';
$fs = $this->createFsMock();
$fs->expects($this->once())
->method('remove')
->with(array('/path/to/empty/file'));
$tmpFs = $this->createTmpFsMock();
$tmpFs->expects($this->once())
->method('createEmptyFile')
->with($basePath, $prefix, $suffix, $extension, $maxTry)
->will($this->returnValue('/path/to/empty/file'));
$manager = new Manager($tmpFs, $fs);
$this->assertEquals('/path/to/empty/file', $manager->createEmptyFile($basePath, $prefix, $suffix, $extension, $maxTry));
$manager->clean();
}
public function testCreateTemporaryDirectoryAndCleanScope()
{
$mode = 'mode';
$prefix = 'prefix';
$maxTry = 'maxtry';
$fs = $this->createFsMock();
$fs->expects($this->once())
->method('remove')
->with(array('/path/to/dir'));
$tmpFs = $this->createTmpFsMock();
$tmpFs->expects($this->once())
->method('createTemporaryDirectory')
->with($mode, $maxTry, $prefix)
->will($this->returnValue('/path/to/dir'));
$manager = new Manager($tmpFs, $fs);
$this->assertEquals('/path/to/dir', $manager->createTemporaryDirectory($mode, $maxTry, $prefix));
$manager->clean($prefix);
}
public function testCreateTemporaryDirectoryAndCleanOtherScope()
{
$mode = 'mode';
$prefix = 'prefix';
$maxTry = 'maxtry';
$fs = $this->createFsMock();
$fs->expects($this->never())
->method('remove');
$tmpFs = $this->createTmpFsMock();
$tmpFs->expects($this->once())
->method('createTemporaryDirectory')
->with($mode, $maxTry, $prefix)
->will($this->returnValue('/path/to/dir'));
$manager = new Manager($tmpFs, $fs);
$this->assertEquals('/path/to/dir', $manager->createTemporaryDirectory($mode, $maxTry, $prefix));
$manager->clean('other prefix');
}
public function testCreateTemporaryDirectoryAndCleanScopes()
{
$mode = 'mode';
$prefix = 'prefix';
$maxTry = 'maxtry';
$fs = $this->createFsMock();
$fs->expects($this->once())
->method('remove')
->with(array('/path/to/dir'));
$tmpFs = $this->createTmpFsMock();
$tmpFs->expects($this->once())
->method('createTemporaryDirectory')
->with($mode, $maxTry, $prefix)
->will($this->returnValue('/path/to/dir'));
$manager = new Manager($tmpFs, $fs);
$this->assertEquals('/path/to/dir', $manager->createTemporaryDirectory($mode, $maxTry, $prefix));
$manager->clean();
}
public function testCreateTemporaryFileAndCleanScope()
{
$prefix = 'prefix';
$suffix = 'suffix';
$extension = 'extension';
$maxTry = 'maxtry';
$fs = $this->createFsMock();
$fs->expects($this->once())
->method('remove')
->with(array('/path/to/file'));
$tmpFs = $this->createTmpFsMock();
$tmpFs->expects($this->once())
->method('createTemporaryFile')
->with($prefix, $suffix, $extension, $maxTry)
->will($this->returnValue('/path/to/file'));
$manager = new Manager($tmpFs, $fs);
$this->assertEquals('/path/to/file', $manager->createTemporaryFile($prefix, $suffix, $extension, $maxTry));
$manager->clean($prefix);
}
public function testCreateTemporaryFileAndCleanOtherScope()
{
$prefix = 'prefix';
$suffix = 'suffix';
$extension = 'extension';
$maxTry = 'maxtry';
$fs = $this->createFsMock();
$fs->expects($this->never())
->method('remove');
$tmpFs = $this->createTmpFsMock();
$tmpFs->expects($this->once())
->method('createTemporaryFile')
->with($prefix, $suffix, $extension, $maxTry)
->will($this->returnValue('/path/to/file'));
$manager = new Manager($tmpFs, $fs);
$this->assertEquals('/path/to/file', $manager->createTemporaryFile($prefix, $suffix, $extension, $maxTry));
$manager->clean('other prefix');
}
public function testCreateTemporaryFileAndCleanScopes()
{
$prefix = 'prefix';
$suffix = 'suffix';
$extension = 'extension';
$maxTry = 'maxtry';
$fs = $this->createFsMock();
$fs->expects($this->once())
->method('remove')
->with(array('/path/to/file'));
$tmpFs = $this->createTmpFsMock();
$tmpFs->expects($this->once())
->method('createTemporaryFile')
->with($prefix, $suffix, $extension, $maxTry)
->will($this->returnValue('/path/to/file'));
$manager = new Manager($tmpFs, $fs);
$this->assertEquals('/path/to/file', $manager->createTemporaryFile($prefix, $suffix, $extension, $maxTry));
$manager->clean();
}
public function testCreateTemporaryFilesAndCleanScope()
{
$prefix = 'prefix';
$suffix = 'suffix';
$extension = 'extension';
$maxTry = 'maxtry';
$quantity = 123;
$fs = $this->createFsMock();
$fs->expects($this->once())
->method('remove')
->with(array('/path/to/file'));
$tmpFs = $this->createTmpFsMock();
$tmpFs->expects($this->once())
->method('createTemporaryFiles')
->with($quantity, $prefix, $suffix, $extension, $maxTry)
->will($this->returnValue(array('/path/to/file')));
$manager = new Manager($tmpFs, $fs);
$this->assertEquals(array('/path/to/file'), $manager->createTemporaryFiles($quantity, $prefix, $suffix, $extension, $maxTry));
$manager->clean($prefix);
}
public function testCreateTemporaryFilesAndCleanOtherScope()
{
$prefix = 'prefix';
$suffix = 'suffix';
$extension = 'extension';
$maxTry = 'maxtry';
$quantity = 123;
$fs = $this->createFsMock();
$fs->expects($this->never())
->method('remove');
$tmpFs = $this->createTmpFsMock();
$tmpFs->expects($this->once())
->method('createTemporaryFiles')
->with($quantity, $prefix, $suffix, $extension, $maxTry)
->will($this->returnValue(array('/path/to/file')));
$manager = new Manager($tmpFs, $fs);
$this->assertEquals(array('/path/to/file'), $manager->createTemporaryFiles($quantity, $prefix, $suffix, $extension, $maxTry));
$manager->clean('other prefix');
}
public function testCreateTemporaryFilesAndCleanScopes()
{
$prefix = 'prefix';
$suffix = 'suffix';
$extension = 'extension';
$maxTry = 'maxtry';
$quantity = 123;
$fs = $this->createFsMock();
$fs->expects($this->once())
->method('remove')
->with(array('/path/to/file'));
$tmpFs = $this->createTmpFsMock();
$tmpFs->expects($this->once())
->method('createTemporaryFiles')
->with($quantity, $prefix, $suffix, $extension, $maxTry)
->will($this->returnValue(array('/path/to/file')));
$manager = new Manager($tmpFs, $fs);
$this->assertEquals(array('/path/to/file'), $manager->createTemporaryFiles($quantity, $prefix, $suffix, $extension, $maxTry));
$manager->clean();
}
public function testCreate()
{
$this->assertInstanceOf('Neutron\TemporaryFilesystem\Manager', Manager::create());
}
private function createTmpFsMock()
{
return $this->getMockBuilder('Neutron\TemporaryFilesystem\TemporaryFilesystemInterface')->getMock();
}
private function createFsMock()
{
return $this
->getMockBuilder('Symfony\Component\Filesystem\Filesystem')
->disableOriginalConstructor()
->getMock();
}
}

View File

@@ -0,0 +1,201 @@
<?php
namespace Neutron\TemporaryFilesystem\Tests;
use Neutron\TemporaryFilesystem\IOException;
use Neutron\TemporaryFilesystem\TemporaryFilesystem;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
class TemporaryFilesystemTest extends TestCase
{
/**
* @var string $workspace
*/
private $workspace = null;
/**
* @var TemporaryFilesystem
*/
private $filesystem;
public function setUp()
{
parent::setUp();
$this->workspace = sys_get_temp_dir().DIRECTORY_SEPARATOR.time().rand(0, 1000);
mkdir($this->workspace, 0777, true);
$this->workspace = realpath($this->workspace);
$this->filesystem = TemporaryFilesystem::create();
}
public function tearDown()
{
$this->clean($this->workspace);
}
public function testCreate()
{
$this->assertInstanceOf('Neutron\TemporaryFilesystem\TemporaryFilesystem', TemporaryFilesystem::create());
}
public function testConctruct()
{
$this->assertInstanceOf('Neutron\TemporaryFilesystem\TemporaryFilesystem', new TemporaryFilesystem(new Filesystem()));
}
/**
* @param string $file
*/
private function clean($file)
{
if (is_dir($file) && !is_link($file)) {
$dir = new \FilesystemIterator($file);
foreach ($dir as $childFile) {
$this->clean($childFile);
}
rmdir($file);
} else {
unlink($file);
}
}
/**
* @dataProvider provideFilesToCreate
*/
public function testCreateEmptyFile($prefix, $suffix, $extension, $maxTry, $pattern)
{
$createDir = $this->workspace . DIRECTORY_SEPARATOR . 'book-dir';
mkdir($createDir);
$file = $this->filesystem->createEmptyFile($createDir, $prefix, $suffix, $extension, $maxTry);
$this->assertTrue(file_exists($file));
$this->assertEquals($createDir, dirname($file));
$this->assertEquals(0, filesize($file));
$this->assertRegExp($pattern, basename($file));
unlink($file);
}
public function testCreateTemporaryDir()
{
$dir = $this->filesystem->createTemporaryDirectory();
$this->assertTrue(file_exists($dir));
$this->assertTrue(is_dir($dir));
rmdir($dir);
}
public function testCreateTemporaryDirWithPrefix()
{
$dir = $this->filesystem->createTemporaryDirectory(0777, 200, 'neutron');
$this->assertTrue(file_exists($dir));
$this->assertTrue(is_dir($dir));
$this->assertStringContainsString('neutron', $dir);
rmdir($dir);
}
public function provideFilesToCreate()
{
return array(
array(null, null, null, 10, '/\w{5}/'),
array('romain', null, null, 10, '/romain\w{5}/'),
array(null, 'neutron', null, 10, '/\w{5}neutron/'),
array(null, null, 'io', 10, '/\w{5}\.io/'),
array('romain', null, 'io', 10, '/romain\w{5}\.io/'),
array(null, 'neutron', 'io', 10, '/\w{5}neutron\.io/'),
array('romain', 'neutron', 'io', 10, '/romain\w{5}neutron\.io/'),
);
}
public function testCreateEmptyFileInvalidDir()
{
$this->expectException(IOException::class);
$createDir = $this->workspace . DIRECTORY_SEPARATOR . 'invalid-book-dir';
$this->filesystem->createEmptyFile($createDir);
}
public function testCreateEmptyFileInvalidDirSecondMethod()
{
$this->expectException(IOException::class);
$createDir = $this->workspace . DIRECTORY_SEPARATOR . 'invalid-book-dir';
$this->filesystem->createEmptyFile($createDir, 'romain', 'neutron');
}
public function testCreateEmptyFileFails()
{
$this->expectException(IOException::class);
$createDir = $this->workspace . DIRECTORY_SEPARATOR . 'book-dir';
mkdir($createDir);
$this->filesystem->createEmptyFile($createDir, 'romain', 'neutron', null, 0);
}
public function testCreateEmptyFileOnFile()
{
$this->expectException(IOException::class);
$createDir = $this->workspace . DIRECTORY_SEPARATOR . 'book-dir';
touch($createDir);
$this->filesystem->createEmptyFile($createDir, null, null, null);
}
public function testCreateEmptyFileOnFileSecondMethod()
{
$this->expectException(IOException::class);
$createDir = $this->workspace . DIRECTORY_SEPARATOR . 'book-dir';
touch($createDir);
$this->filesystem->createEmptyFile($createDir, 'romain', 'neutron', 'io');
}
/**
* @dataProvider provideFilesToCreate
*/
public function testTemporaryFiles($prefix, $suffix, $extension, $maxTry, $pattern)
{
$files = $this->filesystem->createTemporaryFiles(3, $prefix, $suffix, $extension, $maxTry);
$this->assertEquals(3, count($files));
foreach ($files as $file) {
$this->assertTrue(file_exists($file));
$this->assertEquals(realpath(sys_get_temp_dir()), realpath(dirname($file)));
$this->assertEquals(0, filesize($file));
$this->assertRegExp($pattern, basename($file));
}
}
/**
* @dataProvider provideFilesToCreate
*/
public function testTemporaryFile($prefix, $suffix, $extension, $maxTry, $pattern)
{
$file = $this->filesystem->createTemporaryFile($prefix, $suffix, $extension, $maxTry);
$this->assertIsString($file);
$this->assertTrue(file_exists($file));
$this->assertEquals(realpath(sys_get_temp_dir()), realpath(dirname($file)));
$this->assertEquals(0, filesize($file));
$this->assertRegExp($pattern, basename($file));
}
public function testTemporaryFilesFails()
{
$this->expectException(IOException::class);
$this->filesystem->createTemporaryFiles(3, 'prefix', 'suffix', null, 0);
}
public function testTemporaryFilesInvalidQuantity()
{
$this->expectException(\InvalidArgumentException::class);
$this->filesystem->createTemporaryFiles(0);
}
}

View File

@@ -0,0 +1,4 @@
<?php
$loader = require __DIR__ . '/../vendor/autoload.php';
$loader->add('Neutron\TemporaryFilesystem\Tests', __DIR__ . '/../tests');