/vendor/neutron/temporary-filesystem/tests/Neutron/TemporaryFilesystem/Tests/ManagerTest.php |
@@ -0,0 +1,301 @@ |
<?php |
|
namespace Neutron\TemporaryFilesystem\Tests; |
|
use Neutron\TemporaryFilesystem\Manager; |
|
class ManagerTest extends \PHPUnit_Framework_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->getMock('Neutron\TemporaryFilesystem\TemporaryFilesystemInterface'); |
} |
|
private function createFsMock() |
{ |
return $this |
->getMockBuilder('Symfony\Component\Filesystem\Filesystem') |
->disableOriginalConstructor() |
->getMock(); |
} |
} |
/vendor/neutron/temporary-filesystem/tests/Neutron/TemporaryFilesystem/Tests/TemporaryFilesystemTest.php |
@@ -0,0 +1,206 @@ |
<?php |
|
namespace Neutron\TemporaryFilesystem\Tests; |
|
use Neutron\TemporaryFilesystem\TemporaryFilesystem; |
use Symfony\Component\Filesystem\Filesystem; |
|
class TemporaryFilesystemTest extends \PHPUnit_Framework_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->assertContains('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/'), |
); |
} |
|
/** |
* @expectedException Neutron\TemporaryFilesystem\IOException |
*/ |
public function testCreateEmptyFileInvalidDir() |
{ |
$createDir = $this->workspace . DIRECTORY_SEPARATOR . 'invalid-book-dir'; |
|
$this->filesystem->createEmptyFile($createDir); |
} |
|
/** |
* @expectedException Neutron\TemporaryFilesystem\IOException |
*/ |
public function testCreateEmptyFileInvalidDirSecondMethod() |
{ |
$createDir = $this->workspace . DIRECTORY_SEPARATOR . 'invalid-book-dir'; |
|
$this->filesystem->createEmptyFile($createDir, 'romain', 'neutron'); |
} |
|
/** |
* @expectedException Neutron\TemporaryFilesystem\IOException |
*/ |
public function testCreateEmptyFileFails() |
{ |
$createDir = $this->workspace . DIRECTORY_SEPARATOR . 'book-dir'; |
mkdir($createDir); |
|
$this->filesystem->createEmptyFile($createDir, 'romain', 'neutron', null, 0); |
} |
|
/** |
* @expectedException Neutron\TemporaryFilesystem\IOException |
*/ |
public function testCreateEmptyFileOnFile() |
{ |
$createDir = $this->workspace . DIRECTORY_SEPARATOR . 'book-dir'; |
touch($createDir); |
|
$this->filesystem->createEmptyFile($createDir, null, null, null); |
} |
|
/** |
* @expectedException Neutron\TemporaryFilesystem\IOException |
*/ |
public function testCreateEmptyFileOnFileSecondMethod() |
{ |
$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->assertInternalType('string', $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)); |
} |
|
/** |
* @expectedException Neutron\TemporaryFilesystem\IOException |
*/ |
public function testTemporaryFilesFails() |
{ |
$this->filesystem->createTemporaryFiles(3, 'prefix', 'suffix', null, 0); |
} |
|
/** |
* @expectedException \InvalidArgumentException |
*/ |
public function testTemporaryFilesInvalidQuantity() |
{ |
$this->filesystem->createTemporaryFiles(0); |
} |
} |