Skip to content

Create factory extending/overriding an existing factory's attributes #108

@vlakoff

Description

@vlakoff

At first glance, I tried to create a factory for admin users like so:

$factory('App\User', [
    'name' => $faker->userName,
    'password' => $faker->password,
]);

$factory('App\User', 'admin_user', [
    'admin' => 1,
]);

… But this doesn't work. All the attributes have to be defined again. This was a bit suprising, mainly because Factory::build('App\User', ['admin' => 1]) was doing the job. But I understand, the current behaviour is perfectly fine, short names aren't at all about extending.

So, I tried this:

$factory('App\User', 'admin_user', Factory::attributesFor('App\User', [
    'admin' => 1,
]));

... But this doesn't work, because the App\User factory isn't available yet.

So far, I have to do this:

$attributes = [
    'name' => $faker->userName,
    'password' => $faker->password,
];

$factory('App\User', $attributes);

$attributes['admin' => 1];

$factory('App\User', 'admin_user', $attributes);

Do you think this could this be improved ? I was thinking of a syntax like so:

$factory('extends:<class-or-shortname>', 'admin_user', [
    'admin' => 1,
]);

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