diff --git a/CHANGELOG.md b/CHANGELOG.md index c5d668f..9457220 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. [Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.9.3...master) -------------- +2021-04-02, 2.9.3 +----------------- + +### Fixed +- Support both customized namespace factories as well as default resolvable ones [\#1201 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/1201) + 2021-04-01, 2.9.2 ----------------- ### Added diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 932a05a..35c1419 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -1103,18 +1103,17 @@ class ModelsCommand extends Command return; } - $traits = class_uses(get_class($model), true); + $modelName = get_class($model); + + $traits = class_uses($modelName, true); if (!in_array('Illuminate\\Database\\Eloquent\\Factories\\HasFactory', $traits)) { return; } - $modelName = get_class($model); - $modelBaseName = class_basename($modelName); - - $factory = "\Database\Factories\\{$modelBaseName}Factory"; - if ($modelName::newFactory()) { $factory = get_class($modelName::newFactory()); + } else { + $factory = Factory::resolveFactoryName($modelName); } $factory = '\\' . trim($factory, '\\'); diff --git a/tests/Console/ModelsCommand/Factories/CustomSpace/ModelWithCustomNamespaceFactory.php b/tests/Console/ModelsCommand/Factories/CustomSpace/ModelWithCustomNamespaceFactory.php new file mode 100644 index 0000000..ffd6147 --- /dev/null +++ b/tests/Console/ModelsCommand/Factories/CustomSpace/ModelWithCustomNamespaceFactory.php @@ -0,0 +1,30 @@ +app->make(ModelsCommand::class); $tester = $this->runCommand($command, [ @@ -29,4 +34,19 @@ class Test extends AbstractModelsCommand $this->assertStringNotContainsString('not found', $tester->getDisplay()); $this->assertMatchesMockedSnapshot(); } + + public static function getFactoryNameResolver(): Closure + { + // This mimics the default resolver, but with adjusted test namespaces. + // Illuminate\Database\Eloquent\Factories\Factory::resolveFactoryName + return function (string $modelName): string { + $appNamespace = 'Barryvdh\\LaravelIdeHelper\\Tests\\Console\\ModelsCommand\\Factories\\'; + + $modelName = Str::startsWith($modelName, $appNamespace . 'Models\\') + ? Str::after($modelName, $appNamespace . 'Models\\') + : Str::after($modelName, $appNamespace); + + return $appNamespace . 'Factories\\' . $modelName . 'Factory'; + }; + } } diff --git a/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php index 4d8b0f8..757ac9a 100644 --- a/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php @@ -4,6 +4,61 @@ declare(strict_types=1); namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\CustomSpace\ModelWithCustomNamespaceFactory; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Model; + +/** + * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithCustomNamespace + * + * @method static \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\CustomSpace\ModelWithCustomNamespaceFactory factory(...$parameters) + * @method static \Illuminate\Database\Eloquent\Builder|ModelWithCustomNamespace newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|ModelWithCustomNamespace newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|ModelWithCustomNamespace query() + * @mixin \Eloquent + */ +class ModelWithCustomNamespace extends Model +{ + use HasFactory; + + /** + * Create a new factory instance for the model. + * + * @return \Illuminate\Database\Eloquent\Factories\Factory + */ + protected static function newFactory() + { + return ModelWithCustomNamespaceFactory::new(); + } +} +