Browse Source

Add model policy test generator

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
a8035bac55
  1. 2
      src/CrudMake.php
  2. 28
      src/Generators/ModelPolicyTestGenerator.php
  3. 43
      src/stubs/test-policy.stub
  4. 65
      tests/Generators/ModelPolicyTestGeneratorTest.php

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

28
src/Generators/ModelPolicyTestGenerator.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);
}
}

43
src/stubs/test-policy.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));
}
}

65
tests/Generators/ModelPolicyTestGeneratorTest.php

@ -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));
}
}
Loading…
Cancel
Save