Skip to content
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
16 changes: 14 additions & 2 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,9 +810,9 @@ public function modelName()
return $this->model;
}

$resolver = static::$modelNameResolvers[static::class] ?? function (self $factory) {
$resolver = static::$modelNameResolvers[static::class] ?? static::$modelNameResolvers[self::class] ?? function (self $factory) {
$namespacedFactoryBasename = Str::replaceLast(
'Factory', '', Str::replaceFirst(static::$namespace, '', get_class($factory))
'Factory', '', Str::replaceFirst(static::$namespace, '', $factory::class)
);

$factoryBasename = Str::replaceLast('Factory', '', class_basename($factory));
Expand Down Expand Up @@ -924,6 +924,18 @@ protected static function appNamespace()
}
}

/**
* Flush the factory's global state.
*
* @return void
*/
public static function flushState()
{
static::$modelNameResolvers = [];
static::$factoryNameResolver = null;
static::$namespace = 'Database\\Factories\\';
}

/**
* Proxy dynamic factory methods onto their proper methods.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\CarbonImmutable;
use Illuminate\Console\Application as Artisan;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Foundation\Bootstrap\HandleExceptions;
Expand Down Expand Up @@ -169,6 +170,7 @@ protected function tearDownTheTestEnvironment(): void
Component::forgetComponentsResolver();
Component::forgetFactory();
ConvertEmptyStringsToNull::flushState();
Factory::flushState();
EncryptCookies::flushState();
HandleExceptions::flushState();
Migrator::withoutMigrations([]);
Expand Down
10 changes: 7 additions & 3 deletions tests/Database/DatabaseEloquentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use Illuminate\Tests\Database\Fixtures\Models\Money\Price;
use Mockery as m;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -838,12 +839,15 @@ public function test_factory_model_names_correct()

public function test_factory_global_model_resolver()
{
\Illuminate\Database\Eloquent\Factories\Factory::guessModelNamesUsing(function ($model) {
Copy link
Member Author

Choose a reason for hiding this comment

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

$model here is an instance of Illuminate\Database\Eloquent\Factories\Factory and not a string as indicated in the test (return). If the code was executed this would have thrown an error instead of passing the test.

return $model.'Factory';
Factory::guessModelNamesUsing(function ($factory) {
return __NAMESPACE__.'\\'.Str::replaceLast('Factory', '', class_basename($factory::class));
});

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

$this->assertEquals(FactoryTestUseFactoryAttributeFactory::new()->modelName(), FactoryTestUseFactoryAttribute::class);
$this->assertEquals(FactoryTestGuessModelFactory::new()->modelName(), FactoryTestGuessModel::class);
}

/**
Expand Down
Loading