Browse Source

Add nameLink method on generated model

tags/0.1.4^0 0.1.4
Nafies Luthfi 8 years ago
parent
commit
68bce39cd9
  1. 11
      src/stubs/lang-app.stub
  2. 10
      src/stubs/model.stub
  3. 14
      src/stubs/test-unit.stub
  4. 17
      tests/Generators/LangGeneratorTest.php
  5. 20
      tests/Generators/ModelGeneratorTest.php
  6. 28
      tests/Generators/ModelTestGeneratorTest.php

11
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',

10
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')]
),
]);
}
}

14
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()
);
}
}

17
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 = "<?php
@ -53,7 +53,7 @@ return [
{
$this->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',

20
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));

28
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()
);
}
}
";

Loading…
Cancel
Save