wn:model command done

This commit is contained in:
Amine Ben hammou 2015-09-23 15:53:55 +01:00
parent 4a60029604
commit 96b1866e35
9 changed files with 101 additions and 82 deletions

9
formats/rules.json Normal file
View File

@ -0,0 +1,9 @@
{
"type": "array",
"separator": " ",
"fields": {
"type": "object",
"separator": "=",
"fields": ["name","rule"]
}
}

View File

@ -1,21 +0,0 @@
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Test extends Model {
protected $fillable = [];
protected $dates = [];
public function users()
{
return $this->hasMany('App\User');
}
public function number()
{
return $this->hasMany('Phone');
}
}

View File

@ -3699,12 +3699,12 @@
"source": {
"type": "git",
"url": "https://github.com/webNeat/lumen-generators.git",
"reference": "04ce3077cd2723b38424e1ea94844582b67c6b79"
"reference": "4a60029604f1b12d81e135761dc255fe2f500b04"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/webNeat/lumen-generators/zipball/04ce3077cd2723b38424e1ea94844582b67c6b79",
"reference": "04ce3077cd2723b38424e1ea94844582b67c6b79",
"url": "https://api.github.com/repos/webNeat/lumen-generators/zipball/4a60029604f1b12d81e135761dc255fe2f500b04",
"reference": "4a60029604f1b12d81e135761dc255fe2f500b04",
"shasum": ""
},
"require": {
@ -3736,7 +3736,7 @@
"lumen",
"rest"
],
"time": "2015-09-23 03:58:39"
"time": "2015-09-23 04:38:56"
}
],
"aliases": [],

View File

@ -6,6 +6,7 @@ $I->runShellCommand('php artisan wn:model TestingModel --path=tests/tmp');
$I->seeInShellOutput('Model TestingModel Generated');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
$I->seeFileContentsEqual('<?php namespace Tests\Tmp;
use Illuminate\Database\Eloquent\Model;
@ -15,7 +16,13 @@ class TestingModel extends Model {
protected $fillable = [];
protected $dates = [];
public $rules = [
// Validation rules
];
// Relationships
}
');
@ -24,63 +31,48 @@ $I->runShellCommand('php artisan wn:model TestingModel --fillable=name,title --p
$I->seeInShellOutput('Model TestingModel Generated');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
$I->seeFileContentsEqual('<?php namespace Tests\Tmp;
use Illuminate\Database\Eloquent\Model;
class TestingModel extends Model {
protected $fillable = ["name", "title"];
protected $dates = [];
}
');
$I->seeInThisFile('protected $fillable = ["name", "title"];');
$I->wantTo('generate a model with dates fields');
$I->runShellCommand('php artisan wn:model TestingModel --dates=started_at --path=tests/tmp');
$I->seeInShellOutput('Model TestingModel Generated');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
$I->seeFileContentsEqual('<?php namespace Tests\Tmp;
use Illuminate\Database\Eloquent\Model;
class TestingModel extends Model {
protected $fillable = [];
protected $dates = ["started_at"];
}
');
$I->seeInThisFile('protected $dates = ["started_at"];');
$I->wantTo('generate a model with relations');
$I->runShellCommand('php artisan wn:model TestingModel --has-many=accounts,friends:App\User,numbers:Phone --path=tests/tmp');
$I->runShellCommand('php artisan wn:model TestingModel --has-many=accounts --belongs-to="owner:App\User" --has-one=number:Phone --path=tests/tmp');
$I->seeInShellOutput('Model TestingModel Generated');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
$I->seeFileContentsEqual('<?php namespace Tests\Tmp;
use Illuminate\Database\Eloquent\Model;
class TestingModel extends Model {
protected $fillable = [];
protected $dates = [];
public function accounts(){
$I->seeInThisFile('
public function accounts()
{
return $this->hasMany("Tests\\Tmp\\Account");
}
public function friends(){
return $this->hasMany("App\\User");
');
$I->seeInThisFile('
public function owner()
{
return $this->belongsTo("App\\User");
}
public function numbers(){
return $this->hasMany("Tests\\Tmp\\Phone");
');
$I->seeInThisFile('
public function number()
{
return $this->hasOne("Tests\\Tmp\\Phone");
}
');
}
');
$I->wantTo('generate a model with validation rules');
$I->runShellCommand('php artisan wn:model TestingModel --rules="name=required age=integer|min:13 email=email|unique:users,email_address" --path=tests/tmp');
$I->seeInShellOutput('Model TestingModel Generated');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
$I->seeInThisFile('
public $rules = [
"name" => "required",
"age" => "integer|min:13",
"email" => "email|unique:users,email_address",
];
');

View File

@ -8,6 +8,12 @@ class TestingModel extends Model {
protected $dates = [];
public $rules = [
"name" => "required",
"age" => "integer|min:13",
"email" => "email|unique:users,email_address",
];
// Relationships
}

View File

@ -4,11 +4,14 @@
class ModelCommand extends BaseCommand {
protected $signature = 'wn:model
{name : Name of the model}
{--fillable= : the fillable fields of the model}
{--dates= : date fields of the model}
{--has-many= : on-to-many relationships of the model}
{--path=app : where to store the model php file}';
{name : Name of the model.}
{--fillable= : the fillable fields.}
{--dates= : date fields.}
{--has-many= : hasMany relationships.}
{--has-one= : hasOne relationships.}
{--belongs-to= : belongsTo relationships.}
{--rules= : fields validation rules.}
{--path=app : where to store the model php file.}';
protected $description = 'Generates a model class for a RESTfull resource';
@ -25,7 +28,8 @@ class ModelCommand extends BaseCommand {
'namespace' => $this->getNamespace(),
'fillable' => $this->getAsArrayFields('fillable'),
'dates' => $this->getAsArrayFields('dates'),
'relations' => $this->getRelations()
'relations' => $this->getRelations(),
'rules' => $this->getRules()
])
->get();
@ -55,10 +59,16 @@ class ModelCommand extends BaseCommand {
protected function getRelations()
{
$relations = array_merge([],
$this->getRelationsByType('hasMany', 'has-many')
$this->getRelationsByType('hasOne', 'has-one'),
$this->getRelationsByType('hasMany', 'has-many'),
$this->getRelationsByType('belongsTo', 'belongs-to')
);
return implode("\n\n", $relations);
if(empty($relations)){
return "\t// Relationships";
}
return implode(PHP_EOL, $relations);
}
protected function getRelationsByType($type, $option)
@ -81,4 +91,22 @@ class ModelCommand extends BaseCommand {
}
return $relations;
}
protected function getRules()
{
$rules = $this->option('rules');
if(! $rules){
return "\t\t// Validation rules";
}
$parser = $this->getArgumentParser('rules');
$template = $this->getTemplate('model/rule');
$items = $parser->parse($rules);
$rules = [];
foreach ($items as $item) {
$rules[] = $template->with($item)->get();
}
return implode(PHP_EOL, $rules);
}
}

View File

@ -8,6 +8,10 @@ class {{name}} extends Model {
protected $dates = [{{dates}}];
public $rules = [
{{rules}}
];
{{relations}}
}

View File

@ -1,4 +1,4 @@
public function {{name}}()
{
return $this->{{type}}('{{model}}');
}
return $this->{{type}}("{{model}}");
}

1
templates/model/rule.wnt Normal file
View File

@ -0,0 +1 @@
"{{name}}" => "{{rule}}",