Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 11 additions & 4 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\WarningMessage;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Exceptions\EntityCreateException;
use RonasIT\Support\Generators\ControllerGenerator;
Expand Down Expand Up @@ -166,7 +168,9 @@ public function handle()
{
$this->validateInput();
$this->checkConfigs();

$this->eventDispatcher->listen(SuccessCreateMessage::class, $this->getSuccessMessageCallback());
$this->eventDispatcher->listen(WarningMessage::class, $this->getWarningMessageCallback());

try {
$this->generate();
Expand Down Expand Up @@ -299,11 +303,14 @@ protected function getRelations()
];
}

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

protected function getWarningMessageCallback(): Closure
{
return function (SuccessCreateMessage $event) {
$this->info($event->message);
};
return fn (WarningMessage $event) => $this->warn($event->message);
}

protected function getFields()
Expand Down
22 changes: 22 additions & 0 deletions src/Events/WarningMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace RonasIT\Support\Events;

use Illuminate\Queue\SerializesModels;

class WarningMessage
{
use SerializesModels;

public string $message;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct($message)
{
$this->message = $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\WarningMessage;
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 WarningMessage($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
61 changes: 61 additions & 0 deletions tests/SeederGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace RonasIT\Support\Tests;

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

class SeederGeneratorTest extends TestCase
{
use SeederGeneratorMockTrait;

public function testCreateSeeder()
{
$this->mockViewsNamespace();
$this->mockConfigurations();
$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()
{
Event::fake();
$this->mockViewsNamespace();
$this->mockConfigurations();
$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(WarningMessage::class);
}
}
38 changes: 38 additions & 0 deletions tests/Support/SeederGeneratorMockTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace RonasIT\Support\Tests\Support;

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

trait SeederGeneratorMockTrait
{
public function mockConfigurations(): void
{
config([
'entity-generator.stubs.database_empty_seeder' => 'entity-generator::database_empty_seeder',
'entity-generator.stubs.legacy_seeder' => 'entity-generator::legacy_seeder',
'entity-generator.stubs.resource' => 'entity-generator::resource',
'entity-generator.stubs.seeder' => 'entity-generator::seeder',
'entity-generator.paths' => [
'seeders' => 'database/seeders',
'models' => 'app/Models',
'database_seeder' => 'database/seeders/DatabaseSeeder.php',
]
]);
}

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

vfsStream::create($structure);
}

public function mockViewsNamespace(): void
{
View::addNamespace('entity-generator', getcwd() . '/stubs');
}
}
12 changes: 12 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,6 +17,17 @@ class TestCase extends BaseTestCase
protected bool $globalExportMode = false;
protected string $generatedFileBasePath;

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

vfsStream::setup();

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

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

public function rollbackToDefaultBasePath(): void
{
$this->app->setBasePath(getcwd());
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