Skip to content

Commit 78b3590

Browse files
authored
Merge pull request #95 from RonasIT/fix/create-database-seeder
fix: createDatabaseSeeder
2 parents feb591c + ab647ee commit 78b3590

File tree

9 files changed

+196
-40
lines changed

9 files changed

+196
-40
lines changed

src/Commands/MakeEntityCommand.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace RonasIT\Support\Commands;
44

5+
use Closure;
56
use Illuminate\Console\Command;
67
use Illuminate\Support\Arr;
78
use Illuminate\Support\Facades\Config;
89
use Illuminate\Support\Str;
910
use RonasIT\Support\Events\SuccessCreateMessage;
11+
use RonasIT\Support\Events\WarningEvent;
1012
use RonasIT\Support\Exceptions\ClassNotExistsException;
1113
use RonasIT\Support\Exceptions\EntityCreateException;
1214
use RonasIT\Support\Generators\ControllerGenerator;
@@ -160,13 +162,22 @@ public function __construct()
160162
/**
161163
* Execute the console command.
162164
*
163-
* @return mixed
165+
* @return void
164166
*/
165-
public function handle()
167+
public function handle(): void
166168
{
167169
$this->validateInput();
168170
$this->checkConfigs();
169-
$this->eventDispatcher->listen(SuccessCreateMessage::class, $this->getSuccessMessageCallback());
171+
172+
$this->eventDispatcher->listen(
173+
events: SuccessCreateMessage::class,
174+
listener: fn (SuccessCreateMessage $event) => $this->info($event->message),
175+
);
176+
177+
$this->eventDispatcher->listen(
178+
events: WarningEvent::class,
179+
listener: fn (WarningEvent $event) => $this->warn($event->message),
180+
);
170181

171182
try {
172183
$this->generate();
@@ -299,13 +310,6 @@ protected function getRelations()
299310
];
300311
}
301312

