Browse Source

Add formfield version stub of model and model test

tags/1.2.2^0 1.2.2
Nafies Luthfi 7 years ago
parent
commit
a760b167b7
  1. 2
      src/CrudApiMake.php
  2. 4
      src/Generators/ModelGenerator.php
  3. 4
      src/Generators/ModelTestGenerator.php
  4. 26
      src/stubs/models/model-formfield.stub
  5. 37
      src/stubs/testcases/unit/model-formfield.stub
  6. 84
      tests/CommandOptions/FullCrudFormfieldOptionsTest.php

2
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.

4
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');

4
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');

26
src/stubs/models/model-formfield.stub

@ -0,0 +1,26 @@
<?php
namespace mstrNmspc;
use App\User;
use Illuminate\Database\Eloquent\Model;
class Master extends Model
{
protected $fillable = ['name', 'description', 'creator_id'];
public function getNameLinkAttribute()
{
return link_to_route('masters.show', $this->name, [$this], [
'title' => __(
'app.show_detail_title',
['name' => $this->name, 'type' => __('master.master')]
),
]);
}
public function creator()
{
return $this->belongsTo(User::class);
}
}

37
src/stubs/testcases/unit/model-formfield.stub

@ -0,0 +1,37 @@
<?php
namespace Tests\Unit\Models;
use App\User;
use fullMstr;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\BrowserKitTest as TestCase;
class MasterTest extends TestCase
{
use DatabaseMigrations;
/** @test */
public function a_master_has_name_link_attribute()
{
$singleMstr = factory(Master::class)->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);
}
}

84
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 = "<?php
namespace App;
use App\User;
use Illuminate\Database\Eloquent\Model;
class {$this->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 = "<?php
namespace Tests\Unit\Models;
use App\User;
use {$this->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));
}
}
Loading…
Cancel
Save