diff --git a/README.md b/README.md index 3d66876..c25d5e2 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,26 @@ bin/console seeders -f -t xeeds # Force to generate a seeder from `xeeds` table in `dist/database/seeders` folder ``` +### Generate `Faker Seeders` + +The Faker seeders are utilized without the `factory()` method to generate seeds. This command was created to address the issue, providing insight into its purpose. + +```shell tab=Laravel +php artisan xeed:faker-seeders +# Generate all seeds from database in `database/seeders` folder + +php artisan xeed:faker-seeders -f -t xeeds +# Force to generate a seeder from `xeeds` table in `database/seeders` folder +``` + +```shell tab=Standalone +bin/console faker-seeders +# Generate all seeds from database in `dist/database/seeders` folder + +bin/console faker-seeders -f -t xeeds +# Force to generate a seeder from `xeeds` table in `dist/database/seeders` folder +``` + ### Generate `DatabaseSeeder` ```shell tab=Laravel @@ -257,6 +277,10 @@ bin/console xeed drop Utilize migration files for all database field types by referring to the following location `database/*.sql` these files are saved in the specified folder. +### Use `testorchestral/testbench` + +You can utilize `testorchestral/testbench` to execute tests. When running Laravel commands, the generated files are saved in the `vendor/orchestra/testbench-core/laravel/database` folder. + ## Credits - [Samgu Lee](https://github.com/cable8mm) diff --git a/bin/console b/bin/console index 9cdcd67..83ed9ab 100755 --- a/bin/console +++ b/bin/console @@ -6,6 +6,7 @@ require __DIR__.'/../vendor/autoload.php'; use Cable8mm\Xeed\Command\CleanCommand; use Cable8mm\Xeed\Command\GenerateDatabaseSeederCommand; use Cable8mm\Xeed\Command\GenerateFactoriesCommand; +use Cable8mm\Xeed\Command\GenerateFakerSeedersCommand; use Cable8mm\Xeed\Command\GenerateMigrationsCommand; use Cable8mm\Xeed\Command\GenerateModelsCommand; use Cable8mm\Xeed\Command\GenerateSeedersCommand; @@ -23,5 +24,6 @@ $application->add(new ImportXeedCommand()); $application->add(new CleanCommand()); $application->add(new GenerateFactoriesCommand()); $application->add(new GenerateMigrationsCommand()); +$application->add(new GenerateFakerSeedersCommand()); $application->run(); diff --git a/src/Command/GenerateFakerSeedersCommand.php b/src/Command/GenerateFakerSeedersCommand.php new file mode 100644 index 0000000..9925c89 --- /dev/null +++ b/src/Command/GenerateFakerSeedersCommand.php @@ -0,0 +1,78 @@ +safeLoad(); + + $this + ->addOption( + 'force', + 'f', + InputOption::VALUE_OPTIONAL, + 'Are files forcibly deleted even if they exist?', + false + )->addOption( + 'table', + 't', + InputOption::VALUE_OPTIONAL, + 'Are you generating the specific table with the faker seed?', + null + ); + } + + /** + * Run the console command. + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $force = $input->getOption('force') ?? true; + + $table = $input->getOption('table'); + + $tables = is_null($table) + ? Xeed::getInstance()->attach()->getTables() + : Xeed::getInstance()->attach($table)->getTables(); + + foreach ($tables as $table) { + try { + FakerSeederGenerator::make($table)->run(force: $force); + + $output->writeln(''.Path::seeder().DIRECTORY_SEPARATOR.$table->seeder().'.php have been generated.'.''); + } catch (\RuntimeException $e) { + $output->writeln(''.Path::seeder().DIRECTORY_SEPARATOR.$table->seeder().'.php file already exists.'.''); + } + } + + $output->writeln('generate-faker-seeders command executed successfully.'); + + return Command::SUCCESS; + } +} diff --git a/src/Generators/FakerSeederGenerator.php b/src/Generators/FakerSeederGenerator.php new file mode 100644 index 0000000..207490d --- /dev/null +++ b/src/Generators/FakerSeederGenerator.php @@ -0,0 +1,91 @@ +destination = Path::seeder(); + } + + if (is_null($namespace)) { + $this->namespace = '\App\Models'; + } + + $this->stub = File::system()->read(Path::stub().DIRECTORY_SEPARATOR.'FakerSeeder.stub'); + } + + /** + * Set count values for the generated stub + */ + public function count(int $count): static + { + $this->count = $count; + + return $this; + } + + /** + * {@inheritDoc} + */ + public function run(bool $force = false): void + { + $record = self::SUB_INTENT.'$records[] = ['.PHP_EOL; + + foreach ($this->table->getColumns() as $column) { + $record .= self::INTENT.$column->fake().PHP_EOL; + } + + $record = preg_replace('/\n$/', '', $record).PHP_EOL.self::SUB_INTENT.'];'; + + $seederClass = str_replace( + ['{class}', '{records}', '{table_name}', '{count}'], + [$this->table->model('Seeder'), $record, $this->table, $this->count], + $this->stub + ); + + File::system()->write( + $this->destination.DIRECTORY_SEPARATOR.$this->table->seeder('.php'), + $seederClass, + $force + ); + } + + /** + * {@inheritDoc} + */ + public static function make( + Table $table, + ?string $namespace = null, + ?string $destination = null + ): static { + return new self($table, $namespace, $destination); + } +} diff --git a/src/Laravel/Commands/GenerateFakerSeedersCommand.php b/src/Laravel/Commands/GenerateFakerSeedersCommand.php new file mode 100644 index 0000000..f94722f --- /dev/null +++ b/src/Laravel/Commands/GenerateFakerSeedersCommand.php @@ -0,0 +1,66 @@ +setAliases([ + 'xeed:fakers', + 'xeed:faker', + ]); + + parent::configure(); + } + + /** + * Execute the console command. + */ + public function handle(Xeed $xeed) + { + $force = $this->option('force') ?? false; + + $table = $this->option('table'); + + $tables = is_null($table) + ? $xeed->addPdo(DB::connection()->getPDO())->attach()->getTables() + : $xeed->addPdo(DB::connection()->getPDO())->attach($table)->getTables(); + + foreach ($tables as $table) { + try { + FakerSeederGenerator::make( + $table, + destination: database_path('seeders') + )->run(force: $force); + + $this->info(database_path('seeders').DIRECTORY_SEPARATOR.$table->seeder().'.php has been generated.'); + } catch (\Exception $e) { + $this->error(database_path('seeders').DIRECTORY_SEPARATOR.$table->seeder().'.php file already exists.'); + } + } + + return Command::SUCCESS; + } +} diff --git a/src/Laravel/XeedServiceProvider.php b/src/Laravel/XeedServiceProvider.php index 843a618..b5370a1 100644 --- a/src/Laravel/XeedServiceProvider.php +++ b/src/Laravel/XeedServiceProvider.php @@ -5,6 +5,7 @@ use Cable8mm\Xeed\Laravel\Commands\CleanCommand; use Cable8mm\Xeed\Laravel\Commands\GenerateDatabaseSeederCommand; use Cable8mm\Xeed\Laravel\Commands\GenerateFactoriesCommand; +use Cable8mm\Xeed\Laravel\Commands\GenerateFakerSeedersCommand; use Cable8mm\Xeed\Laravel\Commands\GenerateMigrationsCommand; use Cable8mm\Xeed\Laravel\Commands\GenerateModelsCommand; use Cable8mm\Xeed\Laravel\Commands\GenerateSeedersCommand; @@ -30,6 +31,7 @@ public function boot() GenerateSeedersCommand::class, ImportXeedCommand::class, CleanCommand::class, + GenerateFakerSeedersCommand::class, ]); } } diff --git a/stubs/FakerSeeder.stub b/stubs/FakerSeeder.stub new file mode 100644 index 0000000..ed48922 --- /dev/null +++ b/stubs/FakerSeeder.stub @@ -0,0 +1,25 @@ +truncate(); + + $records = []; + + for ($i = 0; $i < {count}; $i++) { +{records} + } + + DB::table('{table_name}')->insert($records); + } +} diff --git a/tests/Laravel/Commands/GenerateFakerSeedersCommandTest.php b/tests/Laravel/Commands/GenerateFakerSeedersCommandTest.php new file mode 100644 index 0000000..8eab7be --- /dev/null +++ b/tests/Laravel/Commands/GenerateFakerSeedersCommandTest.php @@ -0,0 +1,28 @@ +artisan('xeed:faker-seeders')->assertSuccessful(); + } + + public function test_execute_xeed_database_command_with_table() + { + $this->artisan('xeed:faker-seeders -t xeeds')->assertSuccessful(); + } + + public function test_execute_xeed_database_alias_command_with_table() + { + $this->artisan('xeed:faker -t xeeds')->assertSuccessful(); + } + + protected function getPackageProviders($app) + { + return [ + 'Cable8mm\Xeed\Laravel\XeedServiceProvider', + ]; + } +} diff --git a/tests/Unit/Generators/FakerSeederGeneratorTest.php b/tests/Unit/Generators/FakerSeederGeneratorTest.php new file mode 100644 index 0000000..d509c74 --- /dev/null +++ b/tests/Unit/Generators/FakerSeederGeneratorTest.php @@ -0,0 +1,33 @@ +run(); + } + + protected function tearDown(): void + { + // File::system()->delete(Path::testgen().DIRECTORY_SEPARATOR.'SampleSeeder.php'); + } + + public function test_it_can_can_generate_seeder_file(): void + { + $this->assertFileExists(Path::testgen().DIRECTORY_SEPARATOR.'SampleSeeder.php'); + } +}