Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] Fix factory recycle for many to many relationships #55271

Draft
wants to merge 1 commit into
base: 12.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public function __construct($factory, $pivot, $relationship)
*/
public function createFor(Model $model)
{
Collection::wrap($this->factory instanceof Factory ? $this->factory->create([], $model) : $this->factory)->each(function ($attachable) use ($model) {
Collection::wrap($this->factory instanceof Factory
? ($this->factory->getRandomRecycledModelsForCount($this->factory->modelName()) ?? $this->factory->create([], $model))
: $this->factory
)->each(function ($attachable) use ($model) {
$model->{$this->relationship}()->attach(
$attachable,
is_callable($this->pivot) ? call_user_func($this->pivot, $model) : $this->pivot
Expand Down
30 changes: 24 additions & 6 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -676,6 +677,19 @@ public function getRandomRecycledModel($modelClassName)
return $this->recycle->get($modelClassName)?->random();
}

/**
* Retrieve a random collection of models based on the count amount of a given type from previously provided models to recycle.
*
* @template TClass of \Illuminate\Database\Eloquent\Model
*
* @param class-string<TClass> $modelClassName
* @return \Illuminate\Support\Collection<int, TClass>|null
*/
public function getRandomRecycledModelsForCount($modelClassName)
{
return $this->recycle->get($modelClassName)?->random($this->count);
}

/**
* Add a new "after making" callback to the model definition.
*
Expand Down Expand Up @@ -979,12 +993,16 @@ public function __call($method, $parameters)
if (str_starts_with($method, 'for')) {
return $this->for($factory->state($parameters[0] ?? []), $relationship);
} elseif (str_starts_with($method, 'has')) {
return $this->has(
$factory
->count(is_numeric($parameters[0] ?? null) ? $parameters[0] : 1)
->state((is_callable($parameters[0] ?? null) || is_array($parameters[0] ?? null)) ? $parameters[0] : ($parameters[1] ?? [])),
$relationship
);
$factory = $factory
->count(is_numeric($parameters[0] ?? null) ? $parameters[0] : 1)
->state((is_callable($parameters[0] ?? null) || is_array($parameters[0] ?? null)) ? $parameters[0] : ($parameters[1] ?? []));

$relationshipType = $this->newModel()->{$relationship}();
if ($relationshipType instanceof BelongsToMany) {
return $this->hasAttached($factory, [], $relationship);
}

return $this->has($factory, $relationship);
}
}
}
16 changes: 16 additions & 0 deletions tests/Database/DatabaseEloquentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,22 @@ public function test_for_method_recycles_models()
$this->assertSame(1, FactoryTestUser::count());
}

public function test_has_method_recycles_models()
{
Factory::guessFactoryNamesUsing(function ($model) {
return $model.'Factory';
});

$roles = FactoryTestRoleFactory::new()->count(2)->create();
$user = FactoryTestUserFactory::new()
->recycle($roles)
->hasRoles(2)
->create();

$this->assertTrue($roles->pluck('id')->contains($user->roles[0]->id));
$this->assertTrue($roles->pluck('id')->contains($user->roles[1]->id));
}

public function test_has_method_does_not_reassign_the_parent()
{
Factory::guessFactoryNamesUsing(function ($model) {
Expand Down
Loading