Browse Source

Prepare namespace detection trait on base generator class

tags/0.2.0
Nafies Luthfi 8 years ago
parent
commit
b4d7d7ad11
  1. 23
      src/Generators/BaseGenerator.php
  2. 16
      src/Generators/ControllerGenerator.php
  3. 28
      tests/CrudMakeCommandTest.php
  4. 12
      tests/TestCase.php

23
src/Generators/BaseGenerator.php

@ -2,14 +2,17 @@
namespace Luthfi\CrudGenerator\Generators;
use Illuminate\Console\DetectsApplicationNamespace;
use Illuminate\Filesystem\Filesystem;
use Luthfi\CrudGenerator\CrudMake;
/**
* Base Generator Class
*/
* Base Generator Class
*/
abstract class BaseGenerator
{
use DetectsApplicationNamespace;
/**
* The injected Filesystem class
*
@ -56,14 +59,14 @@ abstract class BaseGenerator
protected function getStubModelNames()
{
return $this->stubModelNames = [
'model_namespace' => 'mstrNmspc',
'full_model_name' => 'fullMstr',
'plural_model_name' => 'Masters',
'model_name' => 'Master',
'table_name' => 'masters',
'lang_name' => 'master',
'model_namespace' => 'mstrNmspc',
'full_model_name' => 'fullMstr',
'plural_model_name' => 'Masters',
'model_name' => 'Master',
'table_name' => 'masters',
'lang_name' => 'master',
'collection_model_var_name' => 'mstrCollections',
'single_model_var_name' => 'singleMstr',
'single_model_var_name' => 'singleMstr',
];
}
@ -88,7 +91,7 @@ abstract class BaseGenerator
*/
protected function makeDirectory($path)
{
if (! $this->files->isDirectory($path)) {
if (!$this->files->isDirectory($path)) {
$this->files->makeDirectory($path, 0777, true, true);
}

16
src/Generators/ControllerGenerator.php

@ -3,8 +3,8 @@
namespace Luthfi\CrudGenerator\Generators;
/**
* Controller Generator Class
*/
* Controller Generator Class
*/
class ControllerGenerator extends BaseGenerator
{
/**
@ -13,7 +13,7 @@ class ControllerGenerator extends BaseGenerator
public function generate()
{
$parentControllerDirectory = '';
if (! is_null($this->command->option('parent'))) {
if (!is_null($this->command->option('parent'))) {
$parentControllerDirectory = '/'.$this->command->option('parent');
}
$controllerPath = $this->makeDirectory(app_path('Http/Controllers'.$parentControllerDirectory));
@ -33,15 +33,17 @@ class ControllerGenerator extends BaseGenerator
$controllerFileContent = $this->replaceStubString($stub);
if (! is_null($parentName = $this->command->option('parent'))) {
if (!is_null($parentName = $this->command->option('parent'))) {
$searches = [
'App\Http\Controllers;',
"use {$this->modelNames['full_model_name']};\n"
"use {$this->modelNames['full_model_name']};\n",
];
$appNamespace = $this->getAppNamespace();
$replacements = [
"App\Http\Controllers\\{$parentName};",
"use {$this->modelNames['full_model_name']};\nuse App\Http\Controllers\Controller;\n"
"{$appNamespace}Http\Controllers\\{$parentName};",
"use {$this->modelNames['full_model_name']};\nuse {$appNamespace}Http\Controllers\Controller;\n",
];
$controllerFileContent = str_replace(

28
tests/CrudMakeCommandTest.php

@ -11,7 +11,7 @@ class CrudMakeCommandTest extends TestCase
{
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
$this->assertNotRegExp("/{$this->model_name} model already exists./", app(Kernel::class)->output());
$this->assertNotContains("{$this->model_name} model already exists.", app(Kernel::class)->output());
$this->assertFileExists(app_path($this->model_name.'.php'));
$this->assertFileExists(app_path("Http/Controllers/{$this->plural_model_name}Controller.php"));
@ -25,11 +25,11 @@ class CrudMakeCommandTest extends TestCase
$localeConfig = config('app.locale');
$this->assertFileExists(resource_path("lang/{$localeConfig}/{$this->lang_name}.php"));
$this->assertFileExists(database_path("factories/{$this->model_name}Factory.php"));
$this->assertFileExists(base_path("routes/web.php"));
$this->assertFileExists(app_path("Policies/{$this->model_name}Policy.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php"));
$this->assertFileExists(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/Feature/Manage{$this->plural_model_name}Test.php"));
}
/** @test */
@ -38,7 +38,7 @@ class CrudMakeCommandTest extends TestCase
$this->artisan('make:model', ['name' => $this->model_name, '--no-interaction' => true]);
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]);
$this->assertRegExp("/{$this->model_name} model already exists./", app(Kernel::class)->output());
$this->assertContains("{$this->model_name} model already exists.", app(Kernel::class)->output());
$this->assertFileExists(app_path($this->model_name.'.php'));
$this->assertFileNotExists(app_path("Http/Controllers/{$this->plural_model_name}Controller.php"));
@ -52,11 +52,11 @@ class CrudMakeCommandTest extends TestCase
$localeConfig = config('app.locale');
$this->assertFileNotExists(resource_path("lang/{$localeConfig}/{$this->lang_name}.php"));
$this->assertFileNotExists(database_path("factories/{$this->model_name}Factory.php"));
$this->assertFileNotExists(base_path("routes/web.php"));
$this->assertFileNotExists(app_path("Policies/{$this->model_name}Policy.php"));
$this->assertFileNotExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.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/Feature/Manage{$this->plural_model_name}Test.php"));
}
/** @test */
@ -65,7 +65,7 @@ class CrudMakeCommandTest extends TestCase
$this->artisan('make:model', ['name' => 'Entities/Projects/Problem', '--no-interaction' => true]);
$this->artisan('make:crud', ['name' => 'Entities/Projects/Problem', '--no-interaction' => true]);
$this->assertRegExp("/Problem model already exists./", app(Kernel::class)->output());
$this->assertContains("Problem model already exists.", app(Kernel::class)->output());
$this->assertFileExists(app_path('Entities/Projects/Problem.php'));
$this->assertFileNotExists(app_path("Http/Controllers/ProblemsController.php"));
@ -79,10 +79,10 @@ class CrudMakeCommandTest extends TestCase
$localeConfig = config('app.locale');
$this->assertFileNotExists(resource_path("lang/{$localeConfig}/{$this->lang_name}.php"));
$this->assertFileNotExists(database_path("factories/ProblemFactory.php"));
$this->assertFileNotExists(app_path("Policies/ProblemPolicy.php"));
$this->assertFileNotExists(base_path("tests/Feature/ManageProblemsTest.php"));
$this->assertFileNotExists(database_path("factories/ProblemFactory.php"));
$this->assertFileNotExists(base_path("tests/Unit/Models/ProblemTest.php"));
$this->assertFileNotExists(base_path("tests/Feature/ManageProblemsTest.php"));
$this->removeFileOrDir(app_path('Entities/Projects'));
$this->removeFileOrDir(resource_path('views/problems'));
@ -101,7 +101,7 @@ class CrudMakeCommandTest extends TestCase
$this->artisan('make:crud', ['name' => $inputName, '--no-interaction' => true]);
$this->assertNotRegExp("/{$modelName} model already exists./", app(Kernel::class)->output());
$this->assertNotContains("{$modelName} model already exists.", app(Kernel::class)->output());
$this->assertFileExists(app_path($modelPath.'/'.$modelName.'.php'));
$this->assertFileExists(app_path("Http/Controllers/{$pluralModelName}Controller.php"));
@ -115,10 +115,10 @@ class CrudMakeCommandTest extends TestCase
$localeConfig = config('app.locale');
$this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php"));
$this->assertFileExists(database_path("factories/{$modelName}Factory.php"));
$this->assertFileExists(app_path("Policies/{$modelName}Policy.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php"));
$this->assertFileExists(database_path("factories/{$modelName}Factory.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php"));
}
/** @test */
@ -134,7 +134,7 @@ class CrudMakeCommandTest extends TestCase
$this->artisan('make:crud', ['name' => $inputName, '--parent' => $parentName, '--no-interaction' => true]);
$this->assertNotRegExp("/{$modelName} model already exists./", app(Kernel::class)->output());
$this->assertNotContains("{$modelName} model already exists.", app(Kernel::class)->output());
$this->assertFileExists(app_path($modelPath.'/'.$modelName.'.php'));
$this->assertFileExists(app_path("Http/Controllers/{$parentName}/{$pluralModelName}Controller.php"));
@ -149,8 +149,8 @@ class CrudMakeCommandTest extends TestCase
$this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php"));
$this->assertFileExists(database_path("factories/{$modelName}Factory.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php"));
$this->assertFileExists(app_path("Policies/{$parentName}/{$modelName}Policy.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php"));
}
}

