Browse Source

Add model policy generator class

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
46c56cc499
  1. 2
      src/CrudMake.php
  2. 54
      src/Generators/ModelPolicyGenerator.php
  3. 64
      src/stubs/model-policy.stub
  4. 4
      tests/CrudMakeCommandTest.php
  5. 162
      tests/Generators/ModelPolicyGeneratorTest.php
  6. 1
      tests/TestCase.php

2
src/CrudMake.php

@ -12,6 +12,7 @@ use Luthfi\CrudGenerator\Generators\LangFileGenerator;
use Luthfi\CrudGenerator\Generators\MigrationGenerator;
use Luthfi\CrudGenerator\Generators\ModelFactoryGenerator;
use Luthfi\CrudGenerator\Generators\ModelGenerator;
use Luthfi\CrudGenerator\Generators\ModelPolicyGenerator;
use Luthfi\CrudGenerator\Generators\ModelTestGenerator;
use Luthfi\CrudGenerator\Generators\WebRouteGenerator;
@ -93,6 +94,7 @@ class CrudMake extends Command
app(FormViewGenerator::class, ['command' => $this])->generate();
app(LangFileGenerator::class, ['command' => $this])->generate();
app(ModelFactoryGenerator::class, ['command' => $this])->generate();
app(ModelPolicyGenerator::class, ['command' => $this])->generate();
app(FeatureTestGenerator::class, ['command' => $this])->generate();
app(ModelTestGenerator::class, ['command' => $this])->generate();

54
src/Generators/ModelPolicyGenerator.php

@ -0,0 +1,54 @@
<?php
namespace Luthfi\CrudGenerator\Generators;
/**
* Model Policy Generator Class
*/
class ModelPolicyGenerator extends BaseGenerator
{
/**
* {@inheritDoc}
*/
public function generate()
{
$parentDirectory = '';
if (! is_null($this->command->option('parent'))) {
$parentDirectory = '/'.$this->command->option('parent');
}
$modelPolicyPath = $this->makeDirectory(app_path('Policies'.$parentDirectory));
$this->generateFile(
$modelPolicyPath.'/'.$this->modelNames['model_name'].'Policy.php',
$this->getContent()
);
$this->command->info($this->modelNames['model_name'].' model policy generated.');
}
/**
* {@inheritDoc}
*/
protected function getContent()
{
$stub = $this->files->get(__DIR__.'/../stubs/model-policy.stub');
$policyFileContent = $this->replaceStubString($stub);
$userModel = config('auth.providers.users.model');
if ('App\User' !== $userModel) {
$policyFileContent = str_replace('App\User', $userModel, $policyFileContent);
}
if (! is_null($parentName = $this->command->option('parent'))) {
$policyFileContent = str_replace(
'App\Policies;',
"App\Policies\\{$parentName};",
$policyFileContent
);
}
return $policyFileContent;
}
}

64
src/stubs/model-policy.stub

@ -0,0 +1,64 @@
<?php
namespace App\Policies;
use App\User;
use fullMstr;
use Illuminate\Auth\Access\HandlesAuthorization;
class MasterPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view the project.
*
* @param \App\User $user
* @param \fullMstr $singleMstr
* @return mixed
*/
public function view(User $user, Master $singleMstr)
{
// Update $user authorization to view $singleMstr here.
return true;
}
/**
* Determine whether the user can create projects.
*
* @param \App\User $user
* @param \fullMstr $singleMstr
* @return mixed
*/
public function create(User $user, Master $singleMstr)
{
// Update $user authorization to create $singleMstr here.
return true;
}
/**
* Determine whether the user can update the project.
*
* @param \App\User $user
* @param \fullMstr $singleMstr
* @return mixed
*/
public function update(User $user, Master $singleMstr)
{
// Update $user authorization to update $singleMstr here.
return true;
}
/**
* Determine whether the user can delete the project.
*
* @param \App\User $user
* @param \fullMstr $singleMstr
* @return mixed
*/
public function delete(User $user, Master $singleMstr)
{
// Update $user authorization to delete $singleMstr here.
return true;
}
}

4
tests/CrudMakeCommandTest.php

@ -24,6 +24,7 @@ class CrudMakeCommandTest extends TestCase
$this->assertFileExists(resource_path("lang/en/{$this->lang_name}.php"));
$this->assertFileExists(database_path("factories/{$this->model_name}Factory.php"));
$this->assertFileExists(base_path("routes/web.php"));
$this->assertFileExists(app_path("Policies/{$this->model_name}Policy.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$this->model_name}Test.php"));
}
@ -47,6 +48,7 @@ class CrudMakeCommandTest extends TestCase
$this->assertFileNotExists(resource_path("lang/en/{$this->lang_name}.php"));
$this->assertFileNotExists(database_path("factories/{$this->model_name}Factory.php"));
$this->assertFileNotExists(base_path("routes/web.php"));
$this->assertFileNotExists(app_path("Policies/{$this->model_name}Policy.php"));
$this->assertFileNotExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php"));
$this->assertFileNotExists(base_path("tests/Unit/Models/{$this->model_name}Test.php"));
}
@ -75,6 +77,7 @@ class CrudMakeCommandTest extends TestCase
$this->assertFileExists(resource_path("views/{$tableName}/forms.blade.php"));
$this->assertFileExists(resource_path("lang/en/{$langName}.php"));
$this->assertFileExists(database_path("factories/{$modelName}Factory.php"));
$this->assertFileExists(app_path("Policies/{$modelName}Policy.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php"));
}
@ -104,6 +107,7 @@ class CrudMakeCommandTest extends TestCase
$this->assertFileExists(resource_path("views/{$tableName}/forms.blade.php"));
$this->assertFileExists(resource_path("lang/en/{$langName}.php"));
$this->assertFileExists(database_path("factories/{$modelName}Factory.php"));
$this->assertFileExists(app_path("Policies/{$parentName}/{$modelName}Policy.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php"));
}

