Skip to content

Commit

Permalink
Merge pull request #35 from nafiesl/33_bug_model_overrides
Browse files Browse the repository at this point in the history
Bugfix: Prevent Class Overrides for the Existing Models
  • Loading branch information
nafiesl authored Mar 15, 2021
2 parents ccbd929 + e33f0e7 commit 1d505bf
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/Generators/ModelFactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ class ModelFactoryGenerator extends BaseGenerator
public function generate(string $type = 'full')
{
$modelFactoryPath = $this->makeDirectory(database_path('factories'));
$modelFactoryClassPath = $modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php';

if ($this->files->exists($modelFactoryClassPath)) {
$this->command->warn('Use the existing '.$this->modelNames['model_name'].' model factory.');
return;
}

$this->generateFile(
$modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php',
$modelFactoryClassPath,
$this->getContent('database/factories/model-factory')
);

Expand Down
11 changes: 7 additions & 4 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ public function generate(string $type = 'full')
{
$modelPath = $this->modelNames['model_path'];
$modelDirectory = $this->makeDirectory(app_path($modelPath));
$modelClassPath = $modelDirectory.'/'.$this->modelNames['model_name'].'.php';

$this->generateFile(
$modelDirectory.'/'.$this->modelNames['model_name'].'.php',
$this->getContent('models/model')
);
if ($this->files->exists($modelClassPath)) {
$this->command->warn('Use the existing '.$this->modelNames['model_name'].' model.');
return;
}

$this->generateFile($modelClassPath, $this->getContent('models/model'));

$this->command->info($this->modelNames['model_name'].' model generated.');
}
Expand Down
11 changes: 7 additions & 4 deletions src/Generators/ModelPolicyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ public function generate(string $type = 'full')
$parentDirectory = '/'.$this->command->option('parent');
}
$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)) {
$this->command->warn('Use the existing '.$this->modelNames['model_name'].' model policy.');
return;
}

$this->generateFile($modelPolicyClassPath, $this->getContent('models/model-policy'));

$this->command->info($this->modelNames['model_name'].' model policy generated.');

Expand Down
40 changes: 40 additions & 0 deletions tests/Generators/ModelFactoryGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,44 @@ public function definition()
$this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath));
$this->removeFileOrDir(base_path('stubs'));
}

/** @test */
public function it_doesnt_override_the_existing_model_factory_content()
{
$this->artisan('make:factory', ['name' => $this->model_name.'Factory', '--no-interaction' => true]);
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);

$modelFactoryPath = database_path('factories/'.$this->model_name.'Factory.php');
$this->assertFileExists($modelFactoryPath);
$modelFactoryContent = "<?php
namespace Database\Factories;
use App\Model;
use Illuminate\Database\Eloquent\Factories\Factory;
class {$this->model_name}Factory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected \$model = Model::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
//
];
}
}
";
$this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath));
}
}
26 changes: 26 additions & 0 deletions tests/Generators/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,30 @@ public function creator()
$this->removeFileOrDir(resource_path('views/categories'));
$this->removeFileOrDir(resource_path("lang/en/category.php"));
}

/** @test */
public function it_doesnt_override_the_existing_model()
{
$this->mockConsoleOutput = true;
config(['auth.providers.users.model' => 'App\Models\User']);
$this->artisan('make:model', ['name' => 'Models/'.$this->model_name, '--no-interaction' => true]);
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true])
->expectsQuestion('Model file exists, are you sure to generate CRUD files?', true);

$modelPath = app_path('Models/'.$this->model_name.'.php');
$this->assertFileExists($modelPath);
$modelClassContent = "<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class {$this->model_name} extends Model
{
use HasFactory;
}
";
$this->assertEquals($modelClassContent, file_get_contents($modelPath));
}
}
35 changes: 35 additions & 0 deletions tests/Generators/ModelPolicyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,39 @@ public function boot()
";
$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');
$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));
}
}

0 comments on commit 1d505bf

Please sign in to comment.