Skip to content

Commit

Permalink
Fix migration
Browse files Browse the repository at this point in the history
buld: validation placeholder stub on test and add option on create migration
  • Loading branch information
pemudakoding authored Dec 5, 2022
2 parents fb217b8 + dbcec06 commit aee612b
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 117 deletions.
11 changes: 11 additions & 0 deletions src/Commands/Concerns/HasOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,15 @@ protected function resolveFactoryOption(): bool
{
return (bool) $this->option(key: 'factory');
}

/**
* @return string|null
*/
protected function resolveTableName(): string | null
{
/** @var string|null $name */
$name = $this->option(key: 'create') ?: $this->option(key: 'table');

return $name;
}
}
21 changes: 16 additions & 5 deletions src/Commands/MakeMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
use Illuminate\Support\Str;
use KoalaFacade\DiamondConsole\Actions\Stub\CopyStubAction;
use KoalaFacade\DiamondConsole\Commands\Concerns\HasArguments;
use KoalaFacade\DiamondConsole\Commands\Concerns\HasOptions;
use KoalaFacade\DiamondConsole\Commands\Concerns\InteractsWithPath;
use KoalaFacade\DiamondConsole\DataTransferObjects\CopyStubData;
use KoalaFacade\DiamondConsole\DataTransferObjects\PlaceholderData;

class MakeMigrationCommand extends Command
{
use HasArguments, InteractsWithPath;
use HasArguments, HasOptions, InteractsWithPath;

protected $signature = 'diamond:migration {name} {--force}';
protected $signature = 'diamond:migration {name} {--create=} {--table=} {--force}';

protected $description = 'create new migration';

Expand All @@ -29,11 +30,21 @@ public function handle(): void

$destinationPath = base_path(path: 'database/migrations');

$stubPath = $this->resolvePathForStub(name: 'migration');
$stub = 'migration';

$tableName = Str::snake(Str::pluralStudly($this->resolveNameArgument()));
if ($this->option('create')) {
$stub .= '-create';
} elseif ($this->option('table')) {
$stub .= '-table';
}

$fileName = Carbon::now()->format(format: 'Y_m_d_his') . '_create_' . $tableName . '_table.php';
$stubPath = $this->resolvePathForStub(name: $stub);

$migrationName = Str::snake($this->resolveNameArgument());

$fileName = Carbon::now()->format(format: 'Y_m_d_his') . '_' . $migrationName . '.php';

$tableName = $this->resolveTableName() ? Str::snake(Str::pluralStudly($this->resolveTableName())) : '';

$placeholders = new PlaceholderData(tableName: $tableName);

Expand Down
7 changes: 5 additions & 2 deletions src/Commands/MakeModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Str;
use KoalaFacade\DiamondConsole\Actions\Filesystem\FilePresentAction;
use KoalaFacade\DiamondConsole\Actions\Stub\CopyStubAction;
use KoalaFacade\DiamondConsole\Commands\Concerns\HasArguments;
Expand Down Expand Up @@ -59,7 +60,7 @@ class: $this->resolveClassNameByFile(name: $fileName),
withForce: $this->resolveForceOption()
);

if ($filePresent) {
if ($filePresent && ! $this->resolveForceOption()) {
$this->warn(string: $fileName . ' already exists.');

return;
Expand Down Expand Up @@ -100,7 +101,9 @@ protected function resolveFactory(string $fileName): void
protected function resolveMigration(string $fileName): void
{
if ($this->option(key: 'migration')) {
Artisan::call(command: 'diamond:migration ' . $this->resolveClassNameByFile(name: $fileName));
$tableName = $this->resolveClassNameByFile(name: $fileName);

Artisan::call(command: 'diamond:migration Create' . Str::pluralStudly($tableName) . 'Table --create=' . $tableName);
}
}
}
31 changes: 31 additions & 0 deletions stubs/migration-create.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create(table: '{{ table_name }}', callback: function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::dropIfExists(table: '{{ table_name }}');
}
};
32 changes: 32 additions & 0 deletions stubs/migration-table.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table(table: '{{ table_name }}', callback: function (Blueprint $table) {
//
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table(table: '{{ table_name }}', callback: function (Blueprint $table) {
//
});
}
};
7 changes: 2 additions & 5 deletions stubs/migration.stub
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ return new class extends Migration
*/
public function up(): void
{
Schema::create(table: '{{ tableName }}', callback: function (Blueprint $table) {
$table->id();
$table->timestamps();
});
//
}

