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/*
lumen-test/app/
lumen-test/database/
lumen-test/app/*
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(),
// '--parsed' => true
// ]);
//
// generating table seeder
// ...
}

View File

@ -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;
}
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;
$name = explode("\\", $name);
$name = ucwords(str_plural($name[count($name) - 1]));
$name = $name . 'TableSeeder';
return $name;
}
}

View File

@ -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(){

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