Browse Source

Prevent overriding the existing model policy class

pull/35/head
Nafies Luthfi 5 years ago
parent
commit
bcec1e330c
  1. 10
      src/Generators/ModelPolicyGenerator.php
  2. 36
      tests/Generators/ModelPolicyGeneratorTest.php

10
src/Generators/ModelPolicyGenerator.php

@ -17,11 +17,13 @@ class ModelPolicyGenerator extends BaseGenerator
$parentDirectory = '/'.$this->command->option('parent'); $parentDirectory = '/'.$this->command->option('parent');
} }
$modelPolicyPath = $this->makeDirectory(app_path('Policies'.$parentDirectory)); $modelPolicyPath = $this->makeDirectory(app_path('Policies'.$parentDirectory));
$modelPolicyClassPath = $modelPolicyPath.'/'.$this->modelNames['model_name'].'Policy.php';
$this->generateFile(
$modelPolicyPath.'/'.$this->modelNames['model_name'].'Policy.php',
$this->getContent('models/model-policy')
);
if ($this->files->exists($modelPolicyClassPath)) {
return;
}
$this->generateFile($modelPolicyClassPath, $this->getContent('models/model-policy'));
$this->command->info($this->modelNames['model_name'].' model policy generated.'); $this->command->info($this->modelNames['model_name'].' model policy generated.');

36
tests/Generators/ModelPolicyGeneratorTest.php

@ -229,4 +229,40 @@ class AuthServiceProvider extends ServiceProvider
"; ";
$this->assertEquals($authSPContent, file_get_contents($authSPPath)); $this->assertEquals($authSPContent, file_get_contents($authSPPath));
} }
/** @test */
public function it_doesnt_override_the_existing_model_policy_content()
{
$userModel = config('auth.providers.users.model');
$this->artisan('make:policy', ['name' => $this->model_name.'Policy', '--no-interaction' => true]);
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
$modelPolicyPath = app_path('Policies/'.$this->model_name.'Policy.php');
// dd(file_get_contents($modelPolicyPath));
$this->assertFileExists($modelPolicyPath);
$modelPolicyContent = "<?php
namespace App\Policies;
use Illuminate\Auth\Access\HandlesAuthorization;
use {$userModel};
class {$this->model_name}Policy
{
use HandlesAuthorization;
/**
* Create a new policy instance.
*
* @return void
*/
public function __construct()
{
//
}
}
";
$this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath));
}
} }
Loading…
Cancel
Save