Skip to content

Commit

Permalink
allow Folder to merge recursivly and add scheme option
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed Sep 10, 2012
1 parent 99b798f commit 3706853
Show file tree
Hide file tree
Showing 2 changed files with 285 additions and 62 deletions.
302 changes: 246 additions & 56 deletions lib/Cake/Test/Case/Utility/FolderTest.php
Expand Up @@ -29,11 +29,16 @@ class FolderTest extends CakeTestCase {
protected static $_tmp = array();

/**
* Save the directory names in TMP
* Save the directory names in TMP and make sure default directories exist
*
* @return void
*/
public static function setUpBeforeClass() {
$dirs = array('cache', 'logs', 'sessions', 'tests');
foreach ($dirs as $dir) {
new Folder(TMP . $dir, true);
}

foreach (scandir(TMP) as $file) {
if (is_dir(TMP . $file) && !in_array($file, array('.', '..'))) {
self::$_tmp[] = $file;
Expand Down Expand Up @@ -803,62 +808,258 @@ public function testDelete() {
/**
* testCopy method
*
* Verify that subdirectories existing in both destination and source directory
* are merged recursivly.
*
*/
public function testCopy() {
extract($this->_setupFilesystem());

$Folder = new Folder($folderOne);
$result = $Folder->copy($folderThree);
$this->assertTrue($result);
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));

$Folder = new Folder($folderTwo);
$result = $Folder->copy($folderThree);
$this->assertTrue($result);
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
$this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
$this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));

$Folder = new Folder($path);
$Folder->delete();
}

/**
* testCopyWithMerge method
*
* Verify that subdirectories existing in both destination and source directory
* are merged recursivly.
*
*/
public function testCopyWithMerge() {
extract($this->_setupFilesystem());

$Folder = new Folder($folderOne);
$result = $Folder->copy($folderThree);
$this->assertTrue($result);
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));

$Folder = new Folder($folderTwo);
$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::MERGE));
$this->assertTrue($result);
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
$this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
$this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));

$Folder = new Folder($path);
$Folder->delete();
}

/**
* testCopyWithSkip method
*
* Verify that directories and files are copied recursively
* even if the destination directory already exists.
* Subdirectories existing in both destination and source directory
* are skipped and not merged or overwritten.
*
* @return void
*/
public function testCopy() {
public function testCopyWithSkip() {
extract($this->_setupFilesystem());

$Folder = new Folder($folderOne);
$result = $Folder->copy(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
$this->assertTrue($result);
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
$this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));

$Folder = new Folder($folderTwo);
$Folder->delete();

$Folder = new Folder($folderOne);
$result = $Folder->copy(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
$this->assertTrue($result);
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
$this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));

$Folder = new Folder($folderTwo);
$Folder->delete();

new Folder($folderTwo, true);
new Folder($folderTwo . DS . 'folderB', true);
file_put_contents($folderTwo . DS . 'file2.php', 'touched');
file_put_contents($folderTwo . DS . 'folderB' . DS . 'fileB.php', 'untouched');

$Folder = new Folder($folderTwo);
$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::SKIP));
$this->assertTrue($result);
$this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
$this->assertEquals('touched', file_get_contents($folderThree . DS . 'file2.php'));
$this->assertEquals('untouched', file_get_contents($folderThree . DS . 'folderB' . DS . 'fileB.php'));

$Folder = new Folder($path);
$Folder->delete();
}

/**
* testCopyWithOverwrite
*
* Verify that subdirectories existing in both destination and source directory
* are overwritten/replaced recursivly.
*
* $path: folder_test/
* $folderOne: folder_test/folder1/
* - file1.php
* $folderTwo: folder_test/folder2/
* - file2.php
* $folderThree: folder_test/folder1/folder3/
* - file3.php
* $folderFour: folder_test/folder2/folder4/
* - file4.php
* $folderThree: folder_test/folder5/
*/
function testCopyWithOverwrite() {
extract($this->_setupFilesystem());

$Folder = new Folder($folderOne);
$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));

$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));

$Folder = new Folder($folderTwo);
$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));
$this->assertTrue($result);

$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));

