Skip to content

Commit 9eed92a

Browse files
authored
Merge pull request #123 from RonasIT/100_fix_test_generation_when_model_in_not_default_directory
fix: test generation when model in not default directory
2 parents 9e1b452 + 7d65b79 commit 9eed92a

File tree

5 files changed

+11
-7
lines changed

5 files changed

+11
-7
lines changed

src/Generators/AbstractTestsGenerator.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ protected function getInserts(): array
9090

9191
protected function isFactoryExists(string $modelName): bool
9292
{
93-
$modelClass = $this->getModelClass($modelName);
94-
95-
return $this->classExists('factories', "{$modelName}Factory") && method_exists($modelClass, 'factory');
93+
return $this->classExists('factories', "{$modelName}Factory");
9694
}
9795

9896
protected function isMethodExists($modelName, $method): bool
@@ -172,8 +170,8 @@ protected function getMockModel($model): array
172170
return [];
173171
}
174172

175-
$modelClass = $this->getModelClass($model);
176-
$factory = $modelClass::factory();
173+
$factoryNamespace = "{$this->getOrCreateNamespace('factories')}\\{$model}Factory";
174+
$factory = $factoryNamespace::new();
177175

178176
return $factory
179177
->make()

src/Generators/FactoryGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function generate(): void
5151
'namespace' => $this->getOrCreateNamespace('factories'),
5252
'entity' => $this->model,
5353
'fields' => $this->prepareFields(),
54+
'modelNamespace' => $this->getOrCreateNamespace('models'),
5455
]);
5556

5657
$this->saveClass('factories', "{$this->model}Factory", $factoryContent);

stubs/factory.blade.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
use Faker\Generator as Faker;
44
use Illuminate\Database\Eloquent\Factories\Factory;
5+
use {{$modelNamespace}}\{{$entity}};
56

67
class {{$entity}}Factory extends Factory
78
{
9+
protected $model = {{$entity}}::class;
10+
811
public function definition(): array
912
{
1013
@if(!empty($fields))

stubs/model.blade.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
namespace {{$namespace}};
22

3-
use Illuminate\Database\Eloquent\Factories\HasFactory;
43
use Illuminate\Database\Eloquent\Model;
54
use RonasIT\Support\Traits\ModelTrait;
65

76
class {{$entity}} extends Model
87
{
9-
use HasFactory, ModelTrait;
8+
use ModelTrait;
109

1110
protected $fillable = [
1211
@foreach($fields as $field)

tests/fixtures/FactoryGeneratorTest/post_factory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
use Faker\Generator as Faker;
66
use Illuminate\Database\Eloquent\Factories\Factory;
7+
use App\Models\Post;
78

89
class PostFactory extends Factory
910
{
11+
protected $model = Post::class;
12+
1013
public function definition(): array
1114
{
1215
$faker = app(Faker::class);

0 commit comments

Comments
 (0)