From 5f52a3b408c25bbbdb85138e116a15ceefa74a0c Mon Sep 17 00:00:00 2001 From: Rafael Delgado Date: Sat, 2 May 2020 12:04:57 -0500 Subject: [PATCH 1/2] Allow configuration to use --- config/blueprint.php | 11 ++++++ src/Generators/ModelGenerator.php | 12 ++++-- stubs/model/guarded.stub | 6 +++ .../Feature/Generator/ModelGeneratorTest.php | 37 +++++++++++++++++++ tests/fixtures/definitions/model-guarded.bp | 3 ++ tests/fixtures/models/model-guarded.php | 24 ++++++++++++ 6 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 stubs/model/guarded.stub create mode 100644 tests/fixtures/definitions/model-guarded.bp create mode 100644 tests/fixtures/models/model-guarded.php diff --git a/config/blueprint.php b/config/blueprint.php index 3fbe5ad9..47ed467a 100644 --- a/config/blueprint.php +++ b/config/blueprint.php @@ -69,4 +69,15 @@ 'use_constraints' => false, 'fake_nullables' => true, + + /* + |-------------------------------------------------------------------------- + | Models + |-------------------------------------------------------------------------- + | + | Here you may choose to use $guarded instead $fillable. + | + */ + + 'use_guarded' => false, ]; diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 75e274e8..6c93c940 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -92,11 +92,15 @@ private function buildProperties(Model $model) { $properties = ''; - $columns = $this->fillableColumns($model->columns()); - if (!empty($columns)) { - $properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns, false), $this->files->stub('model/fillable.stub')); + if (config('blueprint.use_guarded')) { + $properties .= $this->files->stub('model/guarded.stub'); } else { - $properties .= $this->files->stub('model/fillable.stub'); + $columns = $this->fillableColumns($model->columns()); + if (!empty($columns)) { + $properties .= PHP_EOL . str_replace('[]', $this->pretty_print_array($columns, false), $this->files->stub('model/fillable.stub')); + } else { + $properties .= $this->files->stub('model/fillable.stub'); + } } $columns = $this->hiddenColumns($model->columns()); diff --git a/stubs/model/guarded.stub b/stubs/model/guarded.stub new file mode 100644 index 00000000..3822ab1f --- /dev/null +++ b/stubs/model/guarded.stub @@ -0,0 +1,6 @@ + /** + * The attributes that aren't mass assignable. + * + * @var array + */ + protected $guarded = []; diff --git a/tests/Feature/Generator/ModelGeneratorTest.php b/tests/Feature/Generator/ModelGeneratorTest.php index 97a60c47..0a3af8f0 100644 --- a/tests/Feature/Generator/ModelGeneratorTest.php +++ b/tests/Feature/Generator/ModelGeneratorTest.php @@ -197,6 +197,7 @@ public function output_generates_phpdoc_for_model($definition, $path, $model) $this->files->expects('exists') ->with(dirname($path)) ->andReturnTrue(); + $this->files->expects('put') ->with($path, $this->fixture($model)); @@ -206,6 +207,42 @@ public function output_generates_phpdoc_for_model($definition, $path, $model) $this->assertEquals(['created' => [$path]], $this->subject->output($tree)); } + /** + * @test + */ + public function use_guarded_option_generates_models_with_an_empty_array() + { + $this->app['config']->set('blueprint.use_guarded', true); + + $this->files->expects('stub') + ->with('model/class.stub') + ->andReturn(file_get_contents('stubs/model/class.stub')); + + $this->files->expects('stub') + ->with('model/guarded.stub') + ->andReturn(file_get_contents('stubs/model/guarded.stub')); + + $this->files->expects('stub') + ->with('model/casts.stub') + ->andReturn(file_get_contents('stubs/model/casts.stub')); + + $this->files->shouldReceive('stub') + ->with('model/method.stub') + ->andReturn(file_get_contents('stubs/model/method.stub')); + + $this->files->expects('exists') + ->with(dirname('app/Comment.php')) + ->andReturnTrue(); + + $this->files->expects('put') + ->with('app/Comment.php', $this->fixture('models/model-guarded.php')); + + $tokens = $this->blueprint->parse($this->fixture('definitions/model-guarded.bp')); + $tree = $this->blueprint->analyze($tokens); + + $this->assertEquals(['created' => ['app/Comment.php']], $this->subject->output($tree)); + } + public function modelTreeDataProvider() { return [ diff --git a/tests/fixtures/definitions/model-guarded.bp b/tests/fixtures/definitions/model-guarded.bp new file mode 100644 index 00000000..d89c2865 --- /dev/null +++ b/tests/fixtures/definitions/model-guarded.bp @@ -0,0 +1,3 @@ +models: + Comment: + content: text \ No newline at end of file diff --git a/tests/fixtures/models/model-guarded.php b/tests/fixtures/models/model-guarded.php new file mode 100644 index 00000000..58a9d7b0 --- /dev/null +++ b/tests/fixtures/models/model-guarded.php @@ -0,0 +1,24 @@ + 'integer', + ]; +} From 8537782d18b1017c3bd220930a7842907f79fada Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Sat, 2 May 2020 13:54:53 -0400 Subject: [PATCH 2/2] Use symmetrical test case name --- tests/Feature/Generator/ModelGeneratorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/Generator/ModelGeneratorTest.php b/tests/Feature/Generator/ModelGeneratorTest.php index 0a3af8f0..a79a8a50 100644 --- a/tests/Feature/Generator/ModelGeneratorTest.php +++ b/tests/Feature/Generator/ModelGeneratorTest.php @@ -210,7 +210,7 @@ public function output_generates_phpdoc_for_model($definition, $path, $model) /** * @test */ - public function use_guarded_option_generates_models_with_an_empty_array() + public function output_generates_models_with_guarded_property_when_config_option_is_set() { $this->app['config']->set('blueprint.use_guarded', true);