Browse Source

Merge pull request #36 from nafiesl/33_bug_model_overrides

[1.x] Bugfix: Prevent Class Overrides for the Existing Models
1.x 1.6.2
Nafies Luthfi 5 years ago
committed by GitHub
parent
commit
7b4e0fb05f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/Generators/ModelFactoryGenerator.php
  2. 11
      src/Generators/ModelGenerator.php
  3. 11
      src/Generators/ModelPolicyGenerator.php
  4. 24
      tests/Generators/ModelFactoryGeneratorTest.php
  5. 24
      tests/Generators/ModelGeneratorTest.php
  6. 35
      tests/Generators/ModelPolicyGeneratorTest.php

8
src/Generators/ModelFactoryGenerator.php

@ -13,9 +13,15 @@ class ModelFactoryGenerator extends BaseGenerator
public function generate(string $type = 'full') public function generate(string $type = 'full')
{ {
$modelFactoryPath = $this->makeDirectory(database_path('factories')); $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( $this->generateFile(
$modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php',
$modelFactoryClassPath,
$this->getContent('database/factories/model-factory') $this->getContent('database/factories/model-factory')
); );

11
src/Generators/ModelGenerator.php

@ -14,11 +14,14 @@ class ModelGenerator extends BaseGenerator
{ {
$modelPath = $this->modelNames['model_path']; $modelPath = $this->modelNames['model_path'];
$modelDirectory = $this->makeDirectory(app_path($modelPath)); $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.'); $this->command->info($this->modelNames['model_name'].' model generated.');
} }

11
src/Generators/ModelPolicyGenerator.php

@ -17,11 +17,14 @@ 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)) {
$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.'); $this->command->info($this->modelNames['model_name'].' model policy generated.');

24
tests/Generators/ModelFactoryGeneratorTest.php

@ -74,4 +74,28 @@ class {$this->model_name}Factory extends Factory
$this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath)); $this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath));
$this->removeFileOrDir(base_path('stubs')); $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
/* @var \$factory \Illuminate\Database\Eloquent\Factory */
use App\Model;
use Faker\Generator as Faker;
\$factory->define(Model::class, function (Faker \$faker) {
return [
//
];
});
";
$this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath));
}
} }

24
tests/Generators/ModelGeneratorTest.php

@ -89,4 +89,28 @@ class Category extends Model
$this->removeFileOrDir(resource_path('views/categories')); $this->removeFileOrDir(resource_path('views/categories'));
$this->removeFileOrDir(resource_path("lang/en/category.php")); $this->removeFileOrDir(resource_path("lang/en/category.php"));
} }
/** @test */
public function it_doesnt_override_the_existing_model()
{
$this->mockConsoleOutput = true;
$this->artisan('make:model', ['name' => $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($this->model_name.'.php');
$this->assertFileExists($modelPath);
$modelClassContent = "<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class {$this->model_name} extends Model
{
//
}
";
$this->assertEquals($modelClassContent, file_get_contents($modelPath));
}
} }

35
tests/Generators/ModelPolicyGeneratorTest.php

@ -229,4 +229,39 @@ 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');
$this->assertFileExists($modelPolicyPath);
$modelPolicyContent = "<?php
namespace App\Policies;
use {$userModel};
use Illuminate\Auth\Access\HandlesAuthorization;
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