12
tests/TestCase.php

@ -19,12 +19,12 @@ abstract class TestCase extends BaseTestCase
parent::setUp();
$this->model_name = class_basename('References/Category');
$this->full_model_name = 'App\\'.$this->model_name;
$this->plural_model_name = str_plural($this->model_name);
$this->table_name = snake_case($this->plural_model_name);
$this->lang_name = snake_case($this->model_name);
$this->full_model_name = 'App\\'.$this->model_name;
$this->plural_model_name = str_plural($this->model_name);
$this->table_name = snake_case($this->plural_model_name);
$this->lang_name = snake_case($this->model_name);
$this->collection_model_var_name = camel_case($this->plural_model_name);
$this->single_model_var_name = camel_case($this->model_name);
$this->single_model_var_name = camel_case($this->model_name);
}
public function tearDown()
@ -46,7 +46,7 @@ abstract class TestCase extends BaseTestCase
$defaultLayoutsFile = config('simple-crud.default_layout_view');
$dataViewPathArray = explode('.', $defaultLayoutsFile);
$fileName = array_pop($dataViewPathArray);
$fileName = array_pop($dataViewPathArray);
$defaultLayoutPath = resource_path('views/'.implode('/', $dataViewPathArray));
$this->removeFileOrDir($defaultLayoutPath);

Loading…
Cancel
Save