4 changed files with 138 additions and 0 deletions
-
2src/CrudMake.php
-
28src/Generators/ModelPolicyTestGenerator.php
-
43src/stubs/test-policy.stub
-
65tests/Generators/ModelPolicyTestGeneratorTest.php
@ -0,0 +1,28 @@ |
|||
<?php |
|||
|
|||
namespace Luthfi\CrudGenerator\Generators; |
|||
|
|||
/** |
|||
* Model Test Generator Class |
|||
*/ |
|||
class ModelPolicyTestGenerator extends BaseGenerator |
|||
{ |
|||
/** |
|||
* {@inheritDoc} |
|||
*/ |
|||
public function generate() |
|||
{ |
|||
$modelPolicyTestPath = $this->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); |
|||
} |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
<?php |
|||
|
|||
namespace Tests\Unit\Policies; |
|||
|
|||
use fullMstr; |
|||
use Illuminate\Foundation\Testing\DatabaseMigrations; |
|||
use Tests\TestCase; |
|||
|
|||
class MasterTest extends TestCase |
|||
{ |
|||
use DatabaseMigrations; |
|||
|
|||
/** @test */ |
|||
public function user_can_create_master() |
|||
{ |
|||
$user = $this->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)); |
|||
} |
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
<?php |
|||
|
|||
namespace Tests\Generators; |
|||
|
|||
use Tests\TestCase; |
|||
|
|||
class ModelPolicyTestGeneratorTest extends TestCase |
|||
{ |
|||
/** @test */ |
|||
public function it_creates_correct_model_policy_test_content() |
|||
{ |
|||
$userModel = config('auth.providers.users.model'); |
|||
|
|||
$this->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 = "<?php
|
|||
|
|||
namespace Tests\Unit\Policies; |
|||
|
|||
use {$this->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)); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue