Skip to content

Commit

Permalink
[8.x] Fix autoresolving model name from factory (#40616)
Browse files Browse the repository at this point in the history
* Fix autoresolving model name from factory

* Reverse change to fallback factory

* Add test
  • Loading branch information
driesvints authored Jan 27, 2022
1 parent 22e32b9 commit da28a53
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,12 +686,16 @@ public function newModel(array $attributes = [])
public function modelName()
{
$resolver = static::$modelNameResolver ?: function (self $factory) {
$namespacedFactoryBasename = Str::replaceLast(
'Factory', '', Str::replaceFirst(static::$namespace, '', get_class($factory))
);

$factoryBasename = Str::replaceLast('Factory', '', class_basename($factory));

$appNamespace = static::appNamespace();

return class_exists($appNamespace.'Models\\'.$factoryBasename)
? $appNamespace.'Models\\'.$factoryBasename
return class_exists($appNamespace.'Models\\'.$namespacedFactoryBasename)
? $appNamespace.'Models\\'.$namespacedFactoryBasename
: $appNamespace.$factoryBasename;
};

Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseEloquentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Tests\Database\Fixtures\Models\Money\Price;
use Mockery;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -469,6 +470,18 @@ public function test_resolve_nested_model_factories()
}
}

public function test_resolve_nested_model_name_from_factory()
{
Container::getInstance()->instance(Application::class, $app = Mockery::mock(Application::class));
$app->shouldReceive('getNamespace')->andReturn('Illuminate\\Tests\\Database\\Fixtures\\');

Factory::useNamespace('Illuminate\\Tests\\Database\\Fixtures\\Factories\\');

$factory = Price::factory();

$this->assertSame(Price::class, $factory->modelName());
}

public function test_resolve_non_app_nested_model_factories()
{
Container::getInstance()->instance(Application::class, $app = Mockery::mock(Application::class));
Expand Down
15 changes: 15 additions & 0 deletions tests/Database/Fixtures/Factories/Money/PriceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Illuminate\Tests\Database\Fixtures\Factories\Money;

use Illuminate\Database\Eloquent\Factories\Factory;

class PriceFactory extends Factory
{
public function definition()
{
return [
'name' => $this->faker->name,
];
}
}
19 changes: 19 additions & 0 deletions tests/Database/Fixtures/Models/Money/Price.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Illuminate\Tests\Database\Fixtures\Models\Money;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Tests\Database\Fixtures\Factories\Money\PriceFactory;

class Price extends Model
{
use HasFactory;

protected $table = 'prices';

public static function factory()
{
return PriceFactory::new();
}
}

0 comments on commit da28a53

Please sign in to comment.