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
24 changes: 14 additions & 10 deletions src/Commands/MakeEntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace RonasIT\Support\Commands;

use Closure;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Exceptions\EntityCreateException;
use RonasIT\Support\Generators\ControllerGenerator;
Expand Down Expand Up @@ -160,13 +162,22 @@ public function __construct()
/**
* Execute the console command.
*
* @return mixed
* @return void
*/
public function handle()
public function handle(): void
{
$this->validateInput();
$this->checkConfigs();
$this->eventDispatcher->listen(SuccessCreateMessage::class, $this->getSuccessMessageCallback());

$this->eventDispatcher->listen(
events: SuccessCreateMessage::class,
listener: fn (SuccessCreateMessage $event) => $this->info($event->message),
);

$this->eventDispatcher->listen(
events: WarningEvent::class,
listener: fn (WarningEvent $event) => $this->warn($event->message),
);

try {
$this->generate();
Expand Down Expand Up @@ -299,13 +310,6 @@ protected function getRelations()
];
}

protected function getSuccessMessageCallback()
{
return function (SuccessCreateMessage $event) {
$this->info($event->message);
};
}

protected function getFields()
{
return Arr::only($this->options(), EntityGenerator::AVAILABLE_FIELDS);
Expand Down
15 changes: 15 additions & 0 deletions src/Events/WarningEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace RonasIT\Support\Events;

use Illuminate\Queue\SerializesModels;

class WarningEvent
{
use SerializesModels;

public function __construct(
public readonly string $message,
) {
}
}
36 changes: 17 additions & 19 deletions src/Generators/SeederGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Arr;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\EntityCreateException;

class SeederGenerator extends EntityGenerator
Expand All @@ -15,13 +16,15 @@ public function __construct()
{
parent::__construct();

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

public function generate(): void
{
$this->checkConfigs();
if (!file_exists($this->seedsPath)) {
mkdir($this->seedsPath);
}

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

if (!is_dir($this->seedsPath)) {
mkdir($this->seedsPath);
}

$this->createEntitySeeder();

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

//@TODO: remove after implementing https://github.com/RonasIT/laravel-entity-generator/issues/93
if ($stubPath === 'entity-generator::database_seed_empty') {
$stubPath = 'entity-generator::database_empty_seeder';

$message = "You are using the deprecated value for 'entity-generator.stubs.database_empty_seeder' config. Please use 'entity-generator::database_empty_seeder'.";

event(new WarningEvent($message));
}

$content = "<?php \n\n" . view($stubPath, [
'namespace' => $this->getOrCreateNamespace('seeders')
])->render();
Expand All @@ -70,7 +78,7 @@ protected function createEntitySeeder(): void
'modelsNamespace' => $this->getOrCreateNamespace('models')
])->render();

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

file_put_contents($seederPath, $content);

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

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

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

file_put_contents($this->databaseSeederPath, $fixedContent);
}

protected function checkConfigs(): void
{
if (empty(config('entity-generator.stubs.seeder')) || empty(config('entity-generator.stubs.legacy_seeder'))) {
throw new EntityCreateException('
Looks like you have deprecated configs.
Please follow instructions(https://github.com/RonasIT/laravel-entity-generator/blob/master/ReadMe.md#13)
');
}
}
}
11 changes: 0 additions & 11 deletions tests/NovaTestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@ class NovaTestGeneratorTest extends TestCase
{
use NovaTestMockTrait;

public function setUp(): void
{
parent::setUp();

vfsStream::setup();

$this->generatedFileBasePath = vfsStream::url('root');

$this->app->setBasePath($this->generatedFileBasePath);
}

public function testCreateNovaTestsResourceNotExists()
{
$mock = $this->mockClassExistsFunction();
Expand Down
67 changes: 67 additions & 0 deletions tests/SeederGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace RonasIT\Support\Tests;

use Illuminate\Support\Facades\Event;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Generators\SeederGenerator;
use RonasIT\Support\Tests\Support\SeederGeneratorMockTrait;

class SeederGeneratorTest extends TestCase
{
use SeederGeneratorMockTrait;

public function setUp(): void
{
parent::setUp();

Event::fake();
}

public function testCreateSeeder()
{
$this->mockViewsNamespace();
$this->mockFilesystem();

app(SeederGenerator::class)
->setRelations([
'hasOne' => [],
'belongsTo' => ['User'],
'hasMany' => ['Comment'],
'belongsToMany' => []
])
->setModel('Post')
->generate();

$this->rollbackToDefaultBasePath();

$this->assertGeneratedFileEquals('database_seeder.php', 'database/seeders/DatabaseSeeder.php');
$this->assertGeneratedFileEquals('post_seeder.php', 'database/seeders/PostSeeder.php');
}

public function testCreateSeederWithOldConfig()
{
$this->mockViewsNamespace();
$this->mockFilesystem();

config([
'entity-generator.stubs.database_empty_seeder' => 'entity-generator::database_seed_empty',
]);

app(SeederGenerator::class)
->setRelations([
'hasOne' => [],
'belongsTo' => ['User'],
'hasMany' => ['Comment'],
'belongsToMany' => []
])
->setModel('Post')
->generate();

$this->rollbackToDefaultBasePath();

Event::assertDispatched(WarningEvent::class, function ($event) {
return $event->message === "You are using the deprecated value for 'entity-generator.stubs.database_empty_seeder' config. Please use 'entity-generator::database_empty_seeder'.";
});
}
}
23 changes: 23 additions & 0 deletions tests/Support/SeederGeneratorMockTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace RonasIT\Support\Tests\Support;

use Illuminate\Support\Facades\View;
use org\bovigo\vfs\vfsStream;

trait SeederGeneratorMockTrait
{
public function mockFilesystem(): void
{
$structure = [
'database' => []
];

vfsStream::create($structure);
}

public function mockViewsNamespace(): void
{
View::addNamespace('entity-generator', getcwd() . '/stubs');
}
}
21 changes: 21 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Foundation\Testing\Concerns\InteractsWithViews;
use Illuminate\Support\Str;
use Orchestra\Testbench\TestCase as BaseTestCase;
use org\bovigo\vfs\vfsStream;
use RonasIT\Support\Traits\FixturesTrait;

class TestCase extends BaseTestCase
Expand All @@ -16,11 +17,31 @@ class TestCase extends BaseTestCase
protected bool $globalExportMode = false;
protected string $generatedFileBasePath;

public function setUp(): void
{
parent::setUp();

$this->mockConfigurations();

vfsStream::setup();

$this->generatedFileBasePath = vfsStream::url('root');

$this->app->setBasePath($this->generatedFileBasePath);
}

public function rollbackToDefaultBasePath(): void
{
$this->app->setBasePath(getcwd());
}

public function mockConfigurations(): void
{
config([
'entity-generator' => include('config/entity-generator.php'),
]);
}

protected function getEnvironmentSetUp($app): void
{
$app->useEnvironmentPath(__DIR__ . '/..');
Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/SeederGeneratorTest/database_seeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call(PostSeeder::class);
}
}
21 changes: 21 additions & 0 deletions tests/fixtures/SeederGeneratorTest/post_seeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Post;

class PostSeeder extends Seeder
{
public function run()
{
Post::factory()->make([
'user_id' => \App\Models\User::factory()->create()->id,
]);

\App\Models\Comment::factory()->count(10)->make([
'post_id' => $post->id,
]);

}
}
Loading