laracasts / TestDummy

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

Ability to use existing data #103

Open phroggyy opened 9 years ago

phroggyy commented 9 years ago

This resolves #97. Currently, the command for using existing data rather than new data is model:factoryName, as compared to the default factory:factoryName. If there is no existing data, an entry will be created in the same fashion as factory:factoryName.

This means that, given the following conditions (and assuming we have our relations correctly defined):

$factory('App\User', [
    'first_name'     => $faker->firstName,
    'last_name'     => $faker->lastName,
    'email'             => $faker->unique()->email,
    'password'      => bcrypt($faker->word),
]);

$factory('App\Post', 'post_with_existing_user', [
    'user_id'       => 'model:App\User',
    'title'                => $faker->sentence,
    'body'              => $faker->lastName,
]);

then

Factory::create('post_with_existing_user');
Will produce a result identical to

Factory::create('App\User');
Factory::create('post_with_existing_user');