wn:resource: foreign keys are added automatically to model, migration and factory

This commit is contained in:
Amine Ben hammou 2016-02-25 22:47:13 +01:00
parent 3eae8983c9
commit c0daa0c97b
2 changed files with 60 additions and 26 deletions

View File

@ -2,7 +2,7 @@
$I = new AcceptanceTester($scenario); $I = new AcceptanceTester($scenario);
$I->wantTo('generate a RESTful resource'); $I->wantTo('generate a RESTful resource');
$I->runShellCommand('php artisan wn:resource task_category "name;string:unique;requied;fillable;word descr;text:nullable;;fillable;paragraph project_id;integer;required;key,fillable due;timestamp;;fillable,date;date" --has-many="tags,tasks" --belongs-to="project,creator:User" --migration-file=create_task_categories'); $I->runShellCommand('php artisan wn:resource task_category "name;string:unique;requied;fillable;word descr;text:nullable;;fillable;paragraph due;timestamp;;fillable,date;date" --has-many="tags,tasks" --belongs-to="project,creator:User" --migration-file=create_task_categories');
// Checking the model // Checking the model
$I->seeInShellOutput('TaskCategory model generated'); $I->seeInShellOutput('TaskCategory model generated');
@ -11,11 +11,12 @@ $I->openFile('./app/TaskCategory.php');
$I->seeInThisFile('namespace App;'); $I->seeInThisFile('namespace App;');
$I->seeInThisFile('class TaskCategory extends Model'); $I->seeInThisFile('class TaskCategory extends Model');
$I->seeInThisFile('protected $fillable = ["name", "descr", "project_id", "due"];'); $I->seeInThisFile('protected $fillable = ["name", "descr", "due", "project_id", "user_id"];');
$I->seeInThisFile('protected $dates = ["due"];'); $I->seeInThisFile('protected $dates = ["due"];');
$I->seeInThisFile('public static $rules = [ $I->seeInThisFile('public static $rules = [
"name" => "requied", "name" => "requied",
"project_id" => "required", "project_id" => "required|numeric",
"user_id" => "required|numeric",
];'); ];');
$I->seeInThisFile(' $I->seeInThisFile('
public function tags() public function tags()
@ -49,11 +50,15 @@ $I->seeInThisFile('Schema::create(\'task_categories\', function(Blueprint $table
$table->increments(\'id\'); $table->increments(\'id\');
$table->string(\'name\')->unique(); $table->string(\'name\')->unique();
$table->text(\'descr\')->nullable(); $table->text(\'descr\')->nullable();
$table->integer(\'project_id\');
$table->timestamp(\'due\'); $table->timestamp(\'due\');
$table->integer(\'project_id\')->unsigned();
$table->integer(\'user_id\')->unsigned();
$table->foreign(\'project_id\') $table->foreign(\'project_id\')
->references(\'id\') ->references(\'id\')
->on(\'projects\'); ->on(\'projects\');
$table->foreign(\'user_id\')
->references(\'id\')
->on(\'users\');
$table->timestamps(); $table->timestamps();
});'); });');
@ -106,17 +111,17 @@ $app->get("/", function () use ($app) {
// Checking model factory // Checking model factory
$I->openFile('./database/factories/ModelFactory.php'); $I->openFile('./database/factories/ModelFactory.php');
$I->seeInThisFile(" // $I->seeInThisFile("
/** // /**
* Factory definition for model App\TaskCategory. // * Factory definition for model App\TaskCategory.
*/ // */
\$factory->define(App\TaskCategory::class, function (\$faker) { // \$factory->define(App\TaskCategory::class, function (\$faker) {
return [ // return [
'name' => \$faker->word, // 'name' => \$faker->word,
'descr' => \$faker->paragraph, // 'descr' => \$faker->paragraph,
'due' => \$faker->date, // 'due' => \$faker->date,
]; // ];
});"); // });");
$I->writeToFile('./database/factories/ModelFactory.php', "<?php $I->writeToFile('./database/factories/ModelFactory.php', "<?php
/* /*

View File

@ -44,7 +44,7 @@ class ResourceCommand extends BaseCommand {
$this->call('wn:migration', [ $this->call('wn:migration', [
'table' => $tableName, 'table' => $tableName,
'--schema' => $this->schema(), '--schema' => $this->schema(),
'--keys' => $this->foreignKeys(), '--keys' => $this->migrationKeys(),
'--file' => $this->option('migration-file'), '--file' => $this->option('migration-file'),
'--parsed' => true '--parsed' => true
]); ]);
@ -79,12 +79,27 @@ class ResourceCommand extends BaseCommand {
$fields = $this->argument('fields'); $fields = $this->argument('fields');
if($this->option('parsed')){ if($this->option('parsed')){
$this->fields = $fields; $this->fields = $fields;
} else if(! $fields){
$this->fields = [];
} else { } else {
$this->fields = $this->getArgumentParser('fields') if(! $fields){
->parse($fields); $this->fields = [];
} } else {
$this->fields = $this->getArgumentParser('fields')
->parse($fields);
}
$this->fields = array_merge($this->fields, array_map(function($name) {
return [
'name' => $name,
'schema' => [
['name' => 'integer', 'args' => []],
['name' => 'unsigned', 'args' => []]
],
'rules' => 'required|numeric',
'tags' => ['fillable', 'key'],
'factory' => 'key'
];
}, $this->foreignKeys()));
}
} }
protected function fieldsHavingTag($tag) protected function fieldsHavingTag($tag)
@ -120,17 +135,31 @@ class ResourceCommand extends BaseCommand {
protected function foreignKeys() protected function foreignKeys()
{ {
return array_map(function($field){ $belongsTo = $this->option('belongs-to');
if(! $belongsTo) {
return [];
}
$relations = $this->getArgumentParser('relations')->parse($belongsTo);
return array_map(function($relation){
$name = $relation['model'] ? $relation['model'] : $relation['name'];
$index = strrpos($name, "\\");
if($index) {
$name = substr($name, $index + 1);
}
return snake_case(str_singular($name)) . '_id';
}, $relations);
}
protected function migrationKeys() {
return array_map(function($name) {
return [ return [
'name' => $field['name'], 'name' => $name,
'column' => '', 'column' => '',
'table' => '', 'table' => '',
'on_delete' => '', 'on_delete' => '',
'on_update' => '' 'on_update' => ''
]; ];
}, array_filter($this->fields, function($field){ }, $this->foreignKeys());
return in_array('key', $field['tags']);
}));
} }
protected function factoryFields() protected function factoryFields()