/**
Expand All @@ -26,6 +23,6 @@ return new class extends Migration
*/
public function down(): void
{
Schema::dropIfExists(table: '{{ tableName }}');
//
}
};
10 changes: 10 additions & 0 deletions tests/Feature/Commands/GenerateActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Tests\Feature\Commands;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use KoalaFacade\DiamondConsole\Exceptions\FileAlreadyExistException;

it(description: 'can generate new action class')
Expand All @@ -17,6 +19,10 @@

expect(filePresent(fileName: $fileName))
->toBeTrue();

$actionFile = File::get(path: basePath() . domainPath() . $fileName);

expect(value: Str::contains(haystack: $actionFile, needles: ['{{ class }}', '{{ namespace }}']))->toBeFalse();
})
->group('commands');

Expand All @@ -33,6 +39,10 @@

expect(filePresent(fileName: $fileName))
->toBeTrue();

$actionFile = File::get(path: basePath() . domainPath() . $fileName);

expect(value: Str::contains(haystack: $actionFile, needles: ['{{ class }}', '{{ namespace }}']))->toBeFalse();
})
->group(groups: 'commands');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Tests\Feature\Commands;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use KoalaFacade\DiamondConsole\Exceptions\FileAlreadyExistException;

it(description: 'can generate new DTO')
Expand All @@ -15,18 +17,27 @@
Artisan::call(command: 'diamond:dto PostData Post');

expect(filePresent($fileName))->toBeTrue();

$dtoFile = File::get(path: basePath() . domainPath() . $fileName);

expect(value: Str::contains(haystack: $dtoFile, needles: ['{{ class }}', '{{ namespace }}']))->toBeFalse();
})
->group(groups: 'commands');

it(description: 'can force generate exists DTO')
->tap(function () {
expect(filePresent(fileName: '/Post/DataTransferObjects/StoreUserAction.php'))->toBeFalse();
$fileName = '/Post/DataTransferObjects/PostData.php';
expect(filePresent(fileName: $fileName))->toBeFalse();

Artisan::call(command: 'diamond:install');
Artisan::call(command: 'diamond:dto PostData Post');
Artisan::call(command: 'diamond:dto PostData Post --force');

expect(filePresent(fileName: '/Post/DataTransferObjects/PostData.php'))->toBeTrue();
expect(filePresent(fileName: $fileName))->toBeTrue();

$dtoFile = File::get(path: basePath() . domainPath() . $fileName);

expect(value: Str::contains(haystack: $dtoFile, needles: ['{{ class }}', '{{ namespace }}']))->toBeFalse();
})
->group(groups: 'commands');

Expand Down
20 changes: 15 additions & 5 deletions tests/Feature/Commands/GenerateEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Tests\Feature\Commands;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use KoalaFacade\DiamondConsole\Exceptions\FileAlreadyExistException;

it(description: 'can generate new enum')
->skip(version_compare(PHP_VERSION, '8.1.0', '<'), 'code contains php 8.1 feature cause this test run in ' . PHP_VERSION)
->tap(function () {
$fileName = '/Post/Enums/PostStatus.php';

Expand All @@ -15,11 +18,15 @@
Artisan::call(command: 'diamond:enum PostStatus Post');

expect(filePresent($fileName))->toBeTrue();

$enumFile = File::get(path: basePath() . domainPath() . $fileName);

expect(value: Str::contains(haystack: $enumFile, needles: ['{{ class }}', '{{ namespace }}']))->toBeFalse();
})
->group(groups: 'commands')
->skip(version_compare(PHP_VERSION, '8.1.0', '<='), 'code contains php 8.1 feature cause this test run in ' . PHP_VERSION);
->group(groups: 'commands');