$Folder = new Folder($folderOne);
unlink($fileOneA);
$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));
$this->assertTrue($result);

$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
$this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
$this->assertTrue(!file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
$this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));

$Folder = new Folder($path);
$Folder->delete();
}

/**
* Setup filesystem for copy tests
*
* @return void
*/
protected function _setupFilesystem() {
$path = TMP . 'folder_test';

$folderOne = $path . DS . 'folder1';
$folderTwo = $folderOne . DS . 'folder2';
$folderOneA = $folderOne . DS . 'folderA';
$folderTwo = $path . DS . 'folder2';
$folderTwoB = $folderTwo . DS . 'folderB';
$folderThree = $path . DS . 'folder3';

$fileOne = $folderOne . DS . 'file1.php';
$fileTwo = $folderTwo . DS . 'file2.php';
$fileOneA = $folderOneA . DS . 'fileA.php';
$fileTwoB = $folderTwoB . DS . 'fileB.php';

new Folder($path, true);
new Folder($folderOne, true);
new Folder($folderOneA, true);
new Folder($folderTwo, true);
new Folder($folderTwoB, true);
new Folder($folderThree, true);
touch($fileOne);
touch($fileTwo);
touch($fileOneA);
touch($fileTwoB);

return compact(
'path',
'folderOne', 'folderOneA', 'folderTwo', 'folderTwoB', 'folderThree',
'fileOne', 'fileOneA', 'fileTwo', 'fileTwoB');
}

/**
* testMove method
*
* Verify that directories and files are moved recursively
* even if the destination directory already exists.
* Subdirectories existing in both destination and source directory
* are merged recursivly.
*
* @return void
*/
public function testMove() {
extract($this->_setupFilesystem());

$Folder = new Folder($folderOne);
$result = $Folder->copy($folderThree);
$result = $Folder->move($folderTwo);
$this->assertTrue($result);
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
$this->assertTrue(file_exists($folderThree . DS . 'folder2' . DS . 'file2.php'));
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
$this->assertTrue(is_dir($folderTwo . DS . 'folderB'));
$this->assertTrue(file_exists($folderTwo . DS . 'folderB' . DS . 'fileB.php'));
$this->assertFalse(file_exists($fileOne));
$this->assertTrue(file_exists($folderTwo . DS . 'folderA'));
$this->assertFalse(file_exists($folderOneA));
$this->assertFalse(file_exists($fileOneA));

$Folder = new Folder($folderThree);
$Folder = new Folder($folderTwo);
$Folder->delete();

new Folder($folderOne, true);
new Folder($folderOneA, true);
touch($fileOne);
touch($fileOneA);

$Folder = new Folder($folderOne);
$result = $Folder->copy($folderThree);
$result = $Folder->move($folderTwo);
$this->assertTrue($result);
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
$this->assertTrue(file_exists($folderThree . DS . 'folder2' . DS . 'file2.php'));
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
$this->assertTrue(is_dir($folderTwo . DS . 'folderA'));
$this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
$this->assertFalse(file_exists($fileOne));
$this->assertFalse(file_exists($folderOneA));
$this->assertFalse(file_exists($fileOneA));

$Folder = new Folder($folderThree);
$Folder = new Folder($folderTwo);
$Folder->delete();

new Folder($folderThree, true);
new Folder($folderThree . DS . 'folder2', true);
file_put_contents($folderThree . DS . 'folder2' . DS . 'file2.php', 'untouched');
new Folder($folderOne, true);
new Folder($folderOneA, true);
new Folder($folderTwo, true);
new Folder($folderTwoB, true);
touch($fileOne);
touch($fileOneA);
new Folder($folderOne . DS . 'folderB', true);
touch($folderOne . DS . 'folderB' . DS . 'fileB.php');
file_put_contents($folderTwoB . DS . 'fileB.php', 'untouched');

$Folder = new Folder($folderOne);
$result = $Folder->copy($folderThree);
$result = $Folder->move($folderTwo);
$this->assertTrue($result);
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
$this->assertEquals('untouched', file_get_contents($folderThree . DS . 'folder2' . DS . 'file2.php'));
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
$this->assertEquals('', file_get_contents($folderTwoB . DS . 'fileB.php'));
$this->assertFalse(file_exists($fileOne));
$this->assertFalse(file_exists($folderOneA));
$this->assertFalse(file_exists($fileOneA));

$Folder = new Folder($path);
$Folder->delete();
}

