Skip to content

Commit

Permalink
Fix class factory generation - closes #15.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDoctor0 committed Aug 14, 2022
1 parent f112ff5 commit 19ae15a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
14 changes: 8 additions & 6 deletions resources/views/class-factory.blade.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
namespace {{'Database\\Factories'. $append}};
declare(strict_types=1);

use Illuminate\Database\Eloquent\Factories\Factory;
namespace {{ $namespace }};

use {{ $name }};
@isset($properties['remember_token'])
use Illuminate\Support\Str;
@endisset
use {{ $reflection->getName() }};
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<{{ $reflection->getShortName() }}>
* @@extends Factory<\{{ $name }}>
*/
final class {{ $reflection->getShortName() }}Factory extends Factory
final class {{ $shortName }}Factory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = {{ $reflection->getShortName() }}::class;
protected $model = {{ $shortName }}::class;

/**
* Define the model's default state.
Expand Down
4 changes: 3 additions & 1 deletion resources/views/method-factory.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
declare(strict_types=1);

use Faker\Generator as Faker;
@isset($properties['remember_token'])
use Illuminate\Support\Str;
@endisset

/* @var $factory \Illuminate\Database\Eloquent\Factory */
$factory->define({{ $reflection->getName() }}::class, function (Faker $faker) {
$factory->define({{ $name }}::class, function (Faker $faker) {
return [
@foreach($properties as $name => $property)
'{{$name}}' => {!! $property !!},
Expand Down
5 changes: 3 additions & 2 deletions src/Console/GenerateFactoryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,10 @@ protected function setProperty(Model $model, string $field, string $type): void
protected function createFactory(ReflectionClass $reflection): string
{
return $this->view->make($this->factoryView(), [
'reflection' => $reflection,
'properties' => $this->properties,
'append' => $this->generateAdditionalNameSpace($reflection->getName()),
'name' => $reflection->getName(),
'shortName' => $reflection->getShortName(),
'namespace' => 'Database\\Factories' . $this->generateAdditionalNameSpace($reflection->getName()),
])->render();
}

Expand Down

0 comments on commit 19ae15a

Please sign in to comment.