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
8 changes: 3 additions & 5 deletions src/Generators/AbstractTestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ protected function getInserts(): array

protected function isFactoryExists(string $modelName): bool
{
$modelClass = $this->getModelClass($modelName);

return $this->classExists('factories', "{$modelName}Factory") && method_exists($modelClass, 'factory');
return $this->classExists('factories', "{$modelName}Factory");
}

protected function isMethodExists($modelName, $method): bool
Expand Down Expand Up @@ -172,8 +170,8 @@ protected function getMockModel($model): array
return [];
}

$modelClass = $this->getModelClass($model);
$factory = $modelClass::factory();
$factoryNamespace = "{$this->getOrCreateNamespace('factories')}\\{$model}Factory";
$factory = $factoryNamespace::new();

return $factory
->make()
Expand Down
1 change: 1 addition & 0 deletions src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function generate(): void
'namespace' => $this->getOrCreateNamespace('factories'),
'entity' => $this->model,
'fields' => $this->prepareFields(),
'modelNamespace' => $this->getOrCreateNamespace('models'),
]);

$this->saveClass('factories', "{$this->model}Factory", $factoryContent);
Expand Down
3 changes: 3 additions & 0 deletions stubs/factory.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factories\Factory;
use {{$modelNamespace}}\{{$entity}};

class {{$entity}}Factory extends Factory
{
protected $model = {{$entity}}::class;

public function definition(): array
{
@if(!empty($fields))
Expand Down
3 changes: 1 addition & 2 deletions stubs/model.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace {{$namespace}};

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use RonasIT\Support\Traits\ModelTrait;

class {{$entity}} extends Model
{
use HasFactory, ModelTrait;
use ModelTrait;

protected $fillable = [
@foreach($fields as $field)
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/FactoryGeneratorTest/post_factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Post;

class PostFactory extends Factory
{
protected $model = Post::class;

public function definition(): array
{
$faker = app(Faker::class);
Expand Down
Loading