From a760b167b73bf7f0179947c898417af992e98bde Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 9 Sep 2018 08:21:10 +0800 Subject: [PATCH] Add formfield version stub of model and model test --- src/CrudApiMake.php | 2 +- src/Generators/ModelGenerator.php | 4 ++ src/Generators/ModelTestGenerator.php | 4 ++ src/stubs/models/model-formfield.stub | 26 +++++++ src/stubs/testcases/unit/model-formfield.stub | 37 ++++++++++ .../FullCrudFormfieldOptionsTest.php | 84 ++++++++++++++++++++++ 6 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 src/stubs/models/model-formfield.stub create mode 100644 src/stubs/testcases/unit/model-formfield.stub diff --git a/src/CrudApiMake.php b/src/CrudApiMake.php index f87cc8b..ef58924 100644 --- a/src/CrudApiMake.php +++ b/src/CrudApiMake.php @@ -9,7 +9,7 @@ class CrudApiMake extends GeneratorCommand * * @var string */ - protected $signature = 'make:crud-api {name} {--p|parent=} {--t|tests-only}'; + protected $signature = 'make:crud-api {name} {--p|parent=} {--t|tests-only} {--f|formfield}'; /** * The console command description. diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index c44c318..c98b7ec 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -28,6 +28,10 @@ class ModelGenerator extends BaseGenerator */ public function getContent(string $stubName) { + if ($this->command->option('formfield')) { + $stubName .= '-formfield'; + } + $modelFileContent = $this->getStubFileContent($stubName); $userModel = config('auth.providers.users.model'); diff --git a/src/Generators/ModelTestGenerator.php b/src/Generators/ModelTestGenerator.php index 93f9cba..1f1cb35 100644 --- a/src/Generators/ModelTestGenerator.php +++ b/src/Generators/ModelTestGenerator.php @@ -27,6 +27,10 @@ class ModelTestGenerator extends BaseGenerator */ public function getContent(string $stubName) { + if ($this->command->option('formfield')) { + $stubName .= '-formfield'; + } + $modelFileContent = $this->getStubFileContent($stubName); $baseTestClass = config('simple-crud.base_test_class'); diff --git a/src/stubs/models/model-formfield.stub b/src/stubs/models/model-formfield.stub new file mode 100644 index 0000000..e722cc5 --- /dev/null +++ b/src/stubs/models/model-formfield.stub @@ -0,0 +1,26 @@ +name, [$this], [ + 'title' => __( + 'app.show_detail_title', + ['name' => $this->name, 'type' => __('master.master')] + ), + ]); + } + + public function creator() + { + return $this->belongsTo(User::class); + } +} diff --git a/src/stubs/testcases/unit/model-formfield.stub b/src/stubs/testcases/unit/model-formfield.stub new file mode 100644 index 0000000..dae5bef --- /dev/null +++ b/src/stubs/testcases/unit/model-formfield.stub @@ -0,0 +1,37 @@ +create(); + + $this->assertEquals( + link_to_route('masters.show', $singleMstr->name, [$singleMstr], [ + 'title' => __( + 'app.show_detail_title', + ['name' => $singleMstr->name, 'type' => __('master.master')] + ), + ]), $singleMstr->name_link + ); + } + + /** @test */ + public function a_master_has_belongs_to_creator_relation() + { + $singleMstr = factory(Master::class)->make(); + + $this->assertInstanceOf(User::class, $singleMstr->creator); + $this->assertEquals($singleMstr->creator_id, $singleMstr->creator->id); + } +} diff --git a/tests/CommandOptions/FullCrudFormfieldOptionsTest.php b/tests/CommandOptions/FullCrudFormfieldOptionsTest.php index dbf8aec..9c39690 100644 --- a/tests/CommandOptions/FullCrudFormfieldOptionsTest.php +++ b/tests/CommandOptions/FullCrudFormfieldOptionsTest.php @@ -242,4 +242,88 @@ class FullCrudFormfieldOptionsTest extends TestCase $this->assertEquals($editFormViewContent, file_get_contents($editFormViewPath)); } + /** @test */ + public function it_creates_correct_model_class_with_link_to_route_helper() + { + $this->artisan('make:crud', ['name' => $this->model_name, '--formfield' => true]); + + $modelPath = app_path($this->model_name.'.php'); + $this->assertFileExists($modelPath); + $modelClassContent = "model_name} extends Model +{ + protected \$fillable = ['name', 'description', 'creator_id']; + + public function getNameLinkAttribute() + { + return link_to_route('{$this->table_name}.show', \$this->name, [\$this], [ + 'title' => __( + 'app.show_detail_title', + ['name' => \$this->name, 'type' => __('{$this->lang_name}.{$this->lang_name}')] + ), + ]); + } + + public function creator() + { + return \$this->belongsTo(User::class); + } +} +"; + $this->assertEquals($modelClassContent, file_get_contents($modelPath)); + } + + /** @test */ + public function it_creates_correct_unit_test_class_with_link_to_route_helper() + { + $this->artisan('make:crud', ['name' => $this->model_name, '--formfield' => true]); + + $uniTestPath = base_path("tests/Unit/Models/{$this->model_name}Test.php"); + $this->assertFileExists($uniTestPath); + $modelClassContent = "full_model_name}; +use Illuminate\Foundation\Testing\DatabaseMigrations; +use Tests\BrowserKitTest as TestCase; + +class {$this->model_name}Test extends TestCase +{ + use DatabaseMigrations; + + /** @test */ + public function a_{$this->lang_name}_has_name_link_attribute() + { + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); + + \$this->assertEquals( + link_to_route('{$this->table_name}.show', \${$this->single_model_var_name}->name, [\${$this->single_model_var_name}], [ + 'title' => __( + 'app.show_detail_title', + ['name' => \${$this->single_model_var_name}->name, 'type' => __('{$this->lang_name}.{$this->lang_name}')] + ), + ]), \${$this->single_model_var_name}->name_link + ); + } + + /** @test */ + public function a_{$this->lang_name}_has_belongs_to_creator_relation() + { + \${$this->single_model_var_name} = factory({$this->model_name}::class)->make(); + + \$this->assertInstanceOf(User::class, \${$this->single_model_var_name}->creator); + \$this->assertEquals(\${$this->single_model_var_name}->creator_id, \${$this->single_model_var_name}->creator->id); + } +} +"; + $this->assertEquals($modelClassContent, file_get_contents($uniTestPath)); + } }