From 5605377f86bed6dc54f32472d7900e524221762a Mon Sep 17 00:00:00 2001 From: Amine Ben hammou Date: Fri, 19 Feb 2016 23:31:29 +0100 Subject: [PATCH] Seeder generator added --- .gitignore | 4 +- .../tests/acceptance/SeederCommandCept.php | 34 +++++++++++++ src/Commands/ResourceCommand.php | 3 ++ src/Commands/SeederCommand.php | 51 +++++-------------- src/CommandsServiceProvider.php | 10 ++-- templates/db-seeder.wnt | 21 -------- templates/db-seeder/seeder.wnt | 1 - templates/seed.wnt | 0 8 files changed, 58 insertions(+), 66 deletions(-) create mode 100644 lumen-test/tests/acceptance/SeederCommandCept.php delete mode 100644 templates/db-seeder.wnt delete mode 100644 templates/db-seeder/seeder.wnt delete mode 100644 templates/seed.wnt diff --git a/.gitignore b/.gitignore index c669ab9..9092799 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ tests/_output/* -lumen-test/app/ -lumen-test/database/ \ No newline at end of file +lumen-test/app/* +lumen-test/database/* \ No newline at end of file diff --git a/lumen-test/tests/acceptance/SeederCommandCept.php b/lumen-test/tests/acceptance/SeederCommandCept.php new file mode 100644 index 0000000..cc712dd --- /dev/null +++ b/lumen-test/tests/acceptance/SeederCommandCept.php @@ -0,0 +1,34 @@ +wantTo('generate a seeder with default options'); +$I->runShellCommand('php artisan wn:seeder "App\Task"'); +$I->seeInShellOutput('TaskSeeder generated'); +$I->openFile('./database/seeds/TasksTableSeeder.php'); +$I->seeInThisFile(' +use Illuminate\Database\Seeder; + +class TasksTableSeeder extends Seeder +{ + public function run() + { + factory(App\Task::class, 10)->create(); + } +}'); +$I->deleteFile('./database/seeds/TasksTableSeeder.php'); + +$I->wantTo('generate a seeder with custom options'); +$I->runShellCommand('php artisan wn:seeder "App\Category" --count=25'); +$I->seeInShellOutput('CategoriesTableSeeder generated'); +$I->openFile('./database/seeds/CategoriesTableSeeder.php'); +$I->seeInThisFile(' +use Illuminate\Database\Seeder; + +class CategoriesTableSeeder extends Seeder +{ + public function run() + { + factory(App\Task::class, 25)->create(); + } +}'); +$I->deleteFile('./database/seeds/CategoriesTableSeeder.php'); diff --git a/src/Commands/ResourceCommand.php b/src/Commands/ResourceCommand.php index 0d8b0b9..b8d19b4 100644 --- a/src/Commands/ResourceCommand.php +++ b/src/Commands/ResourceCommand.php @@ -66,6 +66,9 @@ class ResourceCommand extends BaseCommand { // '--fields' => $this->factoryFields(), // '--parsed' => true // ]); + // + // generating table seeder + // ... } diff --git a/src/Commands/SeederCommand.php b/src/Commands/SeederCommand.php index d076f3d..31be03d 100644 --- a/src/Commands/SeederCommand.php +++ b/src/Commands/SeederCommand.php @@ -5,60 +5,37 @@ class SeederCommand extends BaseCommand { protected $signature = 'wn:seeder {model : full qualified name of the model.} - {--cout=10 : number of elements to add in database.} + {--count=10 : number of elements to add in database.} '; - protected $description = 'Generates a model factory'; + protected $description = 'Generates a seeder'; public function handle() { $model = $this->argument('model'); + $name = $this->getSeederName($model); + $file = "./database/seeds/{$name}.php"; - $file = $this->getFile(); - - $content = $this->fs->get($file); - - $content .= $this->getTemplate('factory') + $content = $this->getTemplate('seeder') ->with([ 'model' => $model, - 'factory_fields' => $this->getFieldsContent() + 'name' => $name, + 'count' => $this->option('count') ]) ->get(); + $this->save($content, $file); - $this->info("{$model} factory generated !"); + $this->info("{$name} generated !"); } - protected function getFile() + protected function getSeederName($name) { - $file = $this->option('file'); - if(! $file){ - $file = './database/factories/ModelFactory.php'; - } - return $file; + $name = explode("\\", $name); + $name = ucwords(str_plural($name[count($name) - 1])); + $name = $name . 'TableSeeder'; + return $name; } - protected function getFieldsContent() - { - $content = []; - - $fields = $this->option('fields'); - - if($fields){ - if(! $this->option('parsed')){ - $fields = $this->getArgumentParser('factory-fields')->parse($fields); - } - $template = $this->getTemplate('factory/field'); - foreach($fields as $field){ - $content[] = $template->with($field)->get(); - } - $content = implode(PHP_EOL, $content); - } else { - $content = "\t\t// Fields here"; - } - - return $content; - } - } \ No newline at end of file diff --git a/src/CommandsServiceProvider.php b/src/CommandsServiceProvider.php index e2835b9..dba4203 100644 --- a/src/CommandsServiceProvider.php +++ b/src/CommandsServiceProvider.php @@ -16,7 +16,7 @@ class CommandsServiceProvider extends ServiceProvider $this->registerResourcesCommand(); $this->registerPivotTableCommand(); $this->registerFactoryCommand(); - // $this->registerSeedCommand(); + $this->registerSeederCommand(); // $this->registerTestCommand(); } @@ -48,11 +48,11 @@ class CommandsServiceProvider extends ServiceProvider $this->commands('command.wn.migration'); } - protected function registerSeedCommand(){ - $this->app->singleton('command.wn.seed', function($app){ - return $app['Wn\Generators\Commands\SeedCommand']; + protected function registerSeederCommand(){ + $this->app->singleton('command.wn.seeder', function($app){ + return $app['Wn\Generators\Commands\SeederCommand']; }); - $this->commands('command.wn.seed'); + $this->commands('command.wn.seeder'); } protected function registerRouteCommand(){ diff --git a/templates/db-seeder.wnt b/templates/db-seeder.wnt deleted file mode 100644 index 305d76b..0000000 --- a/templates/db-seeder.wnt +++ /dev/null @@ -1,21 +0,0 @@ -call({{name}}::class); \ No newline at end of file diff --git a/templates/seed.wnt b/templates/seed.wnt deleted file mode 100644 index e69de29..0000000