Skip to content

Commit

Permalink
[Filesystem] added exists method
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Brochet committed Jun 16, 2012
1 parent 7c91ee5 commit 38cad9d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
18 changes: 18 additions & 0 deletions src/Symfony/Component/Filesystem/Filesystem.php
Expand Up @@ -66,6 +66,24 @@ public function mkdir($dirs, $mode = 0777)
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.
*
Expand Down
60 changes: 55 additions & 5 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Expand Up @@ -313,6 +313,56 @@ public function testRemoveCleansInvalidLinks()
$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();
Expand Down Expand Up @@ -421,18 +471,18 @@ public function testSymlink()
$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));
}

Expand Down

0 comments on commit 38cad9d

Please sign in to comment.