Browse Source

Add creator relation on created model

Add creator_id on model table migration file
Update controller to add auth()->id() as model creator
tags/1.0.1^0 1.0.1
Nafies Luthfi 8 years ago
parent
commit
72713e74f6
  1. 10
      src/Generators/ModelFactoryGenerator.php
  2. 10
      src/Generators/ModelGenerator.php
  3. 14
      src/Generators/ModelTestGenerator.php
  4. 5
      src/stubs/controller.full.stub
  5. 5
      src/stubs/controller.simple.stub
  6. 1
      src/stubs/migration-create.stub
  7. 4
      src/stubs/model-factory.stub
  8. 8
      src/stubs/model.stub
  9. 12
      src/stubs/test-unit.stub
  10. 15
      tests/Generators/FullControllerGeneratorTest.php
  11. 1
      tests/Generators/MigrationGeneratorTest.php
  12. 4
      tests/Generators/ModelFactoryGeneratorTest.php
  13. 16
      tests/Generators/ModelGeneratorTest.php
  14. 24
      tests/Generators/ModelTestGeneratorTest.php
  15. 15
      tests/Generators/Simple/SimpleControllerGeneratorTest.php

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

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

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

5
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);
}

5
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');
}

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

4
src/stubs/model-factory.stub

@ -1,5 +1,6 @@
<?php
use App\User;
use fullMstr;
use Faker\Generator as Faker;
@ -8,5 +9,8 @@ $factory->define(Master::class, function (Faker $faker) {
return [
'name' => $faker->word,
'description' => $faker->sentence,
'creator_id' => function () {
return factory(User::class)->create()->id;
},
];
});

8
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);
}
}

12
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);
}
}

15
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);
}

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

4
tests/Generators/ModelFactoryGeneratorTest.php

@ -15,6 +15,7 @@ class ModelFactoryGeneratorTest extends TestCase
$this->assertFileExists($modelFactoryPath);
$modelFactoryContent = "<?php
use App\User;
use {$this->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;
},
];
});
";

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

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

15
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');
}

Loading…
Cancel
Save