Browse Source

Refactor CrudMake command class

Added migration class, unit test, and feature test class content
tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
77dbffb374
  1. 61
      src/CrudMake.php
  2. 123
      tests/CrudMakeCommandTest.php

61
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;
}
}

123
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 = "<?php
namespace DummyNamespace;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class DummyClass extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
\$this->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 = "<?php
namespace DummyNamespace;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class DummyClass extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
\$this->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 = "<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class DummyClass extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('DummyTable', function (Blueprint \$table) {
\$table->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'));
}
}
Loading…
Cancel
Save