Skip to content

Commit fa97aa6

Browse files
committed
recursive remove dir
1 parent c5ad367 commit fa97aa6

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

tests/tests/TestBehatTags.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,39 @@ protected function set_up(): void {
2323
}
2424

2525
protected function tear_down(): void {
26-
2726
if ( $this->temp_dir && file_exists( $this->temp_dir ) ) {
28-
foreach ( glob( $this->temp_dir . '/features/*' ) as $feature_file ) {
29-
unlink( $feature_file );
30-
}
31-
rmdir( $this->temp_dir . '/features' );
32-
rmdir( $this->temp_dir );
27+
$this->remove_dir( $this->temp_dir );
3328
}
3429

3530
parent::tear_down();
3631
}
3732

33+
/**
34+
* Recursively removes a directory and its contents.
35+
*
36+
* @param string $dir The directory to remove.
37+
*/
38+
private function remove_dir( $dir ) {
39+
if ( ! is_dir( $dir ) ) {
40+
return;
41+
}
42+
43+
$iterator = new \RecursiveIteratorIterator(
44+
new \RecursiveDirectoryIterator( $dir, \FilesystemIterator::SKIP_DOTS ),
45+
\RecursiveIteratorIterator::CHILD_FIRST
46+
);
47+
48+
foreach ( $iterator as $file ) {
49+
if ( $file->isDir() ) {
50+
rmdir( $file->getRealPath() );
51+
} else {
52+
unlink( $file->getRealPath() );
53+
}
54+
}
55+
56+
rmdir( $dir );
57+
}
58+
3859
/**
3960
* Runs the behat-tags.php script in a cross-platform way.
4061
*

0 commit comments

Comments
 (0)