thephpleague / factory-muffin

Enables the rapid creation of objects for testing
https://factory-muffin.thephpleague.com/
MIT License
531 stars 72 forks source link

Overwriting model attributes #409

Closed JonoB closed 8 years ago

JonoB commented 8 years ago

Assuming we have a factory as follows:

$fm->define(\Category::class, [
    'code' => Faker::word(),
    'name' => Faker::sentence(),
]);

And in one of our tests we want to inject companyId property:

$category = $this->fm->create(\Category::class, [
     'company_id' => $companyId,
]);

In this case, if $category->toArray() looks like this:

[
  "company_id" => 1
  "id" => 1
]

Basically, the default proprties (code and name) are now gone. Why does injecting properties as the second parameter to ->create() method completely replace all model properties? This doesn't seem to be expected behavior (or consistent with V2).

Am I missing something obvious here?

GrahamCampbell commented 8 years ago

I don't see how this could be happening?

https://github.com/thephpleague/factory-muffin/blob/master/src/FactoryMuffin.php#L181

JonoB commented 8 years ago

Yeah, its because our factories were set up V2 style:

$fm->define(\Category::class, [
    'code' => Faker::word(),
    'name' => Faker::sentence(),
]);

Instead, we need to be using the setDefinitions method:

$fm->define(Category::class)->setDefinitions([
    'code' => Faker::word(),
    'name' => Faker::sentence(),
]);