Browse Source

Add --tests-only command option and use app namespace on generators

Use --test-only command option to generate testing files only
Use app namespace, eg: Acme, instead of default App namespace
tags/0.2.0
Nafies Luthfi 8 years ago
parent
commit
bbfeeb6cfc
  1. 34
      src/CrudMake.php
  2. 12
      src/Generators/ControllerGenerator.php
  3. 5
      src/Generators/FeatureTestGenerator.php
  4. 16
      src/Generators/ModelPolicyGenerator.php
  5. 44
      tests/CommandOptions/TestsOnlyOptionsTest.php

34
src/CrudMake.php

@ -3,6 +3,7 @@
namespace Luthfi\CrudGenerator;
use Illuminate\Console\Command;
use Illuminate\Console\DetectsApplicationNamespace;
use Illuminate\Filesystem\Filesystem;
use Luthfi\CrudGenerator\Generators\ControllerGenerator;
use Luthfi\CrudGenerator\Generators\FeatureTestGenerator;
@ -19,6 +20,8 @@ use Luthfi\CrudGenerator\Generators\WebRouteGenerator;
class CrudMake extends Command
{
use DetectsApplicationNamespace;
/**
* The injected Filesystem class
*
@ -68,14 +71,14 @@ class CrudMake extends Command
*
* @var string
*/
protected $signature = 'make:crud {name} {--parent=}';
protected $signature = 'make:crud {name} {--parent=} {--tests-only}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create simple Laravel complate CRUD files of given model name.';
protected $description = 'Create simple Laravel complete CRUD files of given model name.';
/**
* Execute the console command.
@ -97,6 +100,13 @@ class CrudMake extends Command
$this->warn(config('simple-crud.default_layout_view').' view does not exists.');
}
if ($this->option('tests-only')) {
$this->generateTestFiles();
$this->info('Test files generated successfully!');
return;
}
app(WebRouteGenerator::class, ['command' => $this])->generate();
app(ModelGenerator::class, ['command' => $this])->generate();
app(MigrationGenerator::class, ['command' => $this])->generate();
@ -106,9 +116,8 @@ class CrudMake extends Command
app(LangFileGenerator::class, ['command' => $this])->generate();
app(ModelFactoryGenerator::class, ['command' => $this])->generate();
app(ModelPolicyGenerator::class, ['command' => $this])->generate();
app(FeatureTestGenerator::class, ['command' => $this])->generate();
app(ModelTestGenerator::class, ['command' => $this])->generate();
app(ModelPolicyTestGenerator::class, ['command' => $this])->generate();
$this->generateTestFiles();
$this->info('CRUD files generated successfully!');
}
@ -161,7 +170,8 @@ class CrudMake extends Command
*/
protected function getModelNamespace($modelPath)
{
$modelNamespace = str_replace('/', '\\', 'App/'.ucfirst($modelPath));
$appNamespace = trim($this->getAppNamespace(), '\\');
$modelNamespace = str_replace('/', '\\', $appNamespace.'/'.ucfirst($modelPath));
return $modelNamespace == 'App\\' ? 'App' : $modelNamespace;
}
@ -188,4 +198,16 @@ class CrudMake extends Command
resource_path('views/'.str_replace('.', '/', config('simple-crud.default_layout_view')).'.blade.php')
);
}
/**
* Generate test files
*
* @return void
*/
public function generateTestFiles()
{
app(ModelTestGenerator::class, ['command' => $this])->generate();
app(FeatureTestGenerator::class, ['command' => $this])->generate();
app(ModelPolicyTestGenerator::class, ['command' => $this])->generate();
}
}

12
src/Generators/ControllerGenerator.php

