diff --git a/test/Unit/Core/DirectoryRecursor.test.php b/test/Unit/Core/DirectoryRecursor.test.php new file mode 100644 index 00000000..e42690eb --- /dev/null +++ b/test/Unit/Core/DirectoryRecursor.test.php @@ -0,0 +1,92 @@ +createDirectoryStructure($path, 5, 5); + + $outputArray = DirectoryRecursor::walk( + Path::get(Path::ROOT), [$this, "walkCallback"]); + $this->assertInternalType("array", $outputArray); + + foreach ($outputArray as $outputItem) { + $innerFilePath = substr($outputItem, strpos($outputItem, "/")); + $this->assertFileExists($innerFilePath); + } +} + +/** + * @expectedException \Gt\Core\Exception\RequiredAppResourceNotFoundException + */ +public function testWalkOnDirectoryThatDoesNotExist() { + DirectoryRecursor::walk("/" . uniqid(true), null); +} + +public function testHash() { + $path = Path::get(Path::ROOT); + $this->createDirectoryStructure($path, 5, 5); + + $md5Array = DirectoryRecursor::walk($path, [$this, "hashCallback"]); + $md5 = implode("", $md5Array); + + $md5 = md5($md5); + + $hash = DirectoryRecursor::hash(Path::get(Path::ROOT)); + $this->assertEquals($md5, $hash); +} + +private function createDirectoryStructure($basePath, $depth, $leaves) { + for($d = 1; $d < $depth; $d++) { + $path = "$basePath/"; + $path .= implode("/", array_fill(0, $d, "dir")); + + if(!is_dir($path)) { + mkdir($path, 0775, true); + } + + for($l = 1; $l <= $leaves; $l++) { + $leaf = "$path/leaf-$l.file"; + + file_put_contents($leaf, "File contents of leaf-$l"); + } + } +} + +/** + * + */ +public function walkCallback($file, $iterator) { + $path = $file->getPathname(); + return $path; +} + +/** + * + */ +public function hashCallback($file, $iterator) { + if($file->isDir()) { + return null; + } + + $path = $file->getPathname(); + return md5($path) . md5_file($path); +} + +}# \ No newline at end of file