Skip to content

Instantly share code, notes, and snippets.

@apinstein
Created December 29, 2009 19:39
Show Gist options
  • Save apinstein/265527 to your computer and use it in GitHub Desktop.
Save apinstein/265527 to your computer and use it in GitHub Desktop.
vfsStream unit test demo
require_once('vfsStream/vfsStream.php');
vfsStreamWrapper::register();
makeTree(array(
'root/file.php',
'root/a/a.php',
'root/a/b/b.php',
'root/a/b/c/c.php',
'root/b/',
'root/c/d/',
));
showTree('root');
function makeTree($fileArray, $root = 'root')
{
vfsStreamWrapper::setRoot(new vfsStreamDirectory($root));
foreach ($fileArray as $file) {
$vfsFile = vfsStream::url($file);
$vfsFileDir = vfsStream::url(dirname($file));
// containing dir
if (!file_exists($vfsFileDir))
{
$ok = mkdir($vfsFileDir, 0755, true);
if (!$ok) die("mkdir $vfsFileDir failed");
}
// item
if (substr($file, -1) === '/')
{
// dir
if (!file_exists($vfsFile))
{
$ok = mkdir($vfsFile);
if (!$ok) die("mkdir $file failed");
}
}
else
{
// file
$vfsFileUrl = vfsStream::url($file);
// touch doesn't work with vfs yet
$f = fopen($vfsFileUrl, 'r');
fclose($f);
}
}
}
function showTree($path)
{
// dump path just made
print "ls $path:\n";
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(vfsStream::url($path)), RecursiveIteratorIterator::SELF_FIRST) as $item) {
print $item->getPathname();
if ($item->isDir())
{
print "/";
}
print "\n";
}
print "DONE\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment