laracasts / TestDummy

Easy factories for PHP integration testing.
https://laracasts.com/lessons/whats-new-in-testdummy
MIT License
457 stars 80 forks source link

Faker not returning new randomized data #86

Closed gbrock closed 9 years ago

gbrock commented 9 years ago

The following code throws constraint violations on email and username, but works when only 1 row is generated:

$factory('App\User', function($faker) {
    $created = $faker->dateTimeBetween('-2 years', 'now');

    return [
        'username' => $faker->userName,
        'friendly_name' => $faker->firstName,
        'email' => $faker->safeEmail,
        'created_at' => $created,
        'last_login' => $faker->dateTimeBetween($created, 'now'),
    ];
});

Seems like it isn't generating a new value on each faker call...?

carlosflorencio commented 9 years ago

Yep, the problem is the closure.

Use without the closure and it works.

h-collector commented 9 years ago

In the meantime, you can use release 2.1

LasseHaslev commented 9 years ago

+1

h-collector commented 9 years ago

The problem is actually because of changed design: "Get the generator stuff out of the Factory and Builder classes" as added FactoriesLoader::normalizeDefinitions is resolving user defined Closures only once after loading factories and not before building new entity.

laurencei commented 9 years ago

+1 - this issue is really annoying.

Is it possible to just call the factory method every time I want to create a user? That way the data is randomised on each call - rather than just once?

i.e. it currently doesnt just affect $faker - it also affects basic PHP functions like rand().

gbrock commented 9 years ago

@h-collector's temporary solution works: load 2.1 in your composer.json:

    "laracasts/testdummy": "2.1",
hootlex commented 8 years ago

The issue with php functions like rand persists in 2.3.2