vierge-noire / cakephp-fixture-factories

CakePHP Fixture Factories
https://vierge-noire.github.io/
MIT License
85 stars 20 forks source link

FixtureFactories with new types from plugins #127

Closed gringlas closed 3 years ago

gringlas commented 3 years ago

When I try to create entities with a FixtureFactory on an entity which got added new DataTypes through a plugin I will get an exception, i.e. InvalidArgumentException : Unknown type "upload.file" In my case I'm using the UploadBehaviorPlugon in my ImagesTable, which declares a new Type 'upload.file' in its Plugin.php. I then wanted to use a ImageFactory to create an entity, like:

$image = ImageFactory::make([
            'filename' =>  'path.jpg',
            'path' => 'my/path'
        ])->persist();

I had to add TypeFactory::map('upload.file', FileType::class); to my ImagesTableTest class, to got it working. Is this the intended way, or is this an issue?

Thanks for this great plugin!

pabloelcolombiano commented 3 years ago

Glad you are enjoying the plugin!

This is specific to the Uploaded plugin, and should probably not be handled by the factories.

What you could do is check in the ImageFactory::setDefaultTemplate() method if the type exists (TypeFactory::getMap('upload.file')), and if not found, TypeFactory::map('upload.file', FileType::class);.

This way, your issue is solved for all the tests involving that factory.

gringlas commented 3 years ago

This is working like a charme. Thank you! :)