From c1f3a1ecff5315aa01c2a50f6e2f489ad38026d4 Mon Sep 17 00:00:00 2001 From: Konstantin Lapkovsky Date: Wed, 20 Dec 2023 22:20:39 +0400 Subject: [PATCH 1/8] test: add migration generation test. --- .../FakerMethodNotFoundException.php | 9 ++ ...ound.php => UnknownFieldTypeException.php} | 2 +- src/Generators/FactoryGenerator.php | 4 +- src/Generators/MigrationGenerator.php | 7 +- tests/MigrationGeneratorTest.php | 98 +++++++++++++++++++ .../Support/Migration/MigrationMockTrait.php | 32 ++++++ .../MigrationGeneratorTest/migrations.php | 30 ++++++ 7 files changed, 176 insertions(+), 6 deletions(-) create mode 100644 src/Exceptions/FakerMethodNotFoundException.php rename src/Exceptions/{FakerMethodNotFound.php => UnknownFieldTypeException.php} (56%) create mode 100644 tests/MigrationGeneratorTest.php create mode 100644 tests/Support/Migration/MigrationMockTrait.php create mode 100644 tests/fixtures/MigrationGeneratorTest/migrations.php diff --git a/src/Exceptions/FakerMethodNotFoundException.php b/src/Exceptions/FakerMethodNotFoundException.php new file mode 100644 index 00000000..c652e8a3 --- /dev/null +++ b/src/Exceptions/FakerMethodNotFoundException.php @@ -0,0 +1,9 @@ +getNonRequiredLine($fieldName, $typeName); } - throw new Exception('Unknown fieldType in MigrationGenerator'); + throw new UnknownFieldTypeException("Unknown field type {$typeName} in MigrationGenerator."); } } \ No newline at end of file diff --git a/tests/MigrationGeneratorTest.php b/tests/MigrationGeneratorTest.php new file mode 100644 index 00000000..de33a539 --- /dev/null +++ b/tests/MigrationGeneratorTest.php @@ -0,0 +1,98 @@ +generatedFileBasePath = vfsStream::url('root'); + + $this->app->setBasePath($this->generatedFileBasePath); + } + + public function testSetUnknownFieldType() + { + $this->mockViewsNamespace(); + $this->setupConfigurations(); + + $this->expectException(UnknownFieldTypeException::class); + $this->expectErrorMessage('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->mockViewsNamespace(); + $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->rollbackToDefaultBasePath(); + + $this->assertGeneratedFileEquals('migrations.php', 'database/migrations/2022_02_02_000000_posts_create_table.php'); + } + + public function testMethodForMYSQLConnection() + { + putenv('DB_CONNECTION=mysql'); + + $generator = app(MigrationGenerator::class); + + $reflectionClass = new ReflectionClass(MigrationGenerator::class); + $jsonLineMethod = $reflectionClass->getMethod('getJsonLine'); + $requiredLineMethod = $reflectionClass->getMethod('getRequiredLine'); + + $jsonLineMethod->setAccessible(true); + $requiredLineMethod->setAccessible(true); + + $jsonLineResult = $jsonLineMethod->invoke($generator, 'meta'); + $requiredLineResult = $requiredLineMethod->invoke($generator, 'created_at', 'timestamp-required'); + + $this->assertEquals("\$table->json('meta')->nullable();", $jsonLineResult); + $this->assertEquals("\$table->timestamp('created_at')->nullable();", $requiredLineResult); + } +} diff --git a/tests/Support/Migration/MigrationMockTrait.php b/tests/Support/Migration/MigrationMockTrait.php new file mode 100644 index 00000000..73b7b91e --- /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..85fe8dd0 --- /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() + { + Schema::dropIfExists('posts'); + } +}; \ No newline at end of file From b8bd8e3e2f71ebbb97a40a4ede6bba57a6124d3d Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Wed, 20 Nov 2024 14:45:20 +0500 Subject: [PATCH 2/8] chore: add migration generator tests --- tests/MigrationGeneratorTest.php | 6 +----- tests/Support/Migration/MigrationMockTrait.php | 2 +- tests/fixtures/MigrationGeneratorTest/migrations.php | 2 +- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/tests/MigrationGeneratorTest.php b/tests/MigrationGeneratorTest.php index de33a539..8a6e1983 100644 --- a/tests/MigrationGeneratorTest.php +++ b/tests/MigrationGeneratorTest.php @@ -26,11 +26,10 @@ public function setUp(): void public function testSetUnknownFieldType() { - $this->mockViewsNamespace(); $this->setupConfigurations(); $this->expectException(UnknownFieldTypeException::class); - $this->expectErrorMessage('Unknown field type unknown-type in MigrationGenerator.'); + $this->expectExceptionMessage('Unknown field type unknown-type in MigrationGenerator.'); app(MigrationGenerator::class) ->setModel('Post') @@ -52,7 +51,6 @@ public function testCreateMigration() Carbon::setTestNow('2022-02-02'); $this->mockFilesystem(); - $this->mockViewsNamespace(); $this->setupConfigurations(); app(MigrationGenerator::class) @@ -71,8 +69,6 @@ public function testCreateMigration() ]) ->generate(); - $this->rollbackToDefaultBasePath(); - $this->assertGeneratedFileEquals('migrations.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 index 73b7b91e..05b88f7c 100644 --- a/tests/Support/Migration/MigrationMockTrait.php +++ b/tests/Support/Migration/MigrationMockTrait.php @@ -3,7 +3,7 @@ namespace RonasIT\Support\Tests\Support\Migration; use org\bovigo\vfs\vfsStream; -use RonasIT\Support\Tests\Support\Shared\GeneratorMockTrait; +use RonasIT\Support\Tests\Support\GeneratorMockTrait; trait MigrationMockTrait { diff --git a/tests/fixtures/MigrationGeneratorTest/migrations.php b/tests/fixtures/MigrationGeneratorTest/migrations.php index 85fe8dd0..440be135 100644 --- a/tests/fixtures/MigrationGeneratorTest/migrations.php +++ b/tests/fixtures/MigrationGeneratorTest/migrations.php @@ -5,7 +5,7 @@ use Illuminate\Support\Facades\Schema; use RonasIT\Support\Traits\MigrationTrait; -class PostsCreateTable extends Migration +return new class extends Migration { use MigrationTrait; From 1a07224e1573a09edc99e71ce159901b6918409f Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Wed, 20 Nov 2024 14:47:11 +0500 Subject: [PATCH 3/8] chore: add migration generator tests --- tests/MigrationGeneratorTest.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/MigrationGeneratorTest.php b/tests/MigrationGeneratorTest.php index 8a6e1983..7882fc88 100644 --- a/tests/MigrationGeneratorTest.php +++ b/tests/MigrationGeneratorTest.php @@ -3,7 +3,6 @@ namespace RonasIT\Support\Tests; use Illuminate\Support\Carbon; -use org\bovigo\vfs\vfsStream; use ReflectionClass; use RonasIT\Support\Exceptions\UnknownFieldTypeException; use RonasIT\Support\Generators\MigrationGenerator; @@ -13,17 +12,6 @@ class MigrationGeneratorTest extends TestCase { use MigrationMockTrait; - public function setUp(): void - { - parent::setUp(); - - vfsStream::setup(); - - $this->generatedFileBasePath = vfsStream::url('root'); - - $this->app->setBasePath($this->generatedFileBasePath); - } - public function testSetUnknownFieldType() { $this->setupConfigurations(); From 0fd25796b512269dded69cc0d59f213eecec356b Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Wed, 20 Nov 2024 14:48:00 +0500 Subject: [PATCH 4/8] chore: add migration generator tests --- tests/MigrationGeneratorTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/MigrationGeneratorTest.php b/tests/MigrationGeneratorTest.php index 7882fc88..4bcbfc9a 100644 --- a/tests/MigrationGeneratorTest.php +++ b/tests/MigrationGeneratorTest.php @@ -29,7 +29,7 @@ public function testSetUnknownFieldType() ]) ->setFields([ 'integer-required' => ['media_id', 'user_id'], - 'unknown-type' => ['title'] + 'unknown-type' => ['title'], ]) ->generate(); } @@ -53,7 +53,7 @@ public function testCreateMigration() 'integer-required' => ['media_id', 'user_id'], 'string' => ['title', 'body'], 'json' => ['meta'], - 'timestamp' => ['created_at'] + 'timestamp' => ['created_at'], ]) ->generate(); From a57ba08ffeae637b92924b653231d9fd7d784964 Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Wed, 20 Nov 2024 15:01:10 +0500 Subject: [PATCH 5/8] chore: add migration generator tests --- stubs/migration.blade.php | 2 +- tests/fixtures/MigrationGeneratorTest/migrations.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stubs/migration.blade.php b/stubs/migration.blade.php index 50aa88bf..f1f3c2a5 100644 --- a/stubs/migration.blade.php +++ b/stubs/migration.blade.php @@ -71,4 +71,4 @@ public function createTable() }); } @endif -}; \ No newline at end of file +}; diff --git a/tests/fixtures/MigrationGeneratorTest/migrations.php b/tests/fixtures/MigrationGeneratorTest/migrations.php index 440be135..2ba5ac5d 100644 --- a/tests/fixtures/MigrationGeneratorTest/migrations.php +++ b/tests/fixtures/MigrationGeneratorTest/migrations.php @@ -27,4 +27,4 @@ public function down() { Schema::dropIfExists('posts'); } -}; \ No newline at end of file +}; From 4abc19a840adc28d1a802022aa2cf8107b42c632 Mon Sep 17 00:00:00 2001 From: Anton Zabolotnikov Date: Wed, 20 Nov 2024 15:59:11 +0500 Subject: [PATCH 6/8] chore: add migration generator tests --- src/Generators/MigrationGenerator.php | 25 +++++++++++++------------ tests/MigrationGeneratorTest.php | 6 ++++-- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index c020bd9a..5a0b179f 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -3,7 +3,7 @@ namespace RonasIT\Support\Generators; use Carbon\Carbon; -use Illuminate\Support\Arr; +use Illuminate\Support\Str; use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Exceptions\UnknownFieldTypeException; @@ -21,6 +21,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); @@ -28,22 +29,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 Arr::get(explode('-', $typeName), 1) === 'required'; + 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();"; @@ -52,7 +53,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]; @@ -63,27 +64,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); diff --git a/tests/MigrationGeneratorTest.php b/tests/MigrationGeneratorTest.php index 4bcbfc9a..cd1da8c8 100644 --- a/tests/MigrationGeneratorTest.php +++ b/tests/MigrationGeneratorTest.php @@ -64,9 +64,11 @@ public function testMethodForMYSQLConnection() { putenv('DB_CONNECTION=mysql'); - $generator = app(MigrationGenerator::class); + $generatorClassName = MigrationGenerator::class; - $reflectionClass = new ReflectionClass(MigrationGenerator::class); + $generator = app($generatorClassName); + + $reflectionClass = new ReflectionClass($generatorClassName); $jsonLineMethod = $reflectionClass->getMethod('getJsonLine'); $requiredLineMethod = $reflectionClass->getMethod('getRequiredLine'); From 3473212286fc6fc664a5bf53d5d30037ea064a0f Mon Sep 17 00:00:00 2001 From: roman Date: Tue, 17 Dec 2024 17:05:59 +0600 Subject: [PATCH 7/8] refactor: test refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- src/Exceptions/UnknownFieldTypeException.php | 6 +++ src/Generators/MigrationGenerator.php | 2 +- tests/MigrationGeneratorTest.php | 40 +++++++++++-------- .../migrations_mysql.php | 30 ++++++++++++++ 4 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 tests/fixtures/MigrationGeneratorTest/migrations_mysql.php diff --git a/src/Exceptions/UnknownFieldTypeException.php b/src/Exceptions/UnknownFieldTypeException.php index c196a8d4..2f4d3ccc 100644 --- a/src/Exceptions/UnknownFieldTypeException.php +++ b/src/Exceptions/UnknownFieldTypeException.php @@ -6,4 +6,10 @@ class UnknownFieldTypeException extends Exception { + public function __construct( + protected string $typeName, + protected string $generatorName, + ) { + parent::__construct("Unknown field type {$this->typeName} in {$this->generatorName}."); + } } diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index d0ac3af4..64566c49 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -102,6 +102,6 @@ protected function getTableRow(string $fieldName, string $typeName): string return $this->getNonRequiredLine($fieldName, $typeName); } - throw new UnknownFieldTypeException("Unknown field type {$typeName} in MigrationGenerator."); + throw new UnknownFieldTypeException($typeName, 'MigrationGenerator'); } } \ No newline at end of file diff --git a/tests/MigrationGeneratorTest.php b/tests/MigrationGeneratorTest.php index cd1da8c8..49096c71 100644 --- a/tests/MigrationGeneratorTest.php +++ b/tests/MigrationGeneratorTest.php @@ -16,8 +16,10 @@ public function testSetUnknownFieldType() { $this->setupConfigurations(); - $this->expectException(UnknownFieldTypeException::class); - $this->expectExceptionMessage('Unknown field type unknown-type in MigrationGenerator.'); + $this->assertExceptionThrew( + className: UnknownFieldTypeException::class, + message: 'Unknown field type unknown-type in MigrationGenerator.', + ); app(MigrationGenerator::class) ->setModel('Post') @@ -60,25 +62,31 @@ public function testCreateMigration() $this->assertGeneratedFileEquals('migrations.php', 'database/migrations/2022_02_02_000000_posts_create_table.php'); } - public function testMethodForMYSQLConnection() + public function testCreateMigrationMYSQL() { putenv('DB_CONNECTION=mysql'); - $generatorClassName = MigrationGenerator::class; - - $generator = app($generatorClassName); - - $reflectionClass = new ReflectionClass($generatorClassName); - $jsonLineMethod = $reflectionClass->getMethod('getJsonLine'); - $requiredLineMethod = $reflectionClass->getMethod('getRequiredLine'); + Carbon::setTestNow('2022-02-02'); - $jsonLineMethod->setAccessible(true); - $requiredLineMethod->setAccessible(true); + $this->mockFilesystem(); + $this->setupConfigurations(); - $jsonLineResult = $jsonLineMethod->invoke($generator, 'meta'); - $requiredLineResult = $requiredLineMethod->invoke($generator, 'created_at', 'timestamp-required'); + 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->assertEquals("\$table->json('meta')->nullable();", $jsonLineResult); - $this->assertEquals("\$table->timestamp('created_at')->nullable();", $requiredLineResult); + $this->assertGeneratedFileEquals('migrations_mysql.php', 'database/migrations/2022_02_02_000000_posts_create_table.php'); } } 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'); + } +}; From f4eaeada5b5638f197768017418eedf0a448fc8d Mon Sep 17 00:00:00 2001 From: roman Date: Tue, 17 Dec 2024 17:19:43 +0600 Subject: [PATCH 8/8] refactor: remove useless use ReflectionClass refs: https://github.com/RonasIT/laravel-entity-generator/issues/49 --- tests/MigrationGeneratorTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/MigrationGeneratorTest.php b/tests/MigrationGeneratorTest.php index 49096c71..ac272a48 100644 --- a/tests/MigrationGeneratorTest.php +++ b/tests/MigrationGeneratorTest.php @@ -3,7 +3,6 @@ namespace RonasIT\Support\Tests; use Illuminate\Support\Carbon; -use ReflectionClass; use RonasIT\Support\Exceptions\UnknownFieldTypeException; use RonasIT\Support\Generators\MigrationGenerator; use RonasIT\Support\Tests\Support\Migration\MigrationMockTrait;