it(description: 'can force generate exists enum')
->skip(version_compare(PHP_VERSION, '8.1.0', '<'), 'code contains php 8.1 feature cause this test run in ' . PHP_VERSION)
->tap(function () {
$fileName = '/Post/Enums/PostStatus.php';

Expand All @@ -30,16 +37,19 @@
Artisan::call(command: 'diamond:enum PostStatus Post --force');

expect(filePresent($fileName))->toBeTrue();

$enumFile = File::get(path: basePath() . domainPath() . $fileName);

expect(value: Str::contains(haystack: $enumFile, needles: ['{{ class }}', '{{ namespace }}']))->toBeFalse();
})
->group(groups: 'commands')
->skip(version_compare(PHP_VERSION, '8.1.0', '<='), 'code contains php 8.1 feature cause this test run in ' . PHP_VERSION);
->group(groups: 'commands');

it(description: 'file already exist')
->skip(version_compare(PHP_VERSION, '8.1.0', '<'), 'code contains php 8.1 feature cause this test run in ' . PHP_VERSION)
->tap(function () {
Artisan::call(command: 'diamond:install');
Artisan::call(command: 'diamond:enum PostStatus Post');
Artisan::call(command: 'diamond:enum PostStatus Post');
})
->skip(version_compare(PHP_VERSION, '8.1.0', '<='), 'code contains php 8.1 feature cause this test run in ' . PHP_VERSION)
->group(groups: 'commands')
->throws(exception: FileAlreadyExistException::class);
12 changes: 12 additions & 0 deletions tests/Feature/Commands/GenerateFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
)
)->toBeTrue();

$factoryContractFile = File::get(path: $factoryContractPath);
$factoryConcreteFile = File::get(path: $factoryConcretePath);

expect(value: Str::contains(haystack: $factoryContractFile, needles: ['{{ class }}', '{{ namespace }}']))->toBeFalse();
expect(value: Str::contains(haystack: $factoryConcreteFile, needles: ['{{ class }}', '{{ namespace }}']))->toBeFalse();

File::delete([$factoryContractPath, $factoryConcretePath]);
}
)->group('commands');
Expand Down Expand Up @@ -65,6 +71,12 @@
)
)->toBeTrue();

$factoryContractFile = File::get(path: $factoryContractPath);
$factoryConcreteFile = File::get(path: $factoryConcretePath);

expect(value: Str::contains(haystack: $factoryContractFile, needles: ['{{ class }}', '{{ namespace }}']))->toBeFalse();
expect(value: Str::contains(haystack: $factoryConcreteFile, needles: ['{{ class }}', '{{ namespace }}']))->toBeFalse();

File::delete([$factoryContractPath, $factoryConcretePath]);
}
)->group('commands');
10 changes: 10 additions & 0 deletions tests/Feature/Commands/GenerateMailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Tests\Feature\Commands;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use KoalaFacade\DiamondConsole\Exceptions\FileAlreadyExistException;

it(description: 'can generate new mail class')
Expand All @@ -15,6 +17,10 @@
Artisan::call(command: 'diamond:mail UserApproved User');

expect(filePresent($fileName, prefix: infrastructurePath()))->toBeTrue();

$mailFile = File::get(path: basePath() . infrastructurePath() . $fileName);

expect(value: Str::contains(haystack: $mailFile, needles: ['{{ class }}', '{{ namespace }}']))->toBeFalse();
})
->group('commands');

Expand All @@ -29,6 +35,10 @@
Artisan::call(command: 'diamond:mail UserApproved User --force');

expect(filePresent($fileName, prefix: infrastructurePath()))->toBeTrue();

$mailFile = File::get(path: basePath() . infrastructurePath() . $fileName);

expect(value: Str::contains(haystack: $mailFile, needles: ['{{ class }}', '{{ namespace }}']))->toBeFalse();
})
->group(groups: 'commands');

Expand Down
Loading

0 comments on commit aee612b

Please sign in to comment.