/**
* testMove method
* testMoveWithSkip method
*
* Verify that directories and files are moved recursively
* even if the destination directory already exists.
Expand All @@ -867,68 +1068,57 @@ public function testCopy() {
*
* @return void
*/
public function testMove() {
$path = TMP . 'folder_test';
$folderOne = $path . DS . 'folder1';
$folderTwo = $folderOne . DS . 'folder2';
$folderThree = $path . DS . 'folder3';
$fileOne = $folderOne . DS . 'file1.php';
$fileTwo = $folderTwo . DS . 'file2.php';

new Folder($path, true);
new Folder($folderOne, true);
new Folder($folderTwo, true);
new Folder($folderThree, true);
touch($fileOne);
touch($fileTwo);
public function testMoveWithSkip() {
extract($this->_setupFilesystem());

$Folder = new Folder($folderOne);
$result = $Folder->move($folderThree);
$result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
$this->assertTrue($result);
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
$this->assertTrue(is_dir($folderThree . DS . 'folder2'));
$this->assertTrue(file_exists($folderThree . DS . 'folder2' . DS . 'file2.php'));
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
$this->assertTrue(is_dir($folderTwo . DS . 'folderB'));
$this->assertTrue(file_exists($folderTwoB . DS . 'fileB.php'));
$this->assertFalse(file_exists($fileOne));
$this->assertFalse(file_exists($folderTwo));
$this->assertFalse(file_exists($fileTwo));
$this->assertFalse(file_exists($folderOneA));
$this->assertFalse(file_exists($fileOneA));

$Folder = new Folder($folderThree);
$Folder = new Folder($folderTwo);
$Folder->delete();

new Folder($folderOne, true);
new Folder($folderOneA, true);
new Folder($folderTwo, true);
touch($fileOne);
touch($fileTwo);
touch($fileOneA);

$Folder = new Folder($folderOne);
$result = $Folder->move($folderThree);
$result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
$this->assertTrue($result);
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
$this->assertTrue(is_dir($folderThree . DS . 'folder2'));
$this->assertTrue(file_exists($folderThree . DS . 'folder2' . DS . 'file2.php'));
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
$this->assertTrue(is_dir($folderTwo . DS . 'folderA'));
$this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
$this->assertFalse(file_exists($fileOne));
$this->assertFalse(file_exists($folderTwo));
$this->assertFalse(file_exists($fileTwo));
$this->assertFalse(file_exists($folderOneA));
$this->assertFalse(file_exists($fileOneA));

$Folder = new Folder($folderThree);
$Folder = new Folder($folderTwo);
$Folder->delete();

new Folder($folderOne, true);
new Folder($folderOneA, true);
new Folder($folderTwo, true);
new Folder($folderThree, true);
new Folder($folderThree . DS . 'folder2', true);
new Folder($folderTwoB, true);
touch($fileOne);
touch($fileTwo);
file_put_contents($folderThree . DS . 'folder2' . DS . 'file2.php', 'untouched');
touch($fileOneA);
file_put_contents($folderTwoB . DS . 'fileB.php', 'untouched');

$Folder = new Folder($folderOne);
$result = $Folder->move($folderThree);
$result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
$this->assertTrue($result);
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
$this->assertEquals('untouched', file_get_contents($folderThree . DS . 'folder2' . DS . 'file2.php'));
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
$this->assertEquals('untouched', file_get_contents($folderTwoB . DS . 'fileB.php'));
$this->assertFalse(file_exists($fileOne));
$this->assertFalse(file_exists($folderTwo));
$this->assertFalse(file_exists($fileTwo));
$this->assertFalse(file_exists($folderOneA));
$this->assertFalse(file_exists($fileOneA));

$Folder = new Folder($path);
$Folder->delete();
Expand Down

0 comments on commit 3706853

Please sign in to comment.