From 72713e74f67ce3589a9cf135aae5485ef465d512 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sat, 17 Feb 2018 07:56:44 +0800 Subject: [PATCH] Add creator relation on created model Add creator_id on model table migration file Update controller to add auth()->id() as model creator --- src/Generators/ModelFactoryGenerator.php | 10 ++++++++- src/Generators/ModelGenerator.php | 10 ++++++++- src/Generators/ModelTestGenerator.php | 14 ++++++++++--- src/stubs/controller.full.stub | 5 ++++- src/stubs/controller.simple.stub | 5 ++++- src/stubs/migration-create.stub | 1 + src/stubs/model-factory.stub | 4 ++++ src/stubs/model.stub | 8 +++++++- src/stubs/test-unit.stub | 12 ++++++++++- tests/Generators/FullControllerGeneratorTest.php | 15 +++++++++++--- tests/Generators/MigrationGeneratorTest.php | 1 + tests/Generators/ModelFactoryGeneratorTest.php | 4 ++++ tests/Generators/ModelGeneratorTest.php | 16 +++++++++++++-- tests/Generators/ModelTestGeneratorTest.php | 24 ++++++++++++++++++++-- .../Simple/SimpleControllerGeneratorTest.php | 15 +++++++++++--- 15 files changed, 125 insertions(+), 19 deletions(-) diff --git a/src/Generators/ModelFactoryGenerator.php b/src/Generators/ModelFactoryGenerator.php index 1d5871f..b6191e5 100644 --- a/src/Generators/ModelFactoryGenerator.php +++ b/src/Generators/ModelFactoryGenerator.php @@ -27,6 +27,14 @@ class ModelFactoryGenerator extends BaseGenerator */ protected function getContent(string $stubName) { - return $this->replaceStubString($this->getStubFileContent($stubName)); + $modelFactoryFileContent = $this->getStubFileContent($stubName); + + $userModel = config('auth.providers.users.model'); + + if ('App\User' !== $userModel) { + $modelFactoryFileContent = str_replace('App\User', $userModel, $modelFactoryFileContent); + } + + return $this->replaceStubString($modelFactoryFileContent); } } diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index aa538f1..69b4a52 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -28,6 +28,14 @@ class ModelGenerator extends BaseGenerator */ protected function getContent(string $stubName) { - return $this->replaceStubString($this->getStubFileContent($stubName)); + $modelFileContent = $this->getStubFileContent($stubName); + + $userModel = config('auth.providers.users.model'); + + if ('App\User' !== $userModel) { + $modelFileContent = str_replace('App\User', $userModel, $modelFileContent); + } + + return $this->replaceStubString($modelFileContent); } } diff --git a/src/Generators/ModelTestGenerator.php b/src/Generators/ModelTestGenerator.php index 4391329..09c05fe 100644 --- a/src/Generators/ModelTestGenerator.php +++ b/src/Generators/ModelTestGenerator.php @@ -27,9 +27,17 @@ class ModelTestGenerator extends BaseGenerator */ protected function getContent(string $stubName) { - $stub = $this->getStubFileContent($stubName); + $modelFileContent = $this->getStubFileContent($stubName); + $baseTestClass = config('simple-crud.base_test_class'); - $stub = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $stub); - return $this->replaceStubString($stub); + $modelFileContent = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $modelFileContent); + + $userModel = config('auth.providers.users.model'); + + if ('App\User' !== $userModel) { + $modelFileContent = str_replace('App\User', $userModel, $modelFileContent); + } + + return $this->replaceStubString($modelFileContent); } } diff --git a/src/stubs/controller.full.stub b/src/stubs/controller.full.stub index ff18ae7..27d2436 100644 --- a/src/stubs/controller.full.stub +++ b/src/stubs/controller.full.stub @@ -48,7 +48,10 @@ class MastersController extends Controller 'description' => 'nullable|max:255', ]); - $singleMstr = Master::create($request->only('name', 'description')); + $newMaster = $request->only('name', 'description'); + $newMaster['creator_id'] = auth()->id(); + + $singleMstr = Master::create($newMaster); return redirect()->route('masters.show', $singleMstr); } diff --git a/src/stubs/controller.simple.stub b/src/stubs/controller.simple.stub index d346efb..b593d1a 100644 --- a/src/stubs/controller.simple.stub +++ b/src/stubs/controller.simple.stub @@ -41,7 +41,10 @@ class MastersController extends Controller 'description' => 'nullable|max:255', ]); - Master::create($request->only('name', 'description')); + $newMaster = $request->only('name', 'description'); + $newMaster['creator_id'] = auth()->id(); + + Master::create($newMaster); return redirect()->route('masters.index'); } diff --git a/src/stubs/migration-create.stub b/src/stubs/migration-create.stub index 591eae3..ab637f0 100755 --- a/src/stubs/migration-create.stub +++ b/src/stubs/migration-create.stub @@ -17,6 +17,7 @@ class CreateMastersTable extends Migration $table->increments('id'); $table->string('name', 60); $table->string('description')->nullable(); + $table->unsignedInteger('creator_id'); $table->timestamps(); }); } diff --git a/src/stubs/model-factory.stub b/src/stubs/model-factory.stub index a3c8790..03761ff 100644 --- a/src/stubs/model-factory.stub +++ b/src/stubs/model-factory.stub @@ -1,5 +1,6 @@ define(Master::class, function (Faker $faker) { return [ 'name' => $faker->word, 'description' => $faker->sentence, + 'creator_id' => function () { + return factory(User::class)->create()->id; + }, ]; }); diff --git a/src/stubs/model.stub b/src/stubs/model.stub index b4edef7..f36440c 100644 --- a/src/stubs/model.stub +++ b/src/stubs/model.stub @@ -2,11 +2,12 @@ namespace mstrNmspc; +use App\User; use Illuminate\Database\Eloquent\Model; class Master extends Model { - protected $fillable = ['name', 'description']; + protected $fillable = ['name', 'description', 'creator_id']; public function nameLink() { @@ -17,4 +18,9 @@ class Master extends Model ), ]); } + + public function creator() + { + return $this->belongsTo(User::class); + } } diff --git a/src/stubs/test-unit.stub b/src/stubs/test-unit.stub index 9bf3057..10b632d 100644 --- a/src/stubs/test-unit.stub +++ b/src/stubs/test-unit.stub @@ -2,6 +2,7 @@ namespace Tests\Unit\Models; +use App\User; use fullMstr; use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\BrowserKitTest as TestCase; @@ -11,7 +12,7 @@ class MasterTest extends TestCase use DatabaseMigrations; /** @test */ - public function it_has_name_link_method() + public function a_master_has_name_link_method() { $singleMstr = factory(Master::class)->create(); @@ -24,4 +25,13 @@ class MasterTest extends TestCase ]), $singleMstr->nameLink() ); } + + /** @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/Generators/FullControllerGeneratorTest.php b/tests/Generators/FullControllerGeneratorTest.php index e6a822f..863c0ca 100644 --- a/tests/Generators/FullControllerGeneratorTest.php +++ b/tests/Generators/FullControllerGeneratorTest.php @@ -62,7 +62,10 @@ class {$this->plural_model_name}Controller extends Controller 'description' => 'nullable|max:255', ]); - \${$this->single_model_var_name} = {$this->model_name}::create(\$request->only('name', 'description')); + \$new{$this->model_name} = \$request->only('name', 'description'); + \$new{$this->model_name}['creator_id'] = auth()->id(); + + \${$this->single_model_var_name} = {$this->model_name}::create(\$new{$this->model_name}); return redirect()->route('{$this->table_name}.show', \${$this->single_model_var_name}); } @@ -195,7 +198,10 @@ class CategoriesController extends Controller 'description' => 'nullable|max:255', ]); - \$category = Category::create(\$request->only('name', 'description')); + \$newCategory = \$request->only('name', 'description'); + \$newCategory['creator_id'] = auth()->id(); + + \$category = Category::create(\$newCategory); return redirect()->route('categories.show', \$category); } @@ -329,7 +335,10 @@ class CategoriesController extends Controller 'description' => 'nullable|max:255', ]); - \$category = Category::create(\$request->only('name', 'description')); + \$newCategory = \$request->only('name', 'description'); + \$newCategory['creator_id'] = auth()->id(); + + \$category = Category::create(\$newCategory); return redirect()->route('categories.show', \$category); } diff --git a/tests/Generators/MigrationGeneratorTest.php b/tests/Generators/MigrationGeneratorTest.php index 2946ad1..0fc2838 100644 --- a/tests/Generators/MigrationGeneratorTest.php +++ b/tests/Generators/MigrationGeneratorTest.php @@ -32,6 +32,7 @@ class Create{$this->plural_model_name}Table extends Migration \$table->increments('id'); \$table->string('name', 60); \$table->string('description')->nullable(); + \$table->unsignedInteger('creator_id'); \$table->timestamps(); }); } diff --git a/tests/Generators/ModelFactoryGeneratorTest.php b/tests/Generators/ModelFactoryGeneratorTest.php index 2661d57..d93b37b 100644 --- a/tests/Generators/ModelFactoryGeneratorTest.php +++ b/tests/Generators/ModelFactoryGeneratorTest.php @@ -15,6 +15,7 @@ class ModelFactoryGeneratorTest extends TestCase $this->assertFileExists($modelFactoryPath); $modelFactoryContent = "full_model_name}; use Faker\Generator as Faker; @@ -23,6 +24,9 @@ use Faker\Generator as Faker; return [ 'name' => \$faker->word, 'description' => \$faker->sentence, + 'creator_id' => function () { + return factory(User::class)->create()->id; + }, ]; }); "; diff --git a/tests/Generators/ModelGeneratorTest.php b/tests/Generators/ModelGeneratorTest.php index 74f576e..e2aea6d 100644 --- a/tests/Generators/ModelGeneratorTest.php +++ b/tests/Generators/ModelGeneratorTest.php @@ -17,11 +17,12 @@ class ModelGeneratorTest extends TestCase namespace App; +use App\User; use Illuminate\Database\Eloquent\Model; class {$this->model_name} extends Model { - protected \$fillable = ['name', 'description']; + protected \$fillable = ['name', 'description', 'creator_id']; public function nameLink() { @@ -32,6 +33,11 @@ class {$this->model_name} extends Model ), ]); } + + public function creator() + { + return \$this->belongsTo(User::class); + } } "; $this->assertEquals($modelClassContent, file_get_contents($modelPath)); @@ -48,11 +54,12 @@ class {$this->model_name} extends Model namespace App\Entities\References; +use App\User; use Illuminate\Database\Eloquent\Model; class Category extends Model { - protected \$fillable = ['name', 'description']; + protected \$fillable = ['name', 'description', 'creator_id']; public function nameLink() { @@ -63,6 +70,11 @@ class Category extends Model ), ]); } + + public function creator() + { + return \$this->belongsTo(User::class); + } } "; $this->assertEquals($modelClassContent, file_get_contents($modelPath)); diff --git a/tests/Generators/ModelTestGeneratorTest.php b/tests/Generators/ModelTestGeneratorTest.php index ab6c74d..ca7ae87 100644 --- a/tests/Generators/ModelTestGeneratorTest.php +++ b/tests/Generators/ModelTestGeneratorTest.php @@ -17,6 +17,7 @@ class ModelTestGeneratorTest extends TestCase namespace Tests\Unit\Models; +use App\User; use {$this->full_model_name}; use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\BrowserKitTest as TestCase; @@ -26,7 +27,7 @@ class {$this->model_name}Test extends TestCase use DatabaseMigrations; /** @test */ - public function it_has_name_link_method() + public function a_{$this->lang_name}_has_name_link_method() { \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); @@ -39,6 +40,15 @@ class {$this->model_name}Test extends TestCase ]), \${$this->single_model_var_name}->nameLink() ); } + + /** @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)); @@ -58,6 +68,7 @@ class {$this->model_name}Test extends TestCase namespace Tests\Unit\Models; +use App\User; use {$this->full_model_name}; use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\TestCase as TestCase; @@ -67,7 +78,7 @@ class {$this->model_name}Test extends TestCase use DatabaseMigrations; /** @test */ - public function it_has_name_link_method() + public function a_{$this->lang_name}_has_name_link_method() { \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); @@ -80,6 +91,15 @@ class {$this->model_name}Test extends TestCase ]), \${$this->single_model_var_name}->nameLink() ); } + + /** @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)); diff --git a/tests/Generators/Simple/SimpleControllerGeneratorTest.php b/tests/Generators/Simple/SimpleControllerGeneratorTest.php index ad92ff9..6da64ae 100644 --- a/tests/Generators/Simple/SimpleControllerGeneratorTest.php +++ b/tests/Generators/Simple/SimpleControllerGeneratorTest.php @@ -55,7 +55,10 @@ class {$this->plural_model_name}Controller extends Controller 'description' => 'nullable|max:255', ]); - {$this->model_name}::create(\$request->only('name', 'description')); + \$new{$this->model_name} = \$request->only('name', 'description'); + \$new{$this->model_name}['creator_id'] = auth()->id(); + + {$this->model_name}::create(\$new{$this->model_name}); return redirect()->route('{$this->table_name}.index'); } @@ -159,7 +162,10 @@ class CategoriesController extends Controller 'description' => 'nullable|max:255', ]); - Category::create(\$request->only('name', 'description')); + \$newCategory = \$request->only('name', 'description'); + \$newCategory['creator_id'] = auth()->id(); + + Category::create(\$newCategory); return redirect()->route('categories.index'); } @@ -264,7 +270,10 @@ class CategoriesController extends Controller 'description' => 'nullable|max:255', ]); - Category::create(\$request->only('name', 'description')); + \$newCategory = \$request->only('name', 'description'); + \$newCategory['creator_id'] = auth()->id(); + + Category::create(\$newCategory); return redirect()->route('categories.index'); }