162
tests/Generators/ModelPolicyGeneratorTest.php

@ -0,0 +1,162 @@
<?php
namespace Tests\Generators;
use Tests\TestCase;
class ModelPolicyGeneratorTest extends TestCase
{
/** @test */
public function it_creates_correct_model_policy_content()
{
$userModel = config('auth.providers.users.model');
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
$modelPolicyPath = app_path('Policies/'.$this->model_name.'Policy.php');
$this->assertFileExists($modelPolicyPath);
$modelPolicyContent = "<?php
namespace App\Policies;
use {$userModel};
use {$this->full_model_name};
use Illuminate\Auth\Access\HandlesAuthorization;
class {$this->model_name}Policy
{
use HandlesAuthorization;
/**
* Determine whether the user can view the project.
*
* @param \\{$userModel} \$user
* @param \\{$this->full_model_name} \${$this->single_model_var_name}
* @return mixed
*/
public function view(User \$user, {$this->model_name} \${$this->single_model_var_name})
{
// Update \$user authorization to view \${$this->single_model_var_name} here.
return true;
}
/**
* Determine whether the user can create projects.
*
* @param \\{$userModel} \$user
* @param \\{$this->full_model_name} \${$this->single_model_var_name}
* @return mixed
*/
public function create(User \$user, {$this->model_name} \${$this->single_model_var_name})
{
// Update \$user authorization to create \${$this->single_model_var_name} here.
return true;
}
/**
* Determine whether the user can update the project.
*
* @param \\{$userModel} \$user
* @param \\{$this->full_model_name} \${$this->single_model_var_name}
* @return mixed
*/
public function update(User \$user, {$this->model_name} \${$this->single_model_var_name})
{
// Update \$user authorization to update \${$this->single_model_var_name} here.
return true;
}
/**
* Determine whether the user can delete the project.
*
* @param \\{$userModel} \$user
* @param \\{$this->full_model_name} \${$this->single_model_var_name}
* @return mixed
*/
public function delete(User \$user, {$this->model_name} \${$this->single_model_var_name})
{
// Update \$user authorization to delete \${$this->single_model_var_name} here.
return true;
}
}
";
$this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath));
}
/** @test */
public function it_creates_correct_model_policy_content_with_parent()
{
$userModel = config('auth.providers.users.model');
$this->artisan('make:crud', ['name' => $this->model_name, '--parent' => 'Projects', '--no-interaction' => true]);
$modelPolicyPath = app_path('Policies/Projects/'.$this->model_name.'Policy.php');
$this->assertFileExists($modelPolicyPath);
$modelPolicyContent = "<?php
namespace App\Policies\Projects;
use {$userModel};
use {$this->full_model_name};
use Illuminate\Auth\Access\HandlesAuthorization;
class {$this->model_name}Policy
{
use HandlesAuthorization;
/**
* Determine whether the user can view the project.
*
* @param \\{$userModel} \$user
* @param \\{$this->full_model_name} \${$this->single_model_var_name}
* @return mixed
*/
public function view(User \$user, {$this->model_name} \${$this->single_model_var_name})
{
// Update \$user authorization to view \${$this->single_model_var_name} here.
return true;
}
/**
* Determine whether the user can create projects.
*
* @param \\{$userModel} \$user
* @param \\{$this->full_model_name} \${$this->single_model_var_name}
* @return mixed
*/
public function create(User \$user, {$this->model_name} \${$this->single_model_var_name})
{
// Update \$user authorization to create \${$this->single_model_var_name} here.
return true;
}
/**
* Determine whether the user can update the project.
*
* @param \\{$userModel} \$user
* @param \\{$this->full_model_name} \${$this->single_model_var_name}
* @return mixed
*/
public function update(User \$user, {$this->model_name} \${$this->single_model_var_name})
{
// Update \$user authorization to update \${$this->single_model_var_name} here.
return true;
}
/**
* Determine whether the user can delete the project.
*
* @param \\{$userModel} \$user
* @param \\{$this->full_model_name} \${$this->single_model_var_name}
* @return mixed
*/
public function delete(User \$user, {$this->model_name} \${$this->single_model_var_name})
{
// Update \$user authorization to delete \${$this->single_model_var_name} here.
return true;
}
}
";
$this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath));
}
}

1
tests/TestCase.php

@ -45,6 +45,7 @@ abstract class TestCase extends BaseTestCase
$this->removeFileOrDir(resource_path("lang/en/app.php"));
$this->removeFileOrDir(resource_path("lang/en/{$this->lang_name}.php"));
$this->removeFileOrDir(base_path('routes'));
$this->removeFileOrDir(app_path('Policies'));
$this->removeFileOrDir(base_path('tests/BrowserKitTest.php'));
$this->removeFileOrDir(base_path('tests/Feature'));
$this->removeFileOrDir(base_path('tests/Unit'));

Loading…
Cancel
Save