From 5b3350fc68abdb76fde444b7f1e330ee292aa3c0 Mon Sep 17 00:00:00 2001 From: Josh Moreno Date: Thu, 7 May 2020 20:50:47 -0700 Subject: [PATCH 1/4] fix times method when also using the with method --- src/BaseFactory.php | 28 +++++++++++++--------------- src/MultiFactoryCollection.php | 27 +++++++++++++++++++++++++++ tests/FactoryTest.php | 15 +++++++++++++++ 3 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 src/MultiFactoryCollection.php diff --git a/src/BaseFactory.php b/src/BaseFactory.php index e750dd9..2e26775 100644 --- a/src/BaseFactory.php +++ b/src/BaseFactory.php @@ -13,7 +13,7 @@ abstract class BaseFactory implements FactoryInterface protected string $modelClass; - protected Collection $relatedModels; + protected Collection $relatedModelFactories; protected string $relatedModelRelationshipName; @@ -24,7 +24,7 @@ abstract class BaseFactory implements FactoryInterface public function __construct(Generator $faker) { $this->faker = $faker; - $this->relatedModels = collect(); + $this->relatedModelFactories = collect(); } /** @return static */ @@ -40,37 +40,35 @@ protected function build(array $extra = [], string $creationType = 'create') $modelData = $this->prepareModelData($creationType, $this->getDefaults($this->faker)); $model = $this->modelClass::$creationType(array_merge($modelData, $this->overwriteDefaults, $extra)); - if ($this->relatedModels->isEmpty()) { + if ($this->relatedModelFactories->isEmpty()) { return $model; } + $relatedModels = $this->relatedModelFactories->map(fn($factory) => $factory->make()); + if ($creationType === 'create') { $model->{$this->relatedModelRelationshipName}() - ->saveMany($this->relatedModels); + ->saveMany($relatedModels); return $model; } - return $model->setRelation($this->relatedModelRelationshipName, $this->relatedModels); + return $model->setRelation($this->relatedModelRelationshipName, $relatedModels); } - public function times(int $times = 1): CollectionFactory + public function times(int $times = 1): MultiFactoryCollection { - $collectionData = collect() - ->times($times, function ($key) { - return array_merge($this->getDefaults($this->faker), $this->overwriteDefaults); - }); - - return new CollectionFactory($this->modelClass, $times, $collectionData); + return new MultiFactoryCollection(collect()->times($times, function() { + return clone $this; + })); } public function with(string $relatedModelClass, string $relationshipName, int $times = 1) { $clone = clone $this; - $clone->relatedModels = $this->getFactoryFromClassName($relatedModelClass) - ->times($times) - ->make(); + $clone->relatedModelFactories = collect()->times($times, fn() => $this->getFactoryFromClassName($relatedModelClass)); + $clone->relatedModelRelationshipName = $relationshipName; return $clone; diff --git a/src/MultiFactoryCollection.php b/src/MultiFactoryCollection.php new file mode 100644 index 0000000..511c867 --- /dev/null +++ b/src/MultiFactoryCollection.php @@ -0,0 +1,27 @@ +factories = $factories; + } + + public function create() + { + return $this->factories->map(function($factory) { + return $factory->create(); + }); + } + + public function make() + { + return $this->factories->map(function($factory) { + return $factory->make(); + }); + } +} diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index ea0552e..ba92043 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -214,4 +214,19 @@ public function it_works_with_factory_as_relationship_for_creating_multiple_mode $this->assertCount(4, Recipe::all()); $this->assertCount(4, Group::all()); } + + /** @test */ + public function it_lets_you_add_related_models_when_creating_multiple() + { + $groups = GroupFactory::new() + ->with(Recipe::class, 'recipes', 5, true) + ->times(3) + ->create(); + + $this->assertCount(3, $groups); + $this->assertCount(15, Recipe::all()); + $groups->each(function(Group $group) { + $this->assertCount(5, $group->recipes); + }); + } } From e335cfcacbe1ba8d3f88543eaf31754dff467598 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 11 May 2020 08:21:08 -0700 Subject: [PATCH 2/4] delete unused CollectionFactory class --- src/CollectionFactory.php | 48 --------------------------------------- 1 file changed, 48 deletions(-) delete mode 100644 src/CollectionFactory.php diff --git a/src/CollectionFactory.php b/src/CollectionFactory.php deleted file mode 100644 index 3423b1e..0000000 --- a/src/CollectionFactory.php +++ /dev/null @@ -1,48 +0,0 @@ -modelClass = $modelClass; - $this->times = $times; - $this->modelsDefaultData = $modelData; - } - - public function create(array $extra = []): Collection - { - return $this->build($extra); - } - - public function make(array $extra = []): Collection - { - return $this->build($extra, 'make'); - } - - private function build(array $extra = [], string $creationType = 'create'): Collection - { - $this->modelsDefaultData->transform(function (array $modelFields) use ($creationType) { - return $this->prepareModelData($creationType, $modelFields); - }); - - - return collect() - ->times($this->times) - ->transform(fn ($value, $key) => $this->modelClass::$creationType(array_merge( - $this->modelsDefaultData[$key], - $extra - ))); - } -} From 00cbd7e2f4186343827ec69060811152abc512c9 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 11 May 2020 08:21:34 -0700 Subject: [PATCH 3/4] add return types to MultiFactoryCollection methods --- src/MultiFactoryCollection.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/MultiFactoryCollection.php b/src/MultiFactoryCollection.php index 511c867..9d01694 100644 --- a/src/MultiFactoryCollection.php +++ b/src/MultiFactoryCollection.php @@ -3,7 +3,7 @@ use Illuminate\Support\Collection; -class MultiFactoryCollection{ +class MultiFactoryCollection { protected Collection $factories; public function __construct(Collection $factories) @@ -11,14 +11,14 @@ public function __construct(Collection $factories) $this->factories = $factories; } - public function create() + public function create(): Collection { return $this->factories->map(function($factory) { return $factory->create(); }); } - public function make() + public function make(): Collection { return $this->factories->map(function($factory) { return $factory->make(); From a69c9d804123f3c27bc22e3b1bedf15e88df67d0 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 11 May 2020 08:27:40 -0700 Subject: [PATCH 4/4] use higher order map --- src/BaseFactory.php | 2 +- src/MultiFactoryCollection.php | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/BaseFactory.php b/src/BaseFactory.php index 2e26775..1a6091f 100644 --- a/src/BaseFactory.php +++ b/src/BaseFactory.php @@ -44,7 +44,7 @@ protected function build(array $extra = [], string $creationType = 'create') return $model; } - $relatedModels = $this->relatedModelFactories->map(fn($factory) => $factory->make()); + $relatedModels = $this->relatedModelFactories->map->make(); if ($creationType === 'create') { $model->{$this->relatedModelRelationshipName}() diff --git a/src/MultiFactoryCollection.php b/src/MultiFactoryCollection.php index 9d01694..7ae9970 100644 --- a/src/MultiFactoryCollection.php +++ b/src/MultiFactoryCollection.php @@ -13,15 +13,11 @@ public function __construct(Collection $factories) public function create(): Collection { - return $this->factories->map(function($factory) { - return $factory->create(); - }); + return $this->factories->map->create(); } public function make(): Collection { - return $this->factories->map(function($factory) { - return $factory->make(); - }); + return $this->factories->map->make(); } }