How to set up yaml fixtures with UploadedFile (e.g. image) files? How to set up yaml fixtures with UploadedFile (e.g. image) files? symfony symfony

How to set up yaml fixtures with UploadedFile (e.g. image) files?


You should use Alice.

Alice is a PHP fixtures generator that allows you to load fixtures from PHP or Yaml files easily and manage uploaded files. Here is a snippet of code that loads some data fixtures from a Doctrine Fixtures class:

<?phpnamespace MyProject\User\Fixtures;use Doctrine\Bundle\FixturesBundle\Fixture;use Doctrine\Common\Persistence\ObjectManager;use Nelmio\Alice\Loader\Yaml;use Nelmio\Alice\ORM\Doctrine;use Symfony\Component\Finder\Finder;class LoadUserData implements FixtureInterface{    public function load(ObjectManager $manager)    {        // load objects from a yaml file        $loader = new \Nelmio\Alice\Loader\Yaml();        $objects = $loader->load(__DIR__.'/users.yml');        $persister = new \Nelmio\Alice\ORM\Doctrine($manager);        $persister->persist($objects);        // Copy images into the legacy application        $fs = $this->container->get('filesystem');        $legacyPath = $this->container->getParameter('legacy_path').'/web/uploads';        $basePath = __DIR__.'/../files';        $finder = \Symfony\Component\Finder\Finder::create()            ->files()            ->in($basePath)            ->ignoreDotFiles(true)            ->ignoreVCS(true)        ;        foreach ($finder as $file) {            if ($file->isFile()) {                $newFile = str_replace($basePath, $legacyPath, $file->getPathname());                $fs->copy($file->getPathname(), $newFile);            }        }    }}

And in your data to load in a YML file :

# users.ymlMyProject\User:    user_1:        firstName: "Albert"        lastName:  "Einstein"        username:  "albert"        email:     "albert.einstein@protonmail.com"

More info here.