[TESTS][DataFixtures] Use Temporary file instead of an ad-hoc solution to copy the upload files

This commit is contained in:
Hugo Sales 2021-08-19 12:54:06 +01:00 committed by Diogo Peralta Cordeiro
parent 469cd97b9b
commit 41d759428f
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0

View File

@ -4,11 +4,11 @@ namespace App\DataFixtures;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\GSFile; use App\Core\GSFile;
use App\Util\TemporaryFile;
use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager; use Doctrine\Persistence\ObjectManager;
use Exception; use Exception;
use Functional as F; use Functional as F;
use Symfony\Component\HttpFoundation\File\File;
class MediaFixtures extends Fixture class MediaFixtures extends Fixture
{ {
@ -17,15 +17,15 @@ class MediaFixtures extends Fixture
DB::setManager($manager); DB::setManager($manager);
F\map(glob(INSTALLDIR . '/tests/sample-uploads/*'), F\map(glob(INSTALLDIR . '/tests/sample-uploads/*'),
function (string $filepath) { function (string $filepath) {
$copy_filepath = str_replace('.', '.copy.', $filepath); $file = new TemporaryFile();
copy($filepath, $copy_filepath); $file->write(file_get_contents($filepath));
$file = new File($copy_filepath, checkPath: true);
try { try {
GSFile::sanitizeAndStoreFileAsAttachment($file); GSFile::sanitizeAndStoreFileAsAttachment($file);
} catch (Exception $e) { } catch (Exception $e) {
echo "Could not save file {$copy_filepath}, failed with {$e}\n"; echo "Could not save file {$filepath}, failed with {$e}\n";
} finally {
unset($file);
} }
@unlink($copy_filepath);
}); });
$manager->flush(); $manager->flush();
} }