Browse Source

Refactor test cases

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
f6fb6ab817
  1. 23
      tests/CrudMakeCommandTest.php
  2. 12
      tests/Generators/ControllerGeneratorTest.php
  3. 12
      tests/Generators/FeatureTestGeneratorTest.php
  4. 10
      tests/Generators/MigrationGeneratorTest.php
  5. 13
      tests/Generators/ModelGeneratorTest.php
  6. 12
      tests/Generators/ModelTestGeneratorTest.php
  7. 25
      tests/TestCase.php

23
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"));
}
}

12
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 = "<?php
namespace App\Http\Controllers;
@ -78,12 +78,6 @@ class ItemsController extends Controller
}
}
";
$this->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")));
}
}

12
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 = "<?php
namespace Tests\Feature;
@ -32,12 +32,6 @@ class ManageItemsTest extends TestCase
}
}
";
$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'));
$this->assertEquals($modelClassContent, file_get_contents(base_path("tests/Feature/Manage{$this->pluralModelName}Test.php")));
}
}

10
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 = "<?php
@ -48,11 +48,5 @@ class CreateItemsTable extends Migration
}
";
$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'));
}
}

13
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 = "<?php
namespace App;
@ -23,12 +24,6 @@ class Item extends Model
//
}
";
$this->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));
}
}

12
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 = "<?php
namespace DummyNamespace;
@ -32,12 +32,6 @@ class ItemTest extends TestCase
}
}
";
$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'));
$this->assertEquals($modelClassContent, file_get_contents(base_path("tests/Unit/Models/{$this->modelName}Test.php")));
}
}

25
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'];

Loading…
Cancel
Save