Skip to content

Commit

Permalink
Merge pull request #45 from JoshMoreno/bugfix/fix-times-method
Browse files Browse the repository at this point in the history
fix times method when also using the with method
  • Loading branch information
christophrumpel authored May 11, 2020
2 parents 1779f16 + a69c9d8 commit db43025
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 63 deletions.
28 changes: 13 additions & 15 deletions src/BaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class BaseFactory implements FactoryInterface

protected string $modelClass;

protected Collection $relatedModels;
protected Collection $relatedModelFactories;

protected string $relatedModelRelationshipName;

Expand All @@ -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 */
Expand All @@ -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->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;
Expand Down
48 changes: 0 additions & 48 deletions src/CollectionFactory.php

This file was deleted.

23 changes: 23 additions & 0 deletions src/MultiFactoryCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Christophrumpel\LaravelFactoriesReloaded;

use Illuminate\Support\Collection;

class MultiFactoryCollection {
protected Collection $factories;

public function __construct(Collection $factories)
{
$this->factories = $factories;
}

public function create(): Collection
{
return $this->factories->map->create();
}

public function make(): Collection
{
return $this->factories->map->make();
}
}
15 changes: 15 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,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);
});
}
}

0 comments on commit db43025

Please sign in to comment.