Browse Source

Use crud-api command can use existing models

tags/1.1.1^0 1.1.1
Nafies Luthfi 8 years ago
parent
commit
d4672b7a47
  1. 20
      src/CrudApiMake.php
  2. 16
      tests/CrudApiMakeCommandTest.php

20
src/CrudApiMake.php

@ -28,8 +28,7 @@ class CrudApiMake extends GeneratorCommand
$this->getModelName(); $this->getModelName();
if ($this->modelExists()) { if ($this->modelExists()) {
$this->error("{$this->modelNames['model_name']} model already exists.");
return;
$this->warn("We will use existing {$this->modelNames['model_name']} model.\n");
} }
// Warn if it has no default layout view based on // Warn if it has no default layout view based on
@ -41,17 +40,19 @@ class CrudApiMake extends GeneratorCommand
if ($this->option('tests-only')) { if ($this->option('tests-only')) {
$this->generateTestFiles(); $this->generateTestFiles();
$this->info('Test files generated successfully!');
$this->info('API Test files generated successfully!');
return; return;
} }
$this->generateRoutes(); $this->generateRoutes();
$this->generateModel();
$this->generateController(); $this->generateController();
$this->generateResources();
$this->generateTestFiles(); $this->generateTestFiles();
if ($this->modelExists() == false) {
$this->generateModel();
$this->generateResources();
}
$this->info('CRUD files generated successfully!');
$this->info('API CRUD files generated successfully!');
} }
/** /**
@ -61,9 +62,12 @@ class CrudApiMake extends GeneratorCommand
*/ */
public function generateTestFiles() public function generateTestFiles()
{ {
app('Luthfi\CrudGenerator\Generators\ModelTestGenerator', ['command' => $this])->generate();
app('Luthfi\CrudGenerator\Generators\FeatureTestGenerator', ['command' => $this])->generate('api'); app('Luthfi\CrudGenerator\Generators\FeatureTestGenerator', ['command' => $this])->generate('api');
app('Luthfi\CrudGenerator\Generators\ModelPolicyTestGenerator', ['command' => $this])->generate();
if ($this->modelExists() == false) {
app('Luthfi\CrudGenerator\Generators\ModelTestGenerator', ['command' => $this])->generate();
app('Luthfi\CrudGenerator\Generators\ModelPolicyTestGenerator', ['command' => $this])->generate();
}
} }
/** /**

16
tests/CrudApiMakeCommandTest.php

@ -33,15 +33,15 @@ class CrudApiMakeCommandTest extends TestCase
} }
/** @test */ /** @test */
public function it_cannot_generate_crud_files_if_model_exists()
public function it_generate_api_crud_files_even_if_model_exists()
{ {
$this->artisan('make:model', ['name' => $this->model_name, '--no-interaction' => true]); $this->artisan('make:model', ['name' => $this->model_name, '--no-interaction' => true]);
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
$this->artisan('make:crud-api', ['name' => $this->model_name, '--no-interaction' => true]);
$this->assertContains("{$this->model_name} model already exists.", app(Kernel::class)->output());
$this->assertContains("We will use existing {$this->model_name} model.", app(Kernel::class)->output());
$this->assertFileExists(app_path($this->model_name.'.php')); $this->assertFileExists(app_path($this->model_name.'.php'));
$this->assertFileNotExists(app_path("Http/Controllers/Api/{$this->plural_model_name}Controller.php"));
$this->assertFileExists(app_path("Http/Controllers/Api/{$this->plural_model_name}Controller.php"));
$migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$this->table_name.'_table.php'); $migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$this->table_name.'_table.php');
$this->assertFileNotExists($migrationFilePath); $this->assertFileNotExists($migrationFilePath);
@ -53,7 +53,7 @@ class CrudApiMakeCommandTest extends TestCase
$this->assertFileNotExists(app_path("Policies/{$this->model_name}Policy.php")); $this->assertFileNotExists(app_path("Policies/{$this->model_name}Policy.php"));
$this->assertFileNotExists(database_path("factories/{$this->model_name}Factory.php")); $this->assertFileNotExists(database_path("factories/{$this->model_name}Factory.php"));
$this->assertFileNotExists(base_path("tests/Unit/Models/{$this->model_name}Test.php")); $this->assertFileNotExists(base_path("tests/Unit/Models/{$this->model_name}Test.php"));
$this->assertFileNotExists(base_path("tests/Feature/Api/Manage{$this->plural_model_name}Test.php"));
$this->assertFileExists(base_path("tests/Feature/Api/Manage{$this->plural_model_name}Test.php"));
} }
/** @test */ /** @test */
@ -62,10 +62,10 @@ class CrudApiMakeCommandTest extends TestCase
$this->artisan('make:model', ['name' => 'Entities/Projects/Problem', '--no-interaction' => true]); $this->artisan('make:model', ['name' => 'Entities/Projects/Problem', '--no-interaction' => true]);
$this->artisan('make:crud-api', ['name' => 'Entities/Projects/Problem', '--no-interaction' => true]); $this->artisan('make:crud-api', ['name' => 'Entities/Projects/Problem', '--no-interaction' => true]);
$this->assertContains("Problem model already exists.", app(Kernel::class)->output());
$this->assertContains("We will use existing Problem model.", app(Kernel::class)->output());
$this->assertFileExists(app_path('Entities/Projects/Problem.php')); $this->assertFileExists(app_path('Entities/Projects/Problem.php'));
$this->assertFileNotExists(app_path("Http/Controllers/Api/ProblemsController.php"));
$this->assertFileExists(app_path("Http/Controllers/Api/ProblemsController.php"));
$migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_problems_table.php'); $migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_problems_table.php');
$this->assertFileNotExists($migrationFilePath); $this->assertFileNotExists($migrationFilePath);
@ -79,7 +79,7 @@ class CrudApiMakeCommandTest extends TestCase
$this->assertFileNotExists(app_path("Policies/ProblemPolicy.php")); $this->assertFileNotExists(app_path("Policies/ProblemPolicy.php"));
$this->assertFileNotExists(database_path("factories/ProblemFactory.php")); $this->assertFileNotExists(database_path("factories/ProblemFactory.php"));
$this->assertFileNotExists(base_path("tests/Unit/Models/ProblemTest.php")); $this->assertFileNotExists(base_path("tests/Unit/Models/ProblemTest.php"));
$this->assertFileNotExists(base_path("tests/Feature/Api/ManageProblemsTest.php"));
$this->assertFileExists(base_path("tests/Feature/Api/ManageProblemsTest.php"));
$this->removeFileOrDir(app_path('Entities/Projects')); $this->removeFileOrDir(app_path('Entities/Projects'));
$this->removeFileOrDir(resource_path('views/problems')); $this->removeFileOrDir(resource_path('views/problems'));

Loading…
Cancel
Save