Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[testing] Added
WithTempDirectory::getTempDirectory()
helper.
- Loading branch information
1 parent
d95339b
commit c8990e9
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\Testing\Utils; | ||
|
||
use LastDragon_ru\LaraASP\Migrator\Package; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
|
||
use function mkdir; | ||
use function register_shutdown_function; | ||
use function rmdir; | ||
use function sys_get_temp_dir; | ||
use function tempnam; | ||
use function unlink; | ||
|
||
trait WithTempDirectory { | ||
protected function getTempDirectory(): string { | ||
$pkg = Package::Name; | ||
$path = tempnam(sys_get_temp_dir(), $pkg); | ||
|
||
unlink($path); | ||
mkdir($path); | ||
|
||
register_shutdown_function(static function () use ($path): void { | ||
$files = new RecursiveIteratorIterator( | ||
new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS), | ||
RecursiveIteratorIterator::CHILD_FIRST, | ||
); | ||
|
||
foreach ($files as $file) { | ||
/** @var \SplFileInfo $file */ | ||
if ($file->isDir()) { | ||
rmdir($file->getRealPath()); | ||
} else { | ||
unlink($file->getRealPath()); | ||
} | ||
} | ||
|
||
rmdir($path); | ||
}); | ||
|
||
return $path; | ||
} | ||
} |