From 77dbffb374ea973e0791629d8b35aa866b36d602 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 22 Aug 2017 22:18:02 +0800 Subject: [PATCH] Refactor CrudMake command class Added migration class, unit test, and feature test class content --- src/CrudMake.php | 61 +++++++++++---------- tests/CrudMakeCommandTest.php | 123 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+), 31 deletions(-) diff --git a/src/CrudMake.php b/src/CrudMake.php index 1a5ffe6..115427a 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -44,8 +44,8 @@ class CrudMake extends Command $this->getModelName(); $this->generateModel(); - $this->generateController(); $this->generateMigration(); + $this->generateController(); $this->generateViews(); $this->generateTests(); @@ -69,81 +69,80 @@ class CrudMake extends Command public function generateController() { - if (! $this->files->isDirectory(app_path('Http/Controllers'))) { - $this->files->makeDirectory(app_path('Http/Controllers'), 0777, true, true); - } + $controllerPath = $this->makeDirectory(app_path('Http/Controllers')); - $controllerPath = app_path('Http/Controllers/'.$this->pluralModelName.'Controller.php'); - $this->files->put($controllerPath, $this->getControllerStub()); + $controllerPath = $controllerPath.'/'.$this->pluralModelName.'Controller.php'; + $this->files->put($controllerPath, $this->getControllerContent()); $this->info($this->pluralModelName.'Controller generated.'); } public function generateMigration() { - $migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$this->lowerCasePluralModel.'_table.php'); - $this->files->put($migrationFilePath, $this->getMigrationStub()); + $prefix = date('Y_m_d_His'); + $tableName = $this->lowerCasePluralModel; + $migrationFilePath = database_path("migrations/{$prefix}_create_{$tableName}_table.php"); + $this->files->put($migrationFilePath, $this->getMigrationContent()); $this->info($this->modelName.' table migration generated.'); } public function generateViews() { - $viewPath = resource_path('views/'.$this->lowerCasePluralModel); - if (! $this->files->isDirectory($viewPath)) { - $this->files->makeDirectory($viewPath, 0777, true, true); - } + $viewPath = $this->makeDirectory(resource_path('views/'.$this->lowerCasePluralModel)); - $this->files->put($viewPath.'/index.blade.php', $this->getIndexViewStub()); - $this->files->put($viewPath.'/forms.blade.php', $this->getFormsViewStub()); + $this->files->put($viewPath.'/index.blade.php', $this->getIndexViewContent()); + $this->files->put($viewPath.'/forms.blade.php', $this->getFormsViewContent()); $this->info($this->modelName.' view files generated.'); } public function generateTests() { - $featureTestPath = base_path('tests/Feature'); - if (! $this->files->isDirectory($featureTestPath)) { - $this->files->makeDirectory($featureTestPath, 0777, true, true); - } - $this->files->put($featureTestPath.'/Manage'.$this->pluralModelName.'Test.php', $this->getFeatureTestStub()); + $featureTestPath = $this->makeDirectory(base_path('tests/Feature')); + $this->files->put("{$featureTestPath}/Manage{$this->pluralModelName}Test.php", $this->getFeatureTestContent()); $this->info('Manage'.$this->pluralModelName.'Test generated.'); - $unitTestPath = base_path('tests/Unit/Models'); - if (! $this->files->isDirectory($unitTestPath)) { - $this->files->makeDirectory($unitTestPath, 0777, true, true); - } - - $this->files->put($unitTestPath.'/'.$this->modelName.'Test.php', $this->getUnitTestStub()); + $unitTestPath = $this->makeDirectory(base_path('tests/Unit/Models')); + $this->files->put("{$unitTestPath}/{$this->modelName}Test.php", $this->getUnitTestContent()); $this->info($this->modelName.'Test (model) generated.'); } - public function getControllerStub() + public function getControllerContent() { return $this->files->get(__DIR__.'/stubs/controller.model.stub'); } - private function getMigrationStub() + private function getMigrationContent() { return $this->files->get(__DIR__.'/stubs/migration-create.stub'); } - public function getIndexViewStub() + public function getIndexViewContent() { return $this->files->get(__DIR__.'/stubs/view-index.stub'); } - public function getFormsViewStub() + public function getFormsViewContent() { return $this->files->get(__DIR__.'/stubs/view-forms.stub'); } - public function getFeatureTestStub() + public function getFeatureTestContent() { return $this->files->get(__DIR__.'/stubs/test.stub'); } - public function getUnitTestStub() + public function getUnitTestContent() { return $this->files->get(__DIR__.'/stubs/unit-test.stub'); } + + protected function makeDirectory($path) + { + if (! $this->files->isDirectory($path)) { + $this->files->makeDirectory($path, 0777, true, true); + } + + return $path; + } } diff --git a/tests/CrudMakeCommandTest.php b/tests/CrudMakeCommandTest.php index 5660b7e..3f348f3 100644 --- a/tests/CrudMakeCommandTest.php +++ b/tests/CrudMakeCommandTest.php @@ -139,4 +139,127 @@ class DummyClass extends Controller exec('rm -r '.base_path('tests/Feature')); exec('rm -r '.base_path('tests/Unit')); } + + /** @test */ + public function it_creates_correct_feature_test_class_content() + { + $this->artisan('make:crud', ['name' => 'Item', '--no-interaction' => true]); + + $this->assertFileExists(base_path('tests/Feature/ManageItemsTest.php')); + $modelClassContent = "assertTrue(true); + } +} +"; + $this->assertEquals($modelClassContent, file_get_contents(base_path('tests/Feature/ManageItemsTest.php'))); + exec('rm '.app_path('Item.php')); + exec('rm -r '.app_path('Http')); + exec('rm '.database_path('migrations/*')); + exec('rm -r '.resource_path('views/items')); + exec('rm -r '.base_path('tests/Feature')); + exec('rm -r '.base_path('tests/Unit')); + } + + /** @test */ + public function it_creates_correct_unit_test_class_content() + { + $this->artisan('make:crud', ['name' => 'Item', '--no-interaction' => true]); + + $this->assertFileExists(base_path('tests/Unit/Models/ItemTest.php')); + $modelClassContent = "assertTrue(true); + } +} +"; + $this->assertEquals($modelClassContent, file_get_contents(base_path('tests/Unit/Models/ItemTest.php'))); + exec('rm '.app_path('Item.php')); + exec('rm -r '.app_path('Http')); + exec('rm '.database_path('migrations/*')); + exec('rm -r '.resource_path('views/items')); + exec('rm -r '.base_path('tests/Feature')); + exec('rm -r '.base_path('tests/Unit')); + } + + /** @test */ + public function it_creates_correct_migration_class_content() + { + $this->artisan('make:crud', ['name' => 'Item', '--no-interaction' => true]); + + $migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_items_table.php'); + $this->assertFileExists($migrationFilePath); + $modelClassContent = "increments('id'); + \$table->string('name', 60); + \$table->string('description'); + \$table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('DummyTable'); + } +} +"; + $this->assertEquals($modelClassContent, file_get_contents($migrationFilePath)); + exec('rm '.app_path('Item.php')); + exec('rm -r '.app_path('Http')); + exec('rm '.database_path('migrations/*')); + exec('rm -r '.resource_path('views/items')); + exec('rm -r '.base_path('tests/Feature')); + exec('rm -r '.base_path('tests/Unit')); + } }