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. 44
      src/CrudMake.php
  2. 12
      src/Generators/ControllerGenerator.php
  3. 15
      src/Generators/FeatureTestGenerator.php
  4. 28
      src/Generators/ModelPolicyGenerator.php
  5. 10
      src/Generators/WebRouteGenerator.php
  6. 44
      tests/CommandOptions/TestsOnlyOptionsTest.php

44
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!');
}
@ -120,11 +129,11 @@ class CrudMake extends Command
*/
public function getModelName($modelName = null)
{
$modelName = is_null($modelName) ? $this->argument('name') : $modelName;
$model_name = ucfirst(class_basename($modelName));
$modelName = is_null($modelName) ? $this->argument('name') : $modelName;
$model_name = ucfirst(class_basename($modelName));
$plural_model_name = str_plural($model_name);
$modelPath = $this->getModelPath($modelName);
$modelNamespace = $this->getModelNamespace($modelPath);
$modelPath = $this->getModelPath($modelName);
$modelNamespace = $this->getModelNamespace($modelPath);
return $this->modelNames = [
'model_namespace' => $modelNamespace,
@ -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;
}
@ -184,8 +194,20 @@ class CrudMake extends Command
*/
public function defaultLayoutNotExists()
{
return ! $this->files->exists(
return !$this->files->exists(
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",

15
src/Generators/FeatureTestGenerator.php

@ -16,7 +16,7 @@ class FeatureTestGenerator extends BaseGenerator
$featureTestPath = $this->makeDirectory(base_path('tests/Feature'));
$this->generateFile("{$featureTestPath}/Manage{$this->modelNames['plural_model_name']}Test.php", $this->getContent());
$this->command->info('Manage' . $this->modelNames['plural_model_name'] . 'Test generated.');
$this->command->info('Manage'.$this->modelNames['plural_model_name'].'Test generated.');
}
/**
@ -24,9 +24,9 @@ class FeatureTestGenerator extends BaseGenerator
*/
protected function getContent()
{
$stub = $this->files->get(__DIR__ . '/../stubs/test-feature.stub');
$stub = $this->files->get(__DIR__.'/../stubs/test-feature.stub');
$baseTestClass = config('simple-crud.base_test_class');
$stub = str_replace('use Tests\BrowserKitTest', 'use ' . $baseTestClass, $stub);
$stub = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $stub);
return $this->replaceStubString($stub);
}
@ -42,13 +42,14 @@ class FeatureTestGenerator extends BaseGenerator
$this->files->makeDirectory($testsPath, 0777, true, true);
}
$baseTestPath = base_path(config('simple-crud.base_test_path'));
$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()
);
@ -65,6 +66,6 @@ class FeatureTestGenerator extends BaseGenerator
*/
public function getBrowserKitBaseTestContent()
{
return $this->files->get(__DIR__ . '/../stubs/test-browserkit-base-class.stub');
return $this->files->get(__DIR__.'/../stubs/test-browserkit-base-class.stub');
}
}

28
src/Generators/ModelPolicyGenerator.php

@ -3,8 +3,8 @@
namespace Luthfi\CrudGenerator\Generators;
/**
* Model Policy Generator Class
*/
* Model Policy Generator Class
*/
class ModelPolicyGenerator extends BaseGenerator
{
/**
@ -13,7 +13,7 @@ class ModelPolicyGenerator extends BaseGenerator
public function generate()
{
$parentDirectory = '';
if (! is_null($this->command->option('parent'))) {
if (!is_null($this->command->option('parent'))) {
$parentDirectory = '/'.$this->command->option('parent');
}
$modelPolicyPath = $this->makeDirectory(app_path('Policies'.$parentDirectory));
@ -43,10 +43,18 @@ class ModelPolicyGenerator extends BaseGenerator
$policyFileContent = str_replace('App\User', $userModel, $policyFileContent);
}
if (! is_null($parentName = $this->command->option('parent'))) {
$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
);
}
@ -67,13 +75,15 @@ class ModelPolicyGenerator extends BaseGenerator
$authSPContent = $this->files->get($authSPPath);
if (! is_null($parentName = $this->command->option('parent'))) {
if (!is_null($parentName = $this->command->option('parent'))) {
$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
);
@ -92,7 +102,7 @@ class ModelPolicyGenerator extends BaseGenerator
{
$routeDirPath = $this->makeDirectory($routeDirPath);
if (! $this->files->exists($routeDirPath.'/'.$filename)) {
if (!$this->files->exists($routeDirPath.'/'.$filename)) {
$this->generateFile(
$routeDirPath.'/'.$filename,
$this->files->get(__DIR__.'/../stubs/AuthServiceProvider.stub')

10
src/Generators/WebRouteGenerator.php

@ -3,8 +3,8 @@
namespace Luthfi\CrudGenerator\Generators;
/**
* Web Route Generator Class
*/
* Web Route Generator Class
*/
class WebRouteGenerator extends BaseGenerator
{
/**
@ -27,7 +27,7 @@ class WebRouteGenerator extends BaseGenerator
$webRouteFileContent = $this->replaceStubString($stub);
if (! is_null($parentName = $this->command->option('parent'))) {
if (!is_null($parentName = $this->command->option('parent'))) {
$pluralModelName = $this->modelNames['plural_model_name'];
$webRouteFileContent = str_replace(
@ -48,11 +48,11 @@ class WebRouteGenerator extends BaseGenerator
*/
protected function makeRouteFile($routeDirPath, $filename)
{
if (! $this->files->isDirectory($routeDirPath)) {
if (!$this->files->isDirectory($routeDirPath)) {
$this->files->makeDirectory($routeDirPath, 0777, true, true);
}
if (! $this->files->exists($routeDirPath.'/'.$filename)) {
if (!$this->files->exists($routeDirPath.'/'.$filename)) {
$this->generateFile($routeDirPath.'/'.$filename, "<?php\n");
}

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