From 68bce39cd9d39af6b08e3b1dec78492fd81ec505 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 7 Nov 2017 22:45:00 +0800 Subject: [PATCH] Add nameLink method on generated model --- src/stubs/lang-app.stub | 11 ++++++----- src/stubs/model.stub | 10 ++++++++++ src/stubs/test-unit.stub | 14 +++++++++++--- tests/Generators/LangGeneratorTest.php | 17 +++++++++-------- tests/Generators/ModelGeneratorTest.php | 20 ++++++++++++++++++++ tests/Generators/ModelTestGeneratorTest.php | 28 ++++++++++++++++++++++------ 6 files changed, 78 insertions(+), 22 deletions(-) diff --git a/src/stubs/lang-app.stub b/src/stubs/lang-app.stub index e7f1080..c966d78 100644 --- a/src/stubs/lang-app.stub +++ b/src/stubs/lang-app.stub @@ -2,11 +2,12 @@ return [ // Labels - 'table_no' => '#', - 'total' => 'Total', - 'action' => 'Actions', - 'views' => 'Views', - 'downloads' => 'Downloads', + 'table_no' => '#', + 'total' => 'Total', + 'action' => 'Actions', + 'views' => 'Views', + 'downloads' => 'Downloads', + 'show_detail_title' => 'View :name :type detail', // Actions 'show' => 'View Detail', diff --git a/src/stubs/model.stub b/src/stubs/model.stub index 774bfde..b4edef7 100644 --- a/src/stubs/model.stub +++ b/src/stubs/model.stub @@ -7,4 +7,14 @@ use Illuminate\Database\Eloquent\Model; class Master extends Model { protected $fillable = ['name', 'description']; + + public function nameLink() + { + return link_to_route('masters.show', $this->name, [$this->id], [ + 'title' => trans( + 'app.show_detail_title', + ['name' => $this->name, 'type' => trans('master.master')] + ), + ]); + } } diff --git a/src/stubs/test-unit.stub b/src/stubs/test-unit.stub index de37363..9bf3057 100644 --- a/src/stubs/test-unit.stub +++ b/src/stubs/test-unit.stub @@ -11,9 +11,17 @@ class MasterTest extends TestCase use DatabaseMigrations; /** @test */ - public function it_has_name_attribute() + public function it_has_name_link_method() { - $singleMstr = factory(Master::class)->create(['name' => 'Master 1 name']); - $this->assertEquals('Master 1 name', $singleMstr->name); + $singleMstr = factory(Master::class)->create(); + + $this->assertEquals( + link_to_route('masters.show', $singleMstr->name, [$singleMstr->id], [ + 'title' => trans( + 'app.show_detail_title', + ['name' => $singleMstr->name, 'type' => trans('master.master')] + ), + ]), $singleMstr->nameLink() + ); } } diff --git a/tests/Generators/LangGeneratorTest.php b/tests/Generators/LangGeneratorTest.php index 7d21b83..36daa82 100644 --- a/tests/Generators/LangGeneratorTest.php +++ b/tests/Generators/LangGeneratorTest.php @@ -11,8 +11,8 @@ class LangGeneratorTest extends TestCase { $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $locale = config('app.locale'); - $langPath = resource_path('lang/'.$locale.'/'.$this->lang_name.'.php'); + $locale = config('app.locale'); + $langPath = resource_path('lang/'.$locale.'/'.$this->lang_name.'.php'); $displayModelName = ucwords(str_replace('_', ' ', snake_case($this->model_name))); $this->assertFileExists($langPath); $langFileContent = "artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $locale = config('app.locale'); + $locale = config('app.locale'); $langPath = resource_path('lang/'.$locale.'/app.php'); $this->assertFileExists($langPath); @@ -61,11 +61,12 @@ return [ return [ // Labels - 'table_no' => '#', - 'total' => 'Total', - 'action' => 'Actions', - 'views' => 'Views', - 'downloads' => 'Downloads', + 'table_no' => '#', + 'total' => 'Total', + 'action' => 'Actions', + 'views' => 'Views', + 'downloads' => 'Downloads', + 'show_detail_title' => 'View :name :type detail', // Actions 'show' => 'View Detail', diff --git a/tests/Generators/ModelGeneratorTest.php b/tests/Generators/ModelGeneratorTest.php index bb6a354..a2ea0c4 100644 --- a/tests/Generators/ModelGeneratorTest.php +++ b/tests/Generators/ModelGeneratorTest.php @@ -22,6 +22,16 @@ use Illuminate\Database\Eloquent\Model; class {$this->model_name} extends Model { protected \$fillable = ['name', 'description']; + + public function nameLink() + { + return link_to_route('{$this->table_name}.show', \$this->name, [\$this->id], [ + 'title' => trans( + 'app.show_detail_title', + ['name' => \$this->name, 'type' => trans('{$this->lang_name}.{$this->lang_name}')] + ), + ]); + } } "; $this->assertEquals($modelClassContent, file_get_contents($modelPath)); @@ -43,6 +53,16 @@ use Illuminate\Database\Eloquent\Model; class Category extends Model { protected \$fillable = ['name', 'description']; + + public function nameLink() + { + return link_to_route('{$this->table_name}.show', \$this->name, [\$this->id], [ + 'title' => trans( + 'app.show_detail_title', + ['name' => \$this->name, 'type' => trans('{$this->lang_name}.{$this->lang_name}')] + ), + ]); + } } "; $this->assertEquals($modelClassContent, file_get_contents($modelPath)); diff --git a/tests/Generators/ModelTestGeneratorTest.php b/tests/Generators/ModelTestGeneratorTest.php index 36e2cb5..ab6c74d 100644 --- a/tests/Generators/ModelTestGeneratorTest.php +++ b/tests/Generators/ModelTestGeneratorTest.php @@ -26,10 +26,18 @@ class {$this->model_name}Test extends TestCase use DatabaseMigrations; /** @test */ - public function it_has_name_attribute() + public function it_has_name_link_method() { - \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => '{$this->model_name} 1 name']); - \$this->assertEquals('{$this->model_name} 1 name', \${$this->single_model_var_name}->name); + \${$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}->id], [ + 'title' => trans( + 'app.show_detail_title', + ['name' => \${$this->single_model_var_name}->name, 'type' => trans('{$this->lang_name}.{$this->lang_name}')] + ), + ]), \${$this->single_model_var_name}->nameLink() + ); } } "; @@ -59,10 +67,18 @@ class {$this->model_name}Test extends TestCase use DatabaseMigrations; /** @test */ - public function it_has_name_attribute() + public function it_has_name_link_method() { - \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => '{$this->model_name} 1 name']); - \$this->assertEquals('{$this->model_name} 1 name', \${$this->single_model_var_name}->name); + \${$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}->id], [ + 'title' => trans( + 'app.show_detail_title', + ['name' => \${$this->single_model_var_name}->name, 'type' => trans('{$this->lang_name}.{$this->lang_name}')] + ), + ]), \${$this->single_model_var_name}->nameLink() + ); } } ";