From 55b8a32cc52cb5b96ded36ee08cc2166d0450ffb Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sun, 26 May 2024 18:26:31 -0700 Subject: [PATCH] Update IntegrationTestCase.php --- .../Integration/Util/IntegrationTestCase.php | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/Integration/Util/IntegrationTestCase.php b/tests/Integration/Util/IntegrationTestCase.php index c1a55041..0d7ba854 100644 --- a/tests/Integration/Util/IntegrationTestCase.php +++ b/tests/Integration/Util/IntegrationTestCase.php @@ -15,6 +15,7 @@ use RecursiveIteratorIterator; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Finder\Finder; /** * Class IntegrationTestCase @@ -89,6 +90,39 @@ protected function deleteDir($dir) $filesystem = new Filesystem(new LocalFilesystemAdapter('/')); + /** + * Delete symlinks first. + * + * @see https://github.com/thephpleague/flysystem/issues/1560 + */ + $finder = new Finder(); + $finder->in($dir); + if ($finder->hasResults()) { + + /** @var \SplFileInfo[] $files */ + $files = iterator_to_array($finder->getIterator()); + /** @var \SplFileInfo[] $links */ + $links = array_filter( + $files, + function ($file) { + return $file->isLink(); + } + ); + + // Sort by longest filename first. + uasort($links, function ($a, $b) { + return strlen($b->getPath()) <=> strlen($a->getPath()); + }); + + foreach ($links as $link) { + $linkPath = "{$link->getPath()}/{$link->getFilename()}"; + unlink($linkPath); + if (is_readable($linkPath)) { + rmdir($linkPath); + } + } + } + $filesystem->deleteDirectory($dir); } }