Skip to content

$faker->optional() is not getting called foreach transaction #101

@manuphatak

Description

@manuphatak

$faker->optional() is only getting called ONE time per Seeder. So the results are either ALL filled or ALL empty instead of healthy mix of each.

Problem

Isolated example:

<?php // tests/factories/TimeEntryFactory.php

/** @var Closure $factory */
$factory('App\TimeEntry',
         [
             'user_id'    => 'factory:App\User',
             'comment'    => $faker->optional($weight = 0.5)->sentence()
         ]);

and

<?php // database/seeds/TimeEntryTableSeeder.php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Laracasts\TestDummy\Factory as TestDummy;


class TimeEntryTableSeeder extends Seeder
{

    public function run()
    {
        DB::table('time_entry')->delete();

        TestDummy::times(100)->create('App\TimeEntry');
    }
}

You would expect about 50/100 entries to have null "comments", instead it's randomly either 0 or 100.

Solution:

Using for loop instead of times() did not work.

Using a for loop, pulling in Faker, overriding the factory did.

Work around:

<?php // database/seeds/TimeEntryTableSeeder.php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Faker\Factory as Faker;
use Laracasts\TestDummy\Factory as TestDummy;

class TimeEntryTableSeeder extends Seeder
{

    protected $faker;

    function __construct(Faker $faker)
    {
        $this->faker = $faker->create();
    }

    public function run()
    {
        DB::table('time_entry')->delete();

        for ($i = 0; $i <= 100; $i++) {
            TestDummy::create(
                'App\TimeEntry',
                ['comment' => $this->faker->optional($weight = 0.5)->sentence()]
            );
        }
    }
}

With this solution, the results are no longer ALL or NOTHING, it looks 50/50.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions