From a9adfdcf35be40365441672bbf6a3edf964b2041 Mon Sep 17 00:00:00 2001 From: Gianluca Bine Date: Fri, 16 Oct 2020 22:12:39 -0300 Subject: [PATCH] fix: pivot table naming when model name contains path prefix --- src/Models/Model.php | 4 +- .../Generators/MigrationGeneratorTest.php | 57 +++++++++++++++++++ tests/fixtures/drafts/with-path-prefix.yaml | 6 ++ ...prefix-table-name-city-region-laravel6.php | 31 ++++++++++ ...ith-path-prefix-table-name-city-region.php | 31 ++++++++++ ...path-prefix-table-name-region-laravel6.php | 33 +++++++++++ .../with-path-prefix-table-name-region.php | 33 +++++++++++ 7 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/drafts/with-path-prefix.yaml create mode 100644 tests/fixtures/migrations/with-path-prefix-table-name-city-region-laravel6.php create mode 100644 tests/fixtures/migrations/with-path-prefix-table-name-city-region.php create mode 100644 tests/fixtures/migrations/with-path-prefix-table-name-region-laravel6.php create mode 100644 tests/fixtures/migrations/with-path-prefix-table-name-region.php diff --git a/src/Models/Model.php b/src/Models/Model.php index 36936289..9d99832e 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -155,7 +155,7 @@ public function addRelationship(string $type, string $reference) public function addPivotTable(string $reference) { - $segments = [$this->name(), $reference]; + $segments = [$this->name(), class_basename($reference)]; sort($segments); $this->pivotTables[] = $segments; } @@ -164,7 +164,7 @@ public function indexes(): array { return $this->indexes; } - + public function addIndex(Index $index) { $this->indexes[] = $index; diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index 88cb68d8..fb7a0645 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -550,6 +550,63 @@ public function output_also_creates_pivot_table_migration_with_custom_name_larav $this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree)); } + /** + * @test + */ + public function output_creates_pivot_table_migration_correctly_when_model_name_contains_path_prefix() + { + $this->files->expects('stub') + ->with('migration.stub') + ->andReturn($this->stub('migration.stub')); + + $now = Carbon::now(); + Carbon::setTestNow($now); + + $model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_regions_table.php'); + $pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_city_region_table.php'); + + $this->files->expects('exists')->twice()->andReturn(false); + + $this->files->expects('put') + ->with($model_migration, $this->fixture('migrations/with-path-prefix-table-name-region.php')); + $this->files->expects('put') + ->with($pivot_migration, $this->fixture('migrations/with-path-prefix-table-name-city-region.php')); + + $tokens = $this->blueprint->parse($this->fixture('drafts/with-path-prefix.yaml')); + $tree = $this->blueprint->analyze($tokens); + + $this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree)); + } + + /** + * @test + * @environment-setup useLaravel6 + */ + public function output_creates_pivot_table_migration_correctly_when_model_name_contains_path_prefix_laravel6() + { + $this->files->expects('stub') + ->with('migration.stub') + ->andReturn($this->stub('migration.stub')); + + $now = Carbon::now(); + Carbon::setTestNow($now); + + $model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_regions_table.php'); + $pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_city_region_table.php'); + + $this->files->expects('exists')->twice()->andReturn(false); + + $this->files->expects('put') + ->with($model_migration, $this->fixture('migrations/with-path-prefix-table-name-region-laravel6.php')); + $this->files->expects('put') + ->with($pivot_migration, $this->fixture('migrations/with-path-prefix-table-name-city-region-laravel6.php')); + + $tokens = $this->blueprint->parse($this->fixture('drafts/with-path-prefix.yaml')); + $tree = $this->blueprint->analyze($tokens); + + $this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree)); + } + /** * @test */ diff --git a/tests/fixtures/drafts/with-path-prefix.yaml b/tests/fixtures/drafts/with-path-prefix.yaml new file mode 100644 index 00000000..d5be0896 --- /dev/null +++ b/tests/fixtures/drafts/with-path-prefix.yaml @@ -0,0 +1,6 @@ +models: + Lookup\Region: + name_en: string:255 nullable + softDeletes + relationships: + belongsToMany: Lookup\City diff --git a/tests/fixtures/migrations/with-path-prefix-table-name-city-region-laravel6.php b/tests/fixtures/migrations/with-path-prefix-table-name-city-region-laravel6.php new file mode 100644 index 00000000..a3ece17c --- /dev/null +++ b/tests/fixtures/migrations/with-path-prefix-table-name-city-region-laravel6.php @@ -0,0 +1,31 @@ +unsignedBigInteger('city_id'); + $table->unsignedBigInteger('region_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('city_region'); + } +} diff --git a/tests/fixtures/migrations/with-path-prefix-table-name-city-region.php b/tests/fixtures/migrations/with-path-prefix-table-name-city-region.php new file mode 100644 index 00000000..148b0185 --- /dev/null +++ b/tests/fixtures/migrations/with-path-prefix-table-name-city-region.php @@ -0,0 +1,31 @@ +foreignId('city_id'); + $table->foreignId('region_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('city_region'); + } +} diff --git a/tests/fixtures/migrations/with-path-prefix-table-name-region-laravel6.php b/tests/fixtures/migrations/with-path-prefix-table-name-region-laravel6.php new file mode 100644 index 00000000..6e70b3ee --- /dev/null +++ b/tests/fixtures/migrations/with-path-prefix-table-name-region-laravel6.php @@ -0,0 +1,33 @@ +bigIncrements('id'); + $table->string('name_en', 255)->nullable(); + $table->softDeletes(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('regions'); + } +} diff --git a/tests/fixtures/migrations/with-path-prefix-table-name-region.php b/tests/fixtures/migrations/with-path-prefix-table-name-region.php new file mode 100644 index 00000000..b326d8bd --- /dev/null +++ b/tests/fixtures/migrations/with-path-prefix-table-name-region.php @@ -0,0 +1,33 @@ +id(); + $table->string('name_en', 255)->nullable(); + $table->softDeletes(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('regions'); + } +}