@ -33,14 +33,20 @@ class ControllerGenerator extends BaseGenerator
$controllerFileContent = $this->replaceStubString($stub);
$appNamespace = $this->getAppNamespace();
$controllerFileContent = str_replace(
"App\Http\Controllers;",
"{$appNamespace}Http\Controllers;",
$controllerFileContent
);
if (!is_null($parentName = $this->command->option('parent'))) {
$searches = [
'App\Http\Controllers;',
"{$appNamespace}Http\Controllers;",
"use {$this->modelNames['full_model_name']};\n",
];
$appNamespace = $this->getAppNamespace();
$replacements = [
"{$appNamespace}Http\Controllers\\{$parentName};",
"use {$this->modelNames['full_model_name']};\nuse {$appNamespace}Http\Controllers\Controller;\n",

5
src/Generators/FeatureTestGenerator.php

@ -42,13 +42,14 @@ class FeatureTestGenerator extends BaseGenerator
$this->files->makeDirectory($testsPath, 0777, true, true);
}
$userModel = config('auth.providers.users.model');
$baseTestPath = base_path(config('simple-crud.base_test_path'));
$baseTestClass = class_basename(config('simple-crud.base_test_class'));
if (!$this->files->exists($baseTestPath)) {
$browserKitTestClassContent = str_replace(
'class BrowserKitTest extends',
"class {$baseTestClass} extends",
['class BrowserKitTest extends', 'App\User'],
["class {$baseTestClass} extends", $userModel],
$this->getBrowserKitBaseTestContent()
);

16
src/Generators/ModelPolicyGenerator.php

@ -43,10 +43,18 @@ class ModelPolicyGenerator extends BaseGenerator
$policyFileContent = str_replace('App\User', $userModel, $policyFileContent);
}
$appNamespace = $this->getAppNamespace();
$policyFileContent = str_replace(
"App\Policies;",
"{$appNamespace}Policies;",
$policyFileContent
);
if (!is_null($parentName = $this->command->option('parent'))) {
$policyFileContent = str_replace(
'App\Policies;',
"App\Policies\\{$parentName};",
"{$appNamespace}Policies;",
"{$appNamespace}Policies\\{$parentName};",
$policyFileContent
);
}
@ -71,9 +79,11 @@ class ModelPolicyGenerator extends BaseGenerator
$modelName = $parentName.'\\'.$modelName;
}
$appNamespace = rtrim($this->getAppNamespace(), '\\');
$authSPContent = str_replace(
" protected \$policies = [\n",
" protected \$policies = [\n '{$fullModelName}' => 'App\Policies\\{$modelName}Policy',\n",
" protected \$policies = [\n '{$fullModelName}' => '{$appNamespace}\Policies\\{$modelName}Policy',\n",
$authSPContent
);

44
tests/CommandOptions/TestsOnlyOptionsTest.php

@ -0,0 +1,44 @@
<?php
namespace Tests\CommandOptions;
use Illuminate\Contracts\Console\Kernel;
use Tests\TestCase;
class TestOnlyOptionsTest extends TestCase
{
/** @test */
public function it_can_generate_only_tests_files()
{
$this->artisan('make:crud', [
'name' => $this->model_name,
'--tests-only' => true,
]);
$output = app(Kernel::class)->output();
$this->assertNotContains("{$this->model_name} model already exists.", $output);
$this->assertFileNotExists(app_path($this->model_name.'.php'));
$this->assertFileNotExists(app_path("Http/Controllers/{$this->plural_model_name}Controller.php"));
$migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$this->table_name.'_table.php');
$this->assertFileNotExists($migrationFilePath);
$this->assertFileNotExists(resource_path("views/{$this->table_name}/index.blade.php"));
$this->assertFileNotExists(resource_path("views/{$this->table_name}/forms.blade.php"));
$localeConfig = config('app.locale');
$this->assertFileNotExists(resource_path("lang/{$localeConfig}/{$this->lang_name}.php"));
$this->assertFileNotExists(base_path("routes/web.php"));
$this->assertFileNotExists(app_path("Policies/{$this->model_name}Policy.php"));
$this->assertFileNotExists(database_path("factories/{$this->model_name}Factory.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$this->model_name}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Policies/{$this->model_name}PolicyTest.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php"));
$this->assertContains('Test files generated successfully!', $output);
}
}
Loading…
Cancel
Save