From a8035bac553ddc164d0c0cb6a7221a5db562be2b Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sat, 21 Oct 2017 18:57:32 +0800 Subject: [PATCH] Add model policy test generator --- src/CrudMake.php | 2 + src/Generators/ModelPolicyTestGenerator.php | 28 ++++++++++ src/stubs/test-policy.stub | 43 +++++++++++++++ tests/Generators/ModelPolicyTestGeneratorTest.php | 65 +++++++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 src/Generators/ModelPolicyTestGenerator.php create mode 100644 src/stubs/test-policy.stub create mode 100644 tests/Generators/ModelPolicyTestGeneratorTest.php diff --git a/src/CrudMake.php b/src/CrudMake.php index ad2238c..417684d 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -13,6 +13,7 @@ use Luthfi\CrudGenerator\Generators\MigrationGenerator; use Luthfi\CrudGenerator\Generators\ModelFactoryGenerator; use Luthfi\CrudGenerator\Generators\ModelGenerator; use Luthfi\CrudGenerator\Generators\ModelPolicyGenerator; +use Luthfi\CrudGenerator\Generators\ModelPolicyTestGenerator; use Luthfi\CrudGenerator\Generators\ModelTestGenerator; use Luthfi\CrudGenerator\Generators\WebRouteGenerator; @@ -97,6 +98,7 @@ class CrudMake extends Command app(ModelPolicyGenerator::class, ['command' => $this])->generate(); app(FeatureTestGenerator::class, ['command' => $this])->generate(); app(ModelTestGenerator::class, ['command' => $this])->generate(); + app(ModelPolicyTestGenerator::class, ['command' => $this])->generate(); $this->info('CRUD files generated successfully!'); } else { diff --git a/src/Generators/ModelPolicyTestGenerator.php b/src/Generators/ModelPolicyTestGenerator.php new file mode 100644 index 0000000..dec33e0 --- /dev/null +++ b/src/Generators/ModelPolicyTestGenerator.php @@ -0,0 +1,28 @@ +makeDirectory(base_path('tests/Unit/Policies')); + $this->generateFile("{$modelPolicyTestPath}/{$this->modelNames['model_name']}PolicyTest.php", $this->getContent()); + $this->command->info($this->modelNames['model_name'].'PolicyTest (model policy) generated.'); + } + + /** + * {@inheritDoc} + */ + protected function getContent() + { + $stub = $this->files->get(__DIR__.'/../stubs/test-policy.stub'); + return $this->replaceStubString($stub); + } +} diff --git a/src/stubs/test-policy.stub b/src/stubs/test-policy.stub new file mode 100644 index 0000000..7b6659b --- /dev/null +++ b/src/stubs/test-policy.stub @@ -0,0 +1,43 @@ +loginAsUser(); + $this->assertTrue($user->can('create', new Master)); + } + + /** @test */ + public function user_can_view_master() + { + $user = $this->loginAsUser(); + $singleMstr = factory(Master::class)->create(['name' => 'Master 1 name']); + $this->assertTrue($user->can('view', $singleMstr)); + } + + /** @test */ + public function user_can_update_master() + { + $user = $this->loginAsUser(); + $singleMstr = factory(Master::class)->create(['name' => 'Master 1 name']); + $this->assertTrue($user->can('update', $singleMstr)); + } + + /** @test */ + public function user_can_delete_master() + { + $user = $this->loginAsUser(); + $singleMstr = factory(Master::class)->create(['name' => 'Master 1 name']); + $this->assertTrue($user->can('delete', $singleMstr)); + } +} diff --git a/tests/Generators/ModelPolicyTestGeneratorTest.php b/tests/Generators/ModelPolicyTestGeneratorTest.php new file mode 100644 index 0000000..589fdf3 --- /dev/null +++ b/tests/Generators/ModelPolicyTestGeneratorTest.php @@ -0,0 +1,65 @@ +artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); + + $modelPolicyPath = base_path("tests/Unit/Policies/{$this->model_name}PolicyTest.php"); + $this->assertFileExists($modelPolicyPath); + + $modelPolicyContent = "full_model_name}; +use Illuminate\Foundation\Testing\DatabaseMigrations; +use Tests\TestCase; + +class {$this->model_name}Test extends TestCase +{ + use DatabaseMigrations; + + /** @test */ + public function user_can_create_{$this->lang_name}() + { + \$user = \$this->loginAsUser(); + \$this->assertTrue(\$user->can('create', new {$this->model_name})); + } + + /** @test */ + public function user_can_view_{$this->lang_name}() + { + \$user = \$this->loginAsUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => '{$this->model_name} 1 name']); + \$this->assertTrue(\$user->can('view', \${$this->single_model_var_name})); + } + + /** @test */ + public function user_can_update_{$this->lang_name}() + { + \$user = \$this->loginAsUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => '{$this->model_name} 1 name']); + \$this->assertTrue(\$user->can('update', \${$this->single_model_var_name})); + } + + /** @test */ + public function user_can_delete_{$this->lang_name}() + { + \$user = \$this->loginAsUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => '{$this->model_name} 1 name']); + \$this->assertTrue(\$user->can('delete', \${$this->single_model_var_name})); + } +} +"; + $this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath)); + } +}