Browse Source

Prevent overriding the existing model factory class

pull/35/head
Nafies Luthfi 5 years ago
parent
commit
56381a7352
  1. 7
      src/Generators/ModelFactoryGenerator.php
  2. 41
      tests/Generators/ModelFactoryGeneratorTest.php

7
src/Generators/ModelFactoryGenerator.php

@ -13,9 +13,14 @@ 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)) {
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')
); );

41
tests/Generators/ModelFactoryGeneratorTest.php

@ -81,4 +81,45 @@ 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:model', ['name' => 'Models/'.$this->model_name, '--no-interaction' => true]);
$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));
}
} }
Loading…
Cancel
Save