Browse Source

Prevent overriding the existing model class

pull/35/head
Nafies Luthfi 5 years ago
parent
commit
403e19a47f
  1. 10
      src/Generators/ModelGenerator.php
  2. 26
      tests/Generators/ModelGeneratorTest.php

10
src/Generators/ModelGenerator.php

@ -14,11 +14,13 @@ class ModelGenerator extends BaseGenerator
{
$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)) {
return;
}
$this->generateFile($modelClassPath, $this->getContent('models/model'));
$this->command->info($this->modelNames['model_name'].' model generated.');
}

26
tests/Generators/ModelGeneratorTest.php

@ -97,4 +97,30 @@ class Category extends Model
$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));
}
}
Loading…
Cancel
Save