Commit 539864f4 by Klimov Paul

"FileHelperTest" has been refactored.

parent dfda6663
...@@ -47,20 +47,42 @@ class FileHelperTest extends TestCase ...@@ -47,20 +47,42 @@ class FileHelperTest extends TestCase
} }
} }
/**
* Creates test files structure,
* @param array $items file system objects to be created in format: objectName => objectContent
* Arrays specifies directories, other values - files.
* @param string $basePath structure base file path.
*/
protected function createFileStructure(array $items, $basePath = '') {
if (empty($basePath)) {
$basePath = $this->testFilePath;
}
foreach ($items as $name => $content) {
$itemName = $basePath . DIRECTORY_SEPARATOR . $name;
if (is_array($content)) {
mkdir($itemName, 0777, true);
$this->createFileStructure($content, $itemName);
} else {
file_put_contents($itemName, $content);
}
}
}
// Tests : // Tests :
public function testCopyDirectory() public function testCopyDirectory()
{ {
$basePath = $this->testFilePath; $srcDirName = 'test_src_dir';
$srcDirName = $basePath . DIRECTORY_SEPARATOR . 'test_src_dir';
mkdir($srcDirName, 0777, true);
$files = array( $files = array(
'file1.txt' => 'file 1 content', 'file1.txt' => 'file 1 content',
'file2.txt' => 'file 2 content', 'file2.txt' => 'file 2 content',
); );
foreach ($files as $name => $content) { $this->createFileStructure(array(
file_put_contents($srcDirName . DIRECTORY_SEPARATOR . $name, $content); $srcDirName => $files
} ));
$basePath = $this->testFilePath;
$srcDirName = $basePath . DIRECTORY_SEPARATOR . $srcDirName;
$dstDirName = $basePath . DIRECTORY_SEPARATOR . 'test_dst_dir'; $dstDirName = $basePath . DIRECTORY_SEPARATOR . 'test_dst_dir';
FileHelper::copyDirectory($srcDirName, $dstDirName); FileHelper::copyDirectory($srcDirName, $dstDirName);
...@@ -75,21 +97,20 @@ class FileHelperTest extends TestCase ...@@ -75,21 +97,20 @@ class FileHelperTest extends TestCase
public function testRemoveDirectory() public function testRemoveDirectory()
{ {
$basePath = $this->testFilePath; $dirName = 'test_dir_for_remove';
$dirName = $basePath . DIRECTORY_SEPARATOR . 'test_dir_for_remove'; $this->createFileStructure(array(
mkdir($dirName, 0777, true); $dirName => array(
$files = array(
'file1.txt' => 'file 1 content', 'file1.txt' => 'file 1 content',
'file2.txt' => 'file 2 content', 'file2.txt' => 'file 2 content',
); 'test_sub_dir' => array(
foreach ($files as $name => $content) { 'sub_dir_file_1.txt' => 'sub dir file 1 content',
file_put_contents($dirName . DIRECTORY_SEPARATOR . $name, $content); 'sub_dir_file_2.txt' => 'sub dir file 2 content',
} ),
$subDirName = $dirName . DIRECTORY_SEPARATOR . 'test_sub_dir'; ),
mkdir($subDirName, 0777, true); ));
foreach ($files as $name => $content) {
file_put_contents($subDirName . DIRECTORY_SEPARATOR . $name, $content); $basePath = $this->testFilePath;
} $dirName = $basePath . DIRECTORY_SEPARATOR . $dirName;
FileHelper::removeDirectory($dirName); FileHelper::removeDirectory($dirName);
...@@ -98,27 +119,25 @@ class FileHelperTest extends TestCase ...@@ -98,27 +119,25 @@ class FileHelperTest extends TestCase
public function testFindFiles() public function testFindFiles()
{ {
$dirName = 'test_dir';
$this->createFileStructure(array(
$dirName => array(
'file_1.txt' => 'file 1 content',
'file_2.txt' => 'file 2 content',
'test_sub_dir' => array(
'file_1_1.txt' => 'sub dir file 1 content',
'file_1_2.txt' => 'sub dir file 2 content',
),
),
));
$basePath = $this->testFilePath; $basePath = $this->testFilePath;
$expectedFiles = array(); $dirName = $basePath . DIRECTORY_SEPARATOR . $dirName;
$expectedFiles = array(
$dirName = $basePath . DIRECTORY_SEPARATOR . 'test_dir_for_remove'; $dirName . DIRECTORY_SEPARATOR . 'file_1.txt',
mkdir($dirName, 0777, true); $dirName . DIRECTORY_SEPARATOR . 'file_2.txt',
$files = array( $dirName . DIRECTORY_SEPARATOR . 'test_sub_dir' . DIRECTORY_SEPARATOR . 'file_1_1.txt',
'file1.txt' => 'file 1 content', $dirName . DIRECTORY_SEPARATOR . 'test_sub_dir' . DIRECTORY_SEPARATOR . 'file_1_2.txt',
'file2.txt' => 'file 2 content',
); );
foreach ($files as $name => $content) {
$fileName = $dirName . DIRECTORY_SEPARATOR . $name;
file_put_contents($fileName, $content);
$expectedFiles[] = $fileName;
}
$subDirName = $dirName . DIRECTORY_SEPARATOR . 'test_sub_dir';
mkdir($subDirName, 0777, true);
foreach ($files as $name => $content) {
$fileName = $subDirName . DIRECTORY_SEPARATOR . $name;
file_put_contents($fileName, $content);
$expectedFiles[] = $fileName;
}
$foundFiles = FileHelper::findFiles($dirName); $foundFiles = FileHelper::findFiles($dirName);
sort($expectedFiles); sort($expectedFiles);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment