[Filesystem] added exists method

This commit is contained in:
Julien Brochet 2012-06-15 18:09:23 +02:00
parent 7c91ee5755
commit 38cad9d415
2 changed files with 73 additions and 5 deletions

View File

@ -66,6 +66,24 @@ class Filesystem
return $ret;
}
/**
* Checks the existence of files or directories.
*
* @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to check
*
* @return Boolean true if the file exists, false otherwise
*/
public function exists($files)
{
foreach ($this->toIterator($files) as $file) {
if (!file_exists($file)) {
return false;
}
}
return true;
}
/**
* Creates empty files.
*

View File

@ -313,6 +313,56 @@ class FilesystemTest extends \PHPUnit_Framework_TestCase
$this->assertTrue(!is_dir($basePath));
}
public function testFilesExists()
{
$basePath = $this->workspace.DIRECTORY_SEPARATOR.'directory'.DIRECTORY_SEPARATOR;
mkdir($basePath);
touch($basePath.'file1');
mkdir($basePath.'folder');
$this->assertTrue($this->filesystem->exists($basePath.'file1'));
$this->assertTrue($this->filesystem->exists($basePath.'folder'));
}
public function testFilesExistsTraversableObjectOfFilesAndDirectories()
{
$basePath = $this->workspace.DIRECTORY_SEPARATOR;
mkdir($basePath.'dir');
touch($basePath.'file');
$files = new \ArrayObject(array(
$basePath.'dir', $basePath.'file'
));
$this->assertTrue($this->filesystem->exists($files));
}
public function testFilesNotExistsTraversableObjectOfFilesAndDirectories()
{
$basePath = $this->workspace.DIRECTORY_SEPARATOR;
mkdir($basePath.'dir');
touch($basePath.'file');
touch($basePath.'file2');
$files = new \ArrayObject(array(
$basePath.'dir', $basePath.'file', $basePath.'file2'
));
unlink($basePath.'file');
$this->assertFalse($this->filesystem->exists($files));
}
public function testInvalidFileNotExists()
{
$basePath = $this->workspace.DIRECTORY_SEPARATOR.'directory'.DIRECTORY_SEPARATOR;
$this->assertFalse($this->filesystem->exists($basePath.time()));
}
public function testChmodChangesFileMode()
{
$this->markAsSkippedIfChmodIsMissing();
@ -421,18 +471,18 @@ class FilesystemTest extends \PHPUnit_Framework_TestCase
$this->assertTrue(is_link($link));
$this->assertEquals($file, readlink($link));
}
/**
* @depends testSymlink
* @depends testSymlink
*/
public function testRemoveSymlink()
{
$this->markAsSkippedIfSymlinkIsMissing();
$link = $this->workspace.DIRECTORY_SEPARATOR.'link';
$this->filesystem->remove($link);
$this->assertTrue(!is_link($link));
}