302-
protected function getSuccessMessageCallback()
303-
{
304-
return function (SuccessCreateMessage $event) {
305-
$this->info($event->message);
306-
};
307-
}
308-
309313
protected function getFields()
310314
{
311315
return Arr::only($this->options(), EntityGenerator::AVAILABLE_FIELDS);

src/Events/WarningEvent.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
7+
class WarningEvent
8+
{
9+
use SerializesModels;
10+
11+
public function __construct(
12+
public readonly string $message,
13+
) {
14+
}
15+
}

src/Generators/SeederGenerator.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Arr;
66
use RonasIT\Support\Events\SuccessCreateMessage;
7+
use RonasIT\Support\Events\WarningEvent;
78
use RonasIT\Support\Exceptions\EntityCreateException;
89

910
class SeederGenerator extends EntityGenerator
@@ -15,13 +16,15 @@ public function __construct()
1516
{
1617
parent::__construct();
1718

18-
$this->seedsPath = Arr::get($this->paths, 'seeders', 'database/seeders');
19-
$this->databaseSeederPath = Arr::get($this->paths, 'database_seeder', 'database/seeders/DatabaseSeeder.php');
19+
$this->seedsPath = base_path(Arr::get($this->paths, 'seeders', 'database/seeders'));
20+
$this->databaseSeederPath = base_path(Arr::get($this->paths, 'database_seeder', 'database/seeders/DatabaseSeeder.php'));
2021
}
2122

2223
public function generate(): void
2324
{
24-
$this->checkConfigs();
25+
if (!file_exists($this->seedsPath)) {
26+
mkdir($this->seedsPath);
27+
}
2528

2629
if (!file_exists($this->databaseSeederPath)) {
2730
list($basePath, $databaseSeederDir) = extract_last_part($this->databaseSeederPath, '/');
@@ -33,10 +36,6 @@ public function generate(): void
3336
$this->createDatabaseSeeder();
3437
}
3538

36-
if (!is_dir($this->seedsPath)) {
37-
mkdir($this->seedsPath);
38-
}
39-
4039
$this->createEntitySeeder();
4140

4241
$this->appendSeederToList();
@@ -46,6 +45,15 @@ protected function createDatabaseSeeder(): void
4645
{
4746
$stubPath = config('entity-generator.stubs.database_empty_seeder');
4847

48+
//@TODO: remove after implementing https://github.com/RonasIT/laravel-entity-generator/issues/93
49+
if ($stubPath === 'entity-generator::database_seed_empty') {
50+
$stubPath = 'entity-generator::database_empty_seeder';
51+
52+
$message = "You are using the deprecated value for 'entity-generator.stubs.database_empty_seeder' config. Please use 'entity-generator::database_empty_seeder'.";
53+
54+
event(new WarningEvent($message));
55+
}
56+
4957
$content = "<?php \n\n" . view($stubPath, [
5058
'namespace' => $this->getOrCreateNamespace('seeders')
5159
])->render();
@@ -70,7 +78,7 @@ protected function createEntitySeeder(): void
7078
'modelsNamespace' => $this->getOrCreateNamespace('models')
7179
])->render();
7280

73-
$seederPath = base_path("{$this->seedsPath}/{$this->model}Seeder.php");
81+
$seederPath = "{$this->seedsPath}/{$this->model}Seeder.php";
7482

7583
file_put_contents($seederPath, $content);
7684

@@ -83,20 +91,10 @@ protected function appendSeederToList(): void
8391
{
8492
$content = file_get_contents($this->databaseSeederPath);
8593

86-
$insertContent = "\n \$this->call({$this->model}Seeder::class);\n }\n}";
94+
$insertContent = " \$this->call({$this->model}Seeder::class);\n }\n}";
8795

8896
$fixedContent = preg_replace('/\}\s*\}\s*\z/', $insertContent, $content);
8997

9098
file_put_contents($this->databaseSeederPath, $fixedContent);
9199
}
92-
93-
protected function checkConfigs(): void
94-
{
95-
if (empty(config('entity-generator.stubs.seeder')) || empty(config('entity-generator.stubs.legacy_seeder'))) {
96-
throw new EntityCreateException('
97-
Looks like you have deprecated configs.
98-
Please follow instructions(https://github.com/RonasIT/laravel-entity-generator/blob/master/ReadMe.md#13)
99-
');
100-
}
101-
}
102100
}

tests/NovaTestGeneratorTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,6 @@ class NovaTestGeneratorTest extends TestCase
1212
{
1313
use NovaTestMockTrait;
1414

15-
public function setUp(): void
16-
{
17-
parent::setUp();
18-
19-
vfsStream::setup();
20-
21-
$this->generatedFileBasePath = vfsStream::url('root');
22-
23-
$this->app->setBasePath($this->generatedFileBasePath);
24-
}
25-
2615
public function testCreateNovaTestsResourceNotExists()
2716
{
2817
$mock = $this->mockClassExistsFunction();

tests/SeederGeneratorTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests;
4+
5+
use Illuminate\Support\Facades\Event;
6+
use RonasIT\Support\Events\WarningEvent;
7+
use RonasIT\Support\Generators\SeederGenerator;
8+
use RonasIT\Support\Tests\Support\SeederGeneratorMockTrait;
9+
10+
class SeederGeneratorTest extends TestCase
11+
{
12+
use SeederGeneratorMockTrait;
13+
14+
public function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
Event::fake();
19+
}
20+
21+
public function testCreateSeeder()
22+
{
23+
$this->mockViewsNamespace();
24+
$this->mockFilesystem();
25+
26+
app(SeederGenerator::class)
27+
->setRelations([
28+
'hasOne' => [],
29+
'belongsTo' => ['User'],
30+
'hasMany' => ['Comment'],
31+
'belongsToMany' => []
32+
])
33+
->setModel('Post')
34+
->generate();
35+
36+
$this->rollbackToDefaultBasePath();
37+
38+
$this->assertGeneratedFileEquals('database_seeder.php', 'database/seeders/DatabaseSeeder.php');
39+
$this->assertGeneratedFileEquals('post_seeder.php', 'database/seeders/PostSeeder.php');
40+
}
41+
42+
public function testCreateSeederWithOldConfig()
43+
{
44+
$this->mockViewsNamespace();
45+
$this->mockFilesystem();
46+
47+
config([
48+
'entity-generator.stubs.database_empty_seeder' => 'entity-generator::database_seed_empty',
49+
]);
50+
51+
app(SeederGenerator::class)
52+
->setRelations([
53+
'hasOne' => [],
54+
'belongsTo' => ['User'],
55+
'hasMany' => ['Comment'],
56+
'belongsToMany' => []
57+
])
58+
->setModel('Post')
59+
->generate();
60+
61+
$this->rollbackToDefaultBasePath();
62+
63+
Event::assertDispatched(WarningEvent::class, function ($event) {
64+
return $event->message === "You are using the deprecated value for 'entity-generator.stubs.database_empty_seeder' config. Please use 'entity-generator::database_empty_seeder'.";
65+
});
66+
}
67+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests\Support;
4+
5+
use Illuminate\Support\Facades\View;
6+
use org\bovigo\vfs\vfsStream;
7+
8+
trait SeederGeneratorMockTrait
9+
{
10+
public function mockFilesystem(): void
11+
{
12+
$structure = [
13+
'database' => []
14+
];
15+
16+
vfsStream::create($structure);
17+
}
18+
19+
public function mockViewsNamespace(): void
20+
{
21+
View::addNamespace('entity-generator', getcwd() . '/stubs');
22+
}
23+
}

tests/TestCase.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Foundation\Testing\Concerns\InteractsWithViews;
77
use Illuminate\Support\Str;
88
use Orchestra\Testbench\TestCase as BaseTestCase;
9+
use org\bovigo\vfs\vfsStream;
910
use RonasIT\Support\Traits\FixturesTrait;
1011

1112
class TestCase extends BaseTestCase
@@ -16,11 +17,31 @@ class TestCase extends BaseTestCase
1617
protected bool $globalExportMode = false;
1718
protected string $generatedFileBasePath;
1819

20+
public function setUp(): void
21+
{
22+
parent::setUp();
23+
24+
$this->mockConfigurations();
25+
26+
vfsStream::setup();
27+
28+
$this->generatedFileBasePath = vfsStream::url('root');
29+
30+
$this->app->setBasePath($this->generatedFileBasePath);
31+
}
32+
1933
public function rollbackToDefaultBasePath(): void
2034
{
2135
$this->app->setBasePath(getcwd());
2236
}
2337

38+
public function mockConfigurations(): void
39+
{
40+
config([
41+
'entity-generator' => include('config/entity-generator.php'),
42+
]);
43+
}
44+
2445
protected function getEnvironmentSetUp($app): void
2546
{
2647
$app->useEnvironmentPath(__DIR__ . '/..');
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use Illuminate\Database\Seeder;
6+
7+
class DatabaseSeeder extends Seeder
8+
{
9+
/**
10+
* Run the database seeds.
11+
*
12+
* @return void
13+
*/
14+
public function run()
15+
{
16+
$this->call(PostSeeder::class);
17+
}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use Illuminate\Database\Seeder;
6+
use App\Models\Post;
7+
8+
class PostSeeder extends Seeder
9+
{
10+
public function run()
11+
{
12+
Post::factory()->make([
13+
'user_id' => \App\Models\User::factory()->create()->id,
14+
]);
15+
16+
\App\Models\Comment::factory()->count(10)->make([
17+
'post_id' => $post->id,
18+
]);
19+
20+
}
21+
}

0 commit comments

Comments
 (0)