diff --git a/src/Exceptions/UnknownFieldTypeException.php b/src/Exceptions/UnknownFieldTypeException.php new file mode 100644 index 00000000..2f4d3ccc --- /dev/null +++ b/src/Exceptions/UnknownFieldTypeException.php @@ -0,0 +1,15 @@ +typeName} in {$this->generatorName}."); + } +} diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 97d08e32..64566c49 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -3,8 +3,9 @@ namespace RonasIT\Support\Generators; use Carbon\Carbon; -use Exception; +use Illuminate\Support\Str; use RonasIT\Support\Events\SuccessCreateMessage; +use RonasIT\Support\Exceptions\UnknownFieldTypeException; class MigrationGenerator extends EntityGenerator { @@ -24,6 +25,7 @@ public function generate(): void 'fields' => $this->fields, 'table' => $this->generateTable($this->fields) ]); + $now = Carbon::now()->format('Y_m_d_His'); $this->saveClass('migrations', "{$now}_{$entities}_create_table", $content); @@ -31,22 +33,22 @@ public function generate(): void event(new SuccessCreateMessage("Created a new Migration: {$entities}_create_table")); } - protected function isJson($typeName): bool + protected function isJson(string $typeName): bool { - return $typeName == 'json'; + return $typeName === 'json'; } - protected function isRequired($typeName): bool + protected function isRequired(string $typeName): bool { - return !empty(explode('-', $typeName)[1]); + return Str::afterLast($typeName, '-') === 'required'; } - protected function isNullable($typeName): bool + protected function isNullable(string $typeName): bool { return empty(explode('-', $typeName)[1]); } - protected function getJsonLine($fieldName): string + protected function getJsonLine(string $fieldName): string { if (env("DB_CONNECTION") == "mysql") { return "\$table->json('{$fieldName}')->nullable();"; @@ -55,7 +57,7 @@ protected function getJsonLine($fieldName): string return "\$table->jsonb('{$fieldName}')->default(\"{}\");"; } - protected function getRequiredLine($fieldName, $typeName): string + protected function getRequiredLine(string $fieldName, string $typeName): string { $type = explode('-', $typeName)[0]; @@ -66,27 +68,27 @@ protected function getRequiredLine($fieldName, $typeName): string return "\$table->{$type}('{$fieldName}');"; } - protected function getNonRequiredLine($fieldName, $typeName): string + protected function getNonRequiredLine(string $fieldName, string $typeName): string { $type = explode('-', $typeName)[0]; return "\$table->{$type}('{$fieldName}')->nullable();"; } - protected function generateTable($fields): array + protected function generateTable(array $fields): array { $resultTable = []; foreach ($fields as $typeName => $fieldNames) { foreach ($fieldNames as $fieldName) { - array_push($resultTable, $this->getTableRow($fieldName, $typeName)); + $resultTable[] = $this->getTableRow($fieldName, $typeName); } } return $resultTable; } - protected function getTableRow($fieldName, $typeName): string + protected function getTableRow(string $fieldName, string $typeName): string { if ($this->isJson($typeName)) { return $this->getJsonLine($fieldName); @@ -100,6 +102,6 @@ protected function getTableRow($fieldName, $typeName): string return $this->getNonRequiredLine($fieldName, $typeName); } - throw new Exception('Unknown fieldType in MigrationGenerator'); + throw new UnknownFieldTypeException($typeName, 'MigrationGenerator'); } } \ No newline at end of file diff --git a/stubs/migration.blade.php b/stubs/migration.blade.php index 0d10701b..747e72bd 100644 --- a/stubs/migration.blade.php +++ b/stubs/migration.blade.php @@ -67,4 +67,4 @@ public function createTable(): void }); } @endif -}; \ No newline at end of file +}; diff --git a/tests/MigrationGeneratorTest.php b/tests/MigrationGeneratorTest.php new file mode 100644 index 00000000..ac272a48 --- /dev/null +++ b/tests/MigrationGeneratorTest.php @@ -0,0 +1,91 @@ +setupConfigurations(); + + $this->assertExceptionThrew( + className: UnknownFieldTypeException::class, + message: 'Unknown field type unknown-type in MigrationGenerator.', + ); + + app(MigrationGenerator::class) + ->setModel('Post') + ->setRelations([ + 'belongsTo' => [], + 'belongsToMany' => [], + 'hasOne' => [], + 'hasMany' => [], + ]) + ->setFields([ + 'integer-required' => ['media_id', 'user_id'], + 'unknown-type' => ['title'], + ]) + ->generate(); + } + + public function testCreateMigration() + { + Carbon::setTestNow('2022-02-02'); + + $this->mockFilesystem(); + $this->setupConfigurations(); + + app(MigrationGenerator::class) + ->setModel('Post') + ->setRelations([ + 'belongsTo' => [], + 'belongsToMany' => [], + 'hasOne' => [], + 'hasMany' => [], + ]) + ->setFields([ + 'integer-required' => ['media_id', 'user_id'], + 'string' => ['title', 'body'], + 'json' => ['meta'], + 'timestamp' => ['created_at'], + ]) + ->generate(); + + $this->assertGeneratedFileEquals('migrations.php', 'database/migrations/2022_02_02_000000_posts_create_table.php'); + } + + public function testCreateMigrationMYSQL() + { + putenv('DB_CONNECTION=mysql'); + + Carbon::setTestNow('2022-02-02'); + + $this->mockFilesystem(); + $this->setupConfigurations(); + + app(MigrationGenerator::class) + ->setModel('Post') + ->setRelations([ + 'belongsTo' => [], + 'belongsToMany' => [], + 'hasOne' => [], + 'hasMany' => [], + ]) + ->setFields([ + 'integer-required' => ['media_id', 'user_id'], + 'string' => ['title', 'body'], + 'json' => ['meta'], + 'timestamp' => ['created_at'], + ]) + ->generate(); + + $this->assertGeneratedFileEquals('migrations_mysql.php', 'database/migrations/2022_02_02_000000_posts_create_table.php'); + } +} diff --git a/tests/Support/Migration/MigrationMockTrait.php b/tests/Support/Migration/MigrationMockTrait.php new file mode 100644 index 00000000..05b88f7c --- /dev/null +++ b/tests/Support/Migration/MigrationMockTrait.php @@ -0,0 +1,32 @@ + 'entity-generator::migration', + 'entity-generator.paths' => [ + 'migrations' => 'database/migrations', + ] + ]); + } + + public function mockFilesystem(): void + { + $structure = [ + 'database' => [ + 'migrations' => [], + ], + ]; + + vfsStream::create($structure); + } +} diff --git a/tests/fixtures/MigrationGeneratorTest/migrations.php b/tests/fixtures/MigrationGeneratorTest/migrations.php new file mode 100644 index 00000000..0da0c69b --- /dev/null +++ b/tests/fixtures/MigrationGeneratorTest/migrations.php @@ -0,0 +1,30 @@ +increments('id'); + $table->integer('media_id'); + $table->integer('user_id'); + $table->string('title')->nullable(); + $table->string('body')->nullable(); + $table->jsonb('meta')->default("{}"); + $table->timestamp('created_at')->nullable(); + $table->timestamps(); + }); + } + + public function down(): void + { + Schema::dropIfExists('posts'); + } +}; diff --git a/tests/fixtures/MigrationGeneratorTest/migrations_mysql.php b/tests/fixtures/MigrationGeneratorTest/migrations_mysql.php new file mode 100644 index 00000000..167c7194 --- /dev/null +++ b/tests/fixtures/MigrationGeneratorTest/migrations_mysql.php @@ -0,0 +1,30 @@ +increments('id'); + $table->integer('media_id'); + $table->integer('user_id'); + $table->string('title')->nullable(); + $table->string('body')->nullable(); + $table->json('meta')->nullable(); + $table->timestamp('created_at')->nullable(); + $table->timestamps(); + }); + } + + public function down(): void + { + Schema::dropIfExists('posts'); + } +};