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->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
$I->seeInShellOutput('TaskCategory model generated');
@ -11,11 +11,12 @@ $I->openFile('./app/TaskCategory.php');
$I->seeInThisFile('namespace App;');
$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('public static $rules = [
"name" => "requied",
"project_id" => "required",
"project_id" => "required|numeric",
"user_id" => "required|numeric",
];');
$I->seeInThisFile('
public function tags()
@ -49,11 +50,15 @@ $I->seeInThisFile('Schema::create(\'task_categories\', function(Blueprint $table
$table->increments(\'id\');
$table->string(\'name\')->unique();
$table->text(\'descr\')->nullable();
$table->integer(\'project_id\');
$table->timestamp(\'due\');
$table->integer(\'project_id\')->unsigned();
$table->integer(\'user_id\')->unsigned();
$table->foreign(\'project_id\')
->references(\'id\')
->on(\'projects\');
$table->foreign(\'user_id\')
->references(\'id\')
->on(\'users\');
$table->timestamps();
});');
@ -106,17 +111,17 @@ $app->get("/", function () use ($app) {
// Checking model factory
$I->openFile('./database/factories/ModelFactory.php');
$I->seeInThisFile("
/**
* Factory definition for model App\TaskCategory.
*/
\$factory->define(App\TaskCategory::class, function (\$faker) {
return [
'name' => \$faker->word,
'descr' => \$faker->paragraph,
'due' => \$faker->date,
];
});");
// $I->seeInThisFile("
// /**
// * Factory definition for model App\TaskCategory.
// */
// \$factory->define(App\TaskCategory::class, function (\$faker) {
// return [
// 'name' => \$faker->word,
// 'descr' => \$faker->paragraph,
// 'due' => \$faker->date,
// ];
// });");
$I->writeToFile('./database/factories/ModelFactory.php', "<?php
/*

View File

@ -44,7 +44,7 @@ class ResourceCommand extends BaseCommand {
$this->call('wn:migration', [
'table' => $tableName,
'--schema' => $this->schema(),
'--keys' => $this->foreignKeys(),
'--keys' => $this->migrationKeys(),
'--file' => $this->option('migration-file'),
'--parsed' => true
]);
@ -79,12 +79,27 @@ class ResourceCommand extends BaseCommand {
$fields = $this->argument('fields');
if($this->option('parsed')){
$this->fields = $fields;
} else if(! $fields){
$this->fields = [];
} else {
$this->fields = $this->getArgumentParser('fields')
->parse($fields);
}
if(! $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)
@ -120,17 +135,31 @@ class ResourceCommand extends BaseCommand {
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 [
'name' => $field['name'],
'name' => $name,
'column' => '',
'table' => '',
'on_delete' => '',
'on_update' => ''
];
}, array_filter($this->fields, function($field){
return in_array('key', $field['tags']);
}));
}, $this->foreignKeys());
}
protected function factoryFields()