Browse Source

Prevent CRUD files generation if model is already exists

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
9141ac01fc
  1. 43
      src/CrudMake.php
  2. 25
      tests/CrudMakeCommandTest.php
  3. 34
      tests/TestCase.php

43
src/CrudMake.php

@ -62,17 +62,22 @@ class CrudMake extends Command
public function handle() public function handle()
{ {
$this->getModelName(); $this->getModelName();
$this->generateResourceRoute();
$this->generateModel();
$this->generateMigration();
$this->generateController();
$this->generateViews();
$this->generateLangFile();
$this->generateModelFactory();
$this->generateTests();
if ( ! $this->modelExists()) {
$this->generateResourceRoute();
$this->info('CRUD files generated successfully!');
$this->generateModel();
$this->generateMigration();
$this->generateController();
$this->generateViews();
$this->generateLangFile();
$this->generateModelFactory();
$this->generateTests();
$this->info('CRUD files generated successfully!');
}
$this->error("{$this->modelNames['model_name']} model already exists.");
} }
/** /**
@ -93,6 +98,16 @@ class CrudMake extends Command
} }
/** /**
* Check for Model file existance
*
* @return void
*/
public function modelExists()
{
return $this->files->exists(app_path($this->modelNames['model_name'].'.php'));
}
/**
* Generate the model file * Generate the model file
* *
* @return void * @return void
@ -128,7 +143,10 @@ class CrudMake extends Command
{ {
$prefix = date('Y_m_d_His'); $prefix = date('Y_m_d_His');
$tableName = $this->modelNames['lowercase_plural_model_name']; $tableName = $this->modelNames['lowercase_plural_model_name'];
$migrationFilePath = database_path("migrations/{$prefix}_create_{$tableName}_table.php");
$migrationPath = $this->makeDirectory(database_path('migrations'));
$migrationFilePath = $migrationPath.'/'.$prefix."_create_{$tableName}_table.php";
$this->generateFile($migrationFilePath, $this->getMigrationContent()); $this->generateFile($migrationFilePath, $this->getMigrationContent());
$this->info($this->modelNames['model_name'].' table migration generated.'); $this->info($this->modelNames['model_name'].' table migration generated.');
@ -172,7 +190,10 @@ class CrudMake extends Command
{ {
$modelFactoryPath = $this->makeDirectory(database_path('factories')); $modelFactoryPath = $this->makeDirectory(database_path('factories'));
$this->generateFile($modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php', $this->getModelFactoryContent());
$this->generateFile(
$modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php',
$this->getModelFactoryContent()
);
$this->info($this->modelNames['lowercase_single_model_name'].' model factory generated.'); $this->info($this->modelNames['lowercase_single_model_name'].' model factory generated.');
} }

25
tests/CrudMakeCommandTest.php

@ -2,6 +2,8 @@
namespace Tests; namespace Tests;
use Illuminate\Contracts\Console\Kernel;
class CrudMakeCommandTest extends TestCase class CrudMakeCommandTest extends TestCase
{ {
/** @test */ /** @test */
@ -23,4 +25,27 @@ class CrudMakeCommandTest extends TestCase
$this->assertFileExists(base_path("tests/Feature/Manage{$this->pluralModelName}Test.php")); $this->assertFileExists(base_path("tests/Feature/Manage{$this->pluralModelName}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$this->modelName}Test.php")); $this->assertFileExists(base_path("tests/Unit/Models/{$this->modelName}Test.php"));
} }
/** @test */
public function it_cannot_generate_crud_files_if_model_exists()
{
$this->artisan('make:model', ['name' => $this->modelName, '--no-interaction' => true]);
$this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]);
$this->assertRegExp("/{$this->modelName} model already exists./", app(Kernel::class)->output());
$this->assertFileExists(app_path($this->modelName.'.php'));
$this->assertFileNotExists(app_path("Http/Controllers/{$this->pluralModelName}Controller.php"));
$migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$this->tableName.'_table.php');
$this->assertFileNotExists($migrationFilePath);
$this->assertFileNotExists(resource_path("views/{$this->tableName}/index.blade.php"));
$this->assertFileNotExists(resource_path("views/{$this->tableName}/forms.blade.php"));
$this->assertFileNotExists(resource_path("lang/en/{$this->singleModelName}.php"));
$this->assertFileNotExists(database_path("factories/{$this->modelName}Factory.php"));
$this->assertFileNotExists(base_path("routes/web.php"));
$this->assertFileNotExists(base_path("tests/Feature/Manage{$this->pluralModelName}Test.php"));
$this->assertFileNotExists(base_path("tests/Unit/Models/{$this->modelName}Test.php"));
}
} }

34
tests/TestCase.php

@ -23,18 +23,36 @@ abstract class TestCase extends BaseTestCase
public function tearDown() public function tearDown()
{ {
exec('rm '.app_path($this->modelName.'.php'));
exec('rm -r '.app_path('Http'));
exec('rm '.database_path('migrations/*'));
exec('rm -r '.resource_path('views/'.$this->tableName));
exec('rm -r '.base_path('routes'));
exec('rm '.base_path('tests/BrowserKitTest.php'));
exec('rm -r '.base_path('tests/Feature'));
exec('rm -r '.base_path('tests/Unit'));
$this->cleanUpGeneratedFiles();
parent::tearDown(); parent::tearDown();
} }
protected function cleanUpGeneratedFiles()
{
$this->removeFileOrDir(app_path($this->modelName.'.php'));
$this->removeFileOrDir(app_path('Http'));
$this->removeFileOrDir(database_path('migrations'));
$this->removeFileOrDir(database_path('factories'));
$this->removeFileOrDir(resource_path('views/'.$this->tableName));
$this->removeFileOrDir(resource_path("lang/en/{$this->singleModelName}.php"));
$this->removeFileOrDir(base_path('routes'));
$this->removeFileOrDir(base_path('tests/BrowserKitTest.php'));
$this->removeFileOrDir(base_path('tests/Feature'));
$this->removeFileOrDir(base_path('tests/Unit'));
}
protected function removeFileOrDir($path)
{
if (file_exists($path) && is_file($path)) {
exec('rm '.$path);
}
if (file_exists($path) && is_dir($path)) {
exec('rm -r '.$path);
}
}
protected function getPackageProviders($app) protected function getPackageProviders($app)
{ {
return ['Luthfi\CrudGenerator\ServiceProvider']; return ['Luthfi\CrudGenerator\ServiceProvider'];

Loading…
Cancel
Save