lithium\test\Unit::_cleanUp()
Removes everything from resources/tmp/tests
directory. Call from inside of your test
method or tearDown()
.
Uses DIRECTORY_SEPARATOR
as getPathname()
is used in a a direct string comparison.
The method may contain slashes and backslashes.
If the file to unlink is readonly, it throws a exception (Permission denied) on Windows. So, the file is checked before an unlink is tried. (this will make the tests run slower but is prefered over a if (!unlink { chmod; unlink }. http://stringoftheseus.com/blog/2010/12/22/php-unlink-permisssion-denied-error-on-windows/
Parameters
-
string
$path
Path to directory with contents to remove. If first character is NOT a slash (
/
) or a Windows drive letter (C:
) prependsLibraries::get(true, 'resources')/tmp/
.
Returns
voidSource
protected function _cleanUp($path = null) {
$resources = Libraries::get(true, 'resources');
$path = $path ?: $resources . '/tmp/tests';
$path = preg_match('/^\w:|^\//', $path) ? $path : $resources . '/tmp/' . $path;
if (!is_dir($path)) {
return;
}
$dirs = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($dirs, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $item) {
$empty = $item->getPathname() === $path . DIRECTORY_SEPARATOR . 'empty';
if ($empty || $iterator->isDot()) {
continue;
}
if ($item->isDir()) {
rmdir($item->getPathname());
continue;
}
if (!$item->isWritable()) {
chmod($item->getPathname(), 0777);
}
unlink($item->getPathname());
}
}