mpociot / laravel-test-factory-helper

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

Better support for enums. #53

Closed TheDoctor0 closed 2 years ago

TheDoctor0 commented 4 years ago

Fix proposed in #51 for issue #50 will remove SQL error, but also make enum fields always a $faker->word in generated factories. PostgreSQL implementation of enum does not store allowed values in type definition, instead there is a field constraint.

I came up with solution that supports both MySQL and PostgreSQL. Right now it should work even without a custom enum type for Doctrine.

I tested it with PostgreSQL and this migration:

Schema::create('tests', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->enum('test', ['one', 'two', 'three']);
    $table->timestamps();
});            

Generated factory:

$factory->define(App\Models\Test::class, function (Faker $faker) {
    return [
        'test' => $faker->randomElement(['one', 'two', 'three']),
    ];
});

Changes that were made:

duellsy commented 4 years ago

Gave this a run in my project today which is using postgres, worked perfectly 💯

TheDoctor0 commented 4 years ago

@jasonmccreary, can u review and potentially merge this PR?

jasonmccreary commented 4 years ago

I'd like to see more verify it. I don't use enums or Postgres. So I'm worried about merging it and breaking something else...

TheDoctor0 commented 4 years ago

I completely understand the concerns. I'll try to make some time in coming days and test this for various field types (with and without enums) on Postgres, MySQL and SQLite to ensure that everything is working as it should.

TheDoctor0 commented 4 years ago

Finally I had some time to check this. Two additional changes were made:

  1. Model connection is used when checking enum column.
  2. Fixed enum column check on PostgreSQL 12+.

I created the test repository, it contains:

  1. Docker configuration for databases containers.
  2. Test models and migrations for SQLite, MySQL and PostgreSQL with all possible fields.
  3. Generated factories.

@jasonmccreary, as you can see, everything is working as intended.