From f6fb6ab817ba459b9f5b1acd4345942c07bb2018 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 28 Aug 2017 20:07:29 +0800 Subject: [PATCH] Refactor test cases --- tests/CrudMakeCommandTest.php | 23 ++++++++--------------- tests/Generators/ControllerGeneratorTest.php | 12 +++--------- tests/Generators/FeatureTestGeneratorTest.php | 12 +++--------- tests/Generators/MigrationGeneratorTest.php | 10 ++-------- tests/Generators/ModelGeneratorTest.php | 13 ++++--------- tests/Generators/ModelTestGeneratorTest.php | 12 +++--------- tests/TestCase.php | 25 +++++++++++++++++++++++++ 7 files changed, 48 insertions(+), 59 deletions(-) diff --git a/tests/CrudMakeCommandTest.php b/tests/CrudMakeCommandTest.php index 5d4e143..749f5d4 100644 --- a/tests/CrudMakeCommandTest.php +++ b/tests/CrudMakeCommandTest.php @@ -7,24 +7,17 @@ class CrudMakeCommandTest extends TestCase /** @test */ public function it_can_generate_simple_crud_files() { - $this->artisan('make:crud', ['name' => 'Item', '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); - $this->assertFileExists(app_path('Item.php')); - $this->assertFileExists(app_path('Http/Controllers/ItemsController.php')); + $this->assertFileExists(app_path($this->modelName.'.php')); + $this->assertFileExists(app_path("Http/Controllers/{$this->pluralModelName}Controller.php")); - $migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_items_table.php'); + $migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$this->tableName.'_table.php'); $this->assertFileExists($migrationFilePath); - $this->assertFileExists(resource_path('views/items/index.blade.php')); - $this->assertFileExists(resource_path('views/items/forms.blade.php')); - $this->assertFileExists(base_path('tests/Feature/ManageItemsTest.php')); - $this->assertFileExists(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')); + $this->assertFileExists(resource_path("views/{$this->tableName}/index.blade.php")); + $this->assertFileExists(resource_path("views/{$this->tableName}/forms.blade.php")); + $this->assertFileExists(base_path("tests/Feature/Manage{$this->pluralModelName}Test.php")); + $this->assertFileExists(base_path("tests/Unit/Models/{$this->modelName}Test.php")); } } diff --git a/tests/Generators/ControllerGeneratorTest.php b/tests/Generators/ControllerGeneratorTest.php index 3dbdbe4..aafe30a 100644 --- a/tests/Generators/ControllerGeneratorTest.php +++ b/tests/Generators/ControllerGeneratorTest.php @@ -9,9 +9,9 @@ class ControllerGeneratorTest extends TestCase /** @test */ public function it_creates_correct_controller_class_content() { - $this->artisan('make:crud', ['name' => 'Item', '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); - $this->assertFileExists(app_path('Item.php')); + $this->assertFileExists(app_path("Http/Controllers/{$this->pluralModelName}Controller.php")); $ctrlClassContent = "assertEquals($ctrlClassContent, file_get_contents(app_path('Http/Controllers/ItemsController.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')); + $this->assertEquals($ctrlClassContent, file_get_contents(app_path("Http/Controllers/{$this->pluralModelName}Controller.php"))); } } diff --git a/tests/Generators/FeatureTestGeneratorTest.php b/tests/Generators/FeatureTestGeneratorTest.php index 302a614..c732a35 100644 --- a/tests/Generators/FeatureTestGeneratorTest.php +++ b/tests/Generators/FeatureTestGeneratorTest.php @@ -9,9 +9,9 @@ class FeatureTestGeneratorTest extends TestCase /** @test */ public function it_creates_correct_feature_test_class_content() { - $this->artisan('make:crud', ['name' => 'Item', '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); - $this->assertFileExists(base_path('tests/Feature/ManageItemsTest.php')); + $this->assertFileExists(base_path("tests/Feature/Manage{$this->pluralModelName}Test.php")); $modelClassContent = "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')); + $this->assertEquals($modelClassContent, file_get_contents(base_path("tests/Feature/Manage{$this->pluralModelName}Test.php"))); } } diff --git a/tests/Generators/MigrationGeneratorTest.php b/tests/Generators/MigrationGeneratorTest.php index a17b69a..89bad16 100644 --- a/tests/Generators/MigrationGeneratorTest.php +++ b/tests/Generators/MigrationGeneratorTest.php @@ -9,9 +9,9 @@ class MigrationGeneratorTest extends TestCase /** @test */ public function it_creates_correct_migration_class_content() { - $this->artisan('make:crud', ['name' => 'Item', '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); - $migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_items_table.php'); + $migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$this->tableName.'_table.php'); $this->assertFileExists($migrationFilePath); $modelClassContent = "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')); } } diff --git a/tests/Generators/ModelGeneratorTest.php b/tests/Generators/ModelGeneratorTest.php index e41e079..edd1bce 100644 --- a/tests/Generators/ModelGeneratorTest.php +++ b/tests/Generators/ModelGeneratorTest.php @@ -9,9 +9,10 @@ class ModelGeneratorTest extends TestCase /** @test */ public function it_creates_correct_model_class_content() { - $this->artisan('make:crud', ['name' => 'Item', '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); - $this->assertFileExists(app_path('Item.php')); + $modelPath = app_path($this->modelName.'.php'); + $this->assertFileExists($modelPath); $modelClassContent = "assertEquals($modelClassContent, file_get_contents(app_path('Item.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')); + $this->assertEquals($modelClassContent, file_get_contents($modelPath)); } } diff --git a/tests/Generators/ModelTestGeneratorTest.php b/tests/Generators/ModelTestGeneratorTest.php index 18602eb..a62d0d6 100644 --- a/tests/Generators/ModelTestGeneratorTest.php +++ b/tests/Generators/ModelTestGeneratorTest.php @@ -9,9 +9,9 @@ class ModelTestGeneratorTest extends TestCase /** @test */ public function it_creates_correct_unit_test_class_content() { - $this->artisan('make:crud', ['name' => 'Item', '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); - $this->assertFileExists(base_path('tests/Unit/Models/ItemTest.php')); + $this->assertFileExists(base_path("tests/Unit/Models/{$this->modelName}Test.php")); $modelClassContent = "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')); + $this->assertEquals($modelClassContent, file_get_contents(base_path("tests/Unit/Models/{$this->modelName}Test.php"))); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 55e0567..2db688b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,6 +6,31 @@ use Orchestra\Testbench\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { + protected $modelName; + protected $pluralModelName; + protected $tableName; + + public function setUp() + { + parent::setUp(); + $this->modelName = 'Item'; + + $this->pluralModelName = str_plural($this->modelName); + $this->tableName = strtolower($this->pluralModelName); + } + + public function tearDown() + { + exec('rm '.app_path($this->modelName.'.php')); + exec('rm -r '.app_path('Http')); + exec('rm '.database_path('migrations/*')); + exec('rm -r '.resource_path('views/'.$this->tableName)); + exec('rm -r '.base_path('tests/Feature')); + exec('rm -r '.base_path('tests/Unit')); + + parent::tearDown(); + } + protected function getPackageProviders($app) { return ['Luthfi\CrudGenerator\ServiceProvider'];