mpociot / laravel-test-factory-helper

Generate Laravel test factories from your existing models
935 stars 87 forks source link

Allow "enum" type #45

Closed skeets23 closed 4 years ago

skeets23 commented 4 years ago

Instead of just mapping enum type fields to act as "string", I created a custom type. This way a MySQL field like this:

size ENUM('x-small', 'small', 'medium', 'large', 'x-large')

will produce a factory like this:

...
'size' => $faker->randomElement(['x-small','small','medium','large','x-large']),
...
jasonmccreary commented 4 years ago

Thanks for this.

Before it can be merged, can you match the PSR-2 coding style. Also, the Enum class seems unnecessary. Please inline it to keep everything within the current class.

skeets23 commented 4 years ago

Ok, I've fixed the indenting and replaced FCN with imports. (I'm assuming that's what you were referring to?)

Unfortunately, the Enum class can't be removed. MySql enum fields are not supported by the DBAL library at all, and so to allow them, a custom data type must be used.

I followed the official documentation to add this custom type:

https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/types.html#custom-mapping-types

Enum's aren't supported in DBAL, but for our purposes here, a custom class that is there just to add an additional type to DBAL so we can differentiate between enum and string is enough to do what we're looking for.

If there's another way to do it, I'd be glad to hear about it. 😅

skeets23 commented 4 years ago

You'll see the CustomEnum class is mentioned on line 76 of GenerateCommand.php

skeets23 commented 4 years ago

Thanks for the code review. I've made the changes and it should be ready to go now. 👍

jasonmccreary commented 4 years ago

Thanks. I'll merge this and tag it this week.