Seeder generator added

This commit is contained in:
Amine Ben hammou 2016-02-19 23:31:29 +01:00
parent 8af58ea50a
commit 5605377f86
8 changed files with 58 additions and 66 deletions

4
.gitignore vendored
View File

@ -4,5 +4,5 @@
tests/_output/* tests/_output/*
lumen-test/app/ lumen-test/app/*
lumen-test/database/ lumen-test/database/*

View File

@ -0,0 +1,34 @@
<?php
$I = new AcceptanceTester($scenario);
$I->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');

View File

@ -66,6 +66,9 @@ class ResourceCommand extends BaseCommand {
// '--fields' => $this->factoryFields(), // '--fields' => $this->factoryFields(),
// '--parsed' => true // '--parsed' => true
// ]); // ]);
//
// generating table seeder
// ...
} }

View File

@ -5,60 +5,37 @@ class SeederCommand extends BaseCommand {
protected $signature = 'wn:seeder protected $signature = 'wn:seeder
{model : full qualified name of the model.} {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() public function handle()
{ {
$model = $this->argument('model'); $model = $this->argument('model');
$name = $this->getSeederName($model);
$file = "./database/seeds/{$name}.php";
$file = $this->getFile(); $content = $this->getTemplate('seeder')
$content = $this->fs->get($file);
$content .= $this->getTemplate('factory')
->with([ ->with([
'model' => $model, 'model' => $model,
'factory_fields' => $this->getFieldsContent() 'name' => $name,
'count' => $this->option('count')
]) ])
->get(); ->get();
$this->save($content, $file); $this->save($content, $file);
$this->info("{$model} factory generated !"); $this->info("{$name} generated !");
} }
protected function getFile() protected function getSeederName($name)
{ {
$file = $this->option('file'); $name = explode("\\", $name);
if(! $file){ $name = ucwords(str_plural($name[count($name) - 1]));
$file = './database/factories/ModelFactory.php'; $name = $name . 'TableSeeder';
} return $name;
return $file;
}
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;
} }
} }

View File

@ -16,7 +16,7 @@ class CommandsServiceProvider extends ServiceProvider
$this->registerResourcesCommand(); $this->registerResourcesCommand();
$this->registerPivotTableCommand(); $this->registerPivotTableCommand();
$this->registerFactoryCommand(); $this->registerFactoryCommand();
// $this->registerSeedCommand(); $this->registerSeederCommand();
// $this->registerTestCommand(); // $this->registerTestCommand();
} }
@ -48,11 +48,11 @@ class CommandsServiceProvider extends ServiceProvider
$this->commands('command.wn.migration'); $this->commands('command.wn.migration');
} }
protected function registerSeedCommand(){ protected function registerSeederCommand(){
$this->app->singleton('command.wn.seed', function($app){ $this->app->singleton('command.wn.seeder', function($app){
return $app['Wn\Generators\Commands\SeedCommand']; return $app['Wn\Generators\Commands\SeederCommand'];
}); });
$this->commands('command.wn.seed'); $this->commands('command.wn.seeder');
} }
protected function registerRouteCommand(){ protected function registerRouteCommand(){

View File

@ -1,21 +0,0 @@
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
{{seeders}}
Model::reguard();
}
}

View File

@ -1 +0,0 @@
$this->call({{name}}::class);

View File