Skip to content

Fixes Factory Using Wrong Model Name #54644

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

Merged
merged 8 commits into from
Feb 17, 2025
Merged
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
10 changes: 5 additions & 5 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ abstract class Factory
public static $namespace = 'Database\\Factories\\';

/**
* The default model name resolver.
* The default model name resolvers.
*
* @var callable(self): class-string<TModel>
* @var array<class-string, callable(self): class-string<TModel>>
*/
protected static $modelNameResolver;
protected static $modelNameResolvers = [];

/**
* The factory name resolver.
Expand Down Expand Up @@ -810,7 +810,7 @@ public function modelName()
return $this->model;
}

$resolver = static::$modelNameResolver ?? function (self $factory) {
$resolver = static::$modelNameResolvers[static::class] ?? function (self $factory) {
$namespacedFactoryBasename = Str::replaceLast(
'Factory', '', Str::replaceFirst(static::$namespace, '', get_class($factory))
);
Expand All @@ -835,7 +835,7 @@ public function modelName()
*/
public static function guessModelNamesUsing(callable $callback)
{
static::$modelNameResolver = $callback;
static::$modelNameResolvers[static::class] = $callback;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if you supply the following to set global model resolver? e.g:

Illuminate\Database\Eloquent\Factories\Factory::guestModelNamesUsing( /* ... */ );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has no effect. I added a test case showing this: SameOldNick@ff30fca

}

/**
Expand Down
55 changes: 55 additions & 0 deletions tests/Database/DatabaseEloquentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Container\Container;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Attributes\UseFactory;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\CrossJoinSequence;
use Illuminate\Database\Eloquent\Factories\Factory;
Expand Down Expand Up @@ -829,6 +830,22 @@ public function test_can_disable_relationships()
$this->assertNull($post->user_id);
}

public function test_factory_model_names_correct()
{
$this->assertEquals(FactoryTestUseFactoryAttribute::factory()->modelName(), FactoryTestUseFactoryAttribute::class);
$this->assertEquals(FactoryTestGuessModel::factory()->modelName(), FactoryTestGuessModel::class);
}

public function test_factory_global_model_resolver()
{
\Illuminate\Database\Eloquent\Factories\Factory::guessModelNamesUsing(function ($model) {
return $model . 'Factory';
});

$this->assertEquals(FactoryTestUseFactoryAttribute::factory()->modelName(), FactoryTestUseFactoryAttribute::class);
$this->assertEquals(FactoryTestGuessModel::factory()->modelName(), FactoryTestGuessModel::class);
}

/**
* Get a database connection instance.
*
Expand Down Expand Up @@ -982,3 +999,41 @@ public function users()
return $this->belongsToMany(FactoryTestUser::class, 'role_user', 'role_id', 'user_id')->withPivot('admin');
}
}

class FactoryTestGuessModelFactory extends Factory
{
protected static function appNamespace()
{
return __NAMESPACE__ . '\\';
}

public function definition()
{
return [
'name' => $this->faker->name(),
];
}
}

class FactoryTestGuessModel extends Eloquent
{
use HasFactory;

protected static $factory = FactoryTestGuessModelFactory::class;
}

class FactoryTestUseFactoryAttributeFactory extends Factory
{
public function definition()
{
return [
'name' => $this->faker->name(),
];
}
}

#[UseFactory(FactoryTestUseFactoryAttributeFactory::class)]
class FactoryTestUseFactoryAttribute extends Eloquent
{
use HasFactory;
}
Loading