Skip to content

Commit

Permalink
test: correctly remove temp files and directories after tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Oct 16, 2024
1 parent bcb7492 commit 3862dc7
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions tests/Hook/DeleteTemporaryFilesHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,40 @@

final class DeleteTemporaryFilesHook implements AfterLastTestHook
{
private const TMP_DIR = __DIR__ . '/../../.tmp';

/**
* @return void
*/
public function executeAfterLastTest(): void
{
$files = scandir('.tmp/');
$this->deleteDirectory(self::TMP_DIR);
}

foreach ($files as $file) {
if ('.' === $file || '..' === $file) {
/**
* @param string $dir
* @param bool $deleteDir
*
* @return void
*/
private function deleteDirectory(string $dir, bool $deleteDir = false): void
{
$paths = scandir($dir);

foreach ($paths as $path) {
if ('.' === $path || '..' === $path) {
continue;
}

if (is_dir(".tmp//$file")) {
rmdir(".tmp//$file");
if (is_dir("$dir/$path")) {
$this->deleteDirectory("$dir/$path", true);
} else {
unlink(".tmp//$file");
unlink("$dir/$path");
}
}

if ($deleteDir) {
rmdir($dir);
}
}
}

0 comments on commit 3862dc7

Please sign in to comment.