Skip to content

Commit 013d708

Browse files
[11.x] Fix regression bug with global Factory::guessModelNamesUsing() (#54665)
* [11.x] Fix regression bug with global `Factory::guessModelNamesUsing()` bug introduced via #54644 Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Update Factory.php --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent ee0f81f commit 013d708

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/Illuminate/Database/Eloquent/Factories/Factory.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,9 +810,9 @@ public function modelName()
810810
return $this->model;
811811
}
812812

813-
$resolver = static::$modelNameResolvers[static::class] ?? function (self $factory) {
813+
$resolver = static::$modelNameResolvers[static::class] ?? static::$modelNameResolvers[self::class] ?? function (self $factory) {
814814
$namespacedFactoryBasename = Str::replaceLast(
815-
'Factory', '', Str::replaceFirst(static::$namespace, '', get_class($factory))
815+
'Factory', '', Str::replaceFirst(static::$namespace, '', $factory::class)
816816
);
817817

818818
$factoryBasename = Str::replaceLast('Factory', '', class_basename($factory));
@@ -924,6 +924,18 @@ protected static function appNamespace()
924924
}
925925
}
926926

927+
/**
928+
* Flush the factory's global state.
929+
*
930+
* @return void
931+
*/
932+
public static function flushState()
933+
{
934+
static::$modelNameResolvers = [];
935+
static::$factoryNameResolver = null;
936+
static::$namespace = 'Database\\Factories\\';
937+
}
938+
927939
/**
928940
* Proxy dynamic factory methods onto their proper methods.
929941
*

src/Illuminate/Foundation/Testing/Concerns/InteractsWithTestCaseLifecycle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Carbon\CarbonImmutable;
66
use Illuminate\Console\Application as Artisan;
77
use Illuminate\Cookie\Middleware\EncryptCookies;
8+
use Illuminate\Database\Eloquent\Factories\Factory;
89
use Illuminate\Database\Eloquent\Model;
910
use Illuminate\Database\Migrations\Migrator;
1011
use Illuminate\Foundation\Bootstrap\HandleExceptions;
@@ -169,6 +170,7 @@ protected function tearDownTheTestEnvironment(): void
169170
Component::forgetComponentsResolver();
170171
Component::forgetFactory();
171172
ConvertEmptyStringsToNull::flushState();
173+
Factory::flushState();
172174
EncryptCookies::flushState();
173175
HandleExceptions::flushState();
174176
Migrator::withoutMigrations([]);

tests/Database/DatabaseEloquentFactoryTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Illuminate\Database\Eloquent\Factories\Sequence;
1717
use Illuminate\Database\Eloquent\Model as Eloquent;
1818
use Illuminate\Database\Eloquent\SoftDeletes;
19+
use Illuminate\Support\Str;
1920
use Illuminate\Tests\Database\Fixtures\Models\Money\Price;
2021
use Mockery as m;
2122
use PHPUnit\Framework\TestCase;
@@ -838,12 +839,15 @@ public function test_factory_model_names_correct()
838839

839840
public function test_factory_global_model_resolver()
840841
{
841-
\Illuminate\Database\Eloquent\Factories\Factory::guessModelNamesUsing(function ($model) {
842-
return $model.'Factory';
842+
Factory::guessModelNamesUsing(function ($factory) {
843+
return __NAMESPACE__.'\\'.Str::replaceLast('Factory', '', class_basename($factory::class));
843844
});
844845

845-
$this->assertEquals(FactoryTestUseFactoryAttribute::factory()->modelName(), FactoryTestUseFactoryAttribute::class);
846846
$this->assertEquals(FactoryTestGuessModel::factory()->modelName(), FactoryTestGuessModel::class);
847+
$this->assertEquals(FactoryTestUseFactoryAttribute::factory()->modelName(), FactoryTestUseFactoryAttribute::class);
848+
849+
$this->assertEquals(FactoryTestUseFactoryAttributeFactory::new()->modelName(), FactoryTestUseFactoryAttribute::class);
850+
$this->assertEquals(FactoryTestGuessModelFactory::new()->modelName(), FactoryTestGuessModel::class);
847851
}
848852

849853
/**

0 commit comments

Comments
 (0)