tempDir(); foreach (['a.txt', 'a/x', 'a/b/y', 'a-b', 'b', 'skip/me', 'Z'] as $f) { @mkdir(dirname("$d/$f"), 0700, true); file_put_contents("$d/$f", $f); } return $d; } public function testOrderMatchesCompare(): void { $paths = array_keys(iterator_to_array((new TreeWalker(['' => $this->tree()]))->walk())); $sorted = $paths; usort($sorted, [TreeWalker::class, 'compare']); $this->assertSame($sorted, $paths); $this->assertSame(['Z', 'a', 'a-b', 'a.txt', 'b', 'skip'], array_values(array_unique(array_map(fn ($p) => explode('/', $p)[0], $paths)))); } public function testEmptyDirectoriesAreYieldedInOrderAndOnResume(): void { $d = $this->tree(); mkdir("$d/a/empty"); mkdir("$d/a-hollow/inner", 0700, true); symlink("$d/a.txt", "$d/skip/link"); mkdir("$d/only-link"); symlink("$d/a.txt", "$d/only-link/link"); $w = new TreeWalker(['' => $d]); $all = array_keys(iterator_to_array($w->walk())); $this->assertContains('a/empty', $all); $this->assertContains('a-hollow/inner', $all); $this->assertNotContains('a-hollow', $all, 'only the deepest'); $this->assertContains('only-link', $all, 'a link is not walked'); $this->assertNotContains('skip', $all); $sorted = $all; usort($sorted, [TreeWalker::class, 'compare']); $this->assertSame($sorted, $all); foreach ($all as $i => $cursor) { $this->assertSame(array_slice($all, $i + 1), array_keys(iterator_to_array($w->walk($cursor))), "after $cursor"); } } public function testResumeAfterCursorYieldsTheRest(): void { $w = new TreeWalker(['' => $this->tree()]); $all = array_keys(iterator_to_array($w->walk())); foreach ($all as $i => $cursor) { $this->assertSame(array_slice($all, $i + 1), array_keys(iterator_to_array($w->walk($cursor))), "after $cursor"); } } public function testExcludeAndMultipleRoots(): void { $d = $this->tree(); $c = $this->tempDir(); file_put_contents("$c/config.php", ' $c, 'data' => $d], ['data/skip', 'data/b']))->walk())); $this->assertContains('config/config.php', $paths); $this->assertContains('data/a/b/y', $paths); $this->assertNotContains('data/skip/me', $paths); $this->assertNotContains('data/b', $paths); } }