From fdeb76eae00a3655b09edb3cb086c592e20a89a6 Mon Sep 17 00:00:00 2001 From: Pedro Martins Date: Fri, 23 Apr 2021 11:13:51 +0100 Subject: [PATCH 1/7] Fix relation name infering when MorphOne/MorphMany --- src/Generators/ModelGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index ff15bf05..2ccbae18 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -230,7 +230,7 @@ protected function buildRelationships(Model $model) if ($type === 'morphTo') { $relationship = sprintf('$this->%s()', $type); } elseif ($type === 'morphMany' || $type === 'morphOne') { - $relation = Str::lower(Str::singular($column_name)) . 'able'; + $relation = Str::lower($is_model_fqn ? Str::singular(Str::afterLast($column_name, '\\')) : Str::singular($column_name)) . 'able'; $relationship = sprintf('$this->%s(%s::class, \'%s\')', $type, $fqcn, $relation); } elseif (!is_null($key)) { $relationship = sprintf('$this->%s(%s::class, \'%s\', \'%s\')', $type, $fqcn, $column_name, $key); From 54ccd5d9c9643808ee852442ccbbe3d95adeabcd Mon Sep 17 00:00:00 2001 From: Pedro X Date: Mon, 26 Apr 2021 14:06:58 +0100 Subject: [PATCH 2/7] Revert "Fix relation name infering when MorphOne/MorphMany" This reverts commit fdeb76eae00a3655b09edb3cb086c592e20a89a6. --- src/Generators/ModelGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 2ccbae18..ff15bf05 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -230,7 +230,7 @@ protected function buildRelationships(Model $model) if ($type === 'morphTo') { $relationship = sprintf('$this->%s()', $type); } elseif ($type === 'morphMany' || $type === 'morphOne') { - $relation = Str::lower($is_model_fqn ? Str::singular(Str::afterLast($column_name, '\\')) : Str::singular($column_name)) . 'able'; + $relation = Str::lower(Str::singular($column_name)) . 'able'; $relationship = sprintf('$this->%s(%s::class, \'%s\')', $type, $fqcn, $relation); } elseif (!is_null($key)) { $relationship = sprintf('$this->%s(%s::class, \'%s\', \'%s\')', $type, $fqcn, $column_name, $key); From 1886853c5c8760552f2ef21462d17450c97beec2 Mon Sep 17 00:00:00 2001 From: Pedro X Date: Mon, 26 Apr 2021 14:08:20 +0100 Subject: [PATCH 3/7] morphone, morphmany with fqn generate correct relation string --- src/Generators/ModelGenerator.php | 10 +-- .../Feature/Generators/ModelGeneratorTest.php | 32 ++++++++++ ...odel-relationships-morphone-morphmany.yaml | 6 ++ ...ationships-morphone-morphmany-laravel8.php | 61 +++++++++++++++++++ 4 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 tests/fixtures/drafts/model-relationships-morphone-morphmany.yaml create mode 100644 tests/fixtures/models/model-relationships-morphone-morphmany-laravel8.php diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index ff15bf05..d93f402e 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -202,16 +202,16 @@ protected function buildRelationships(Model $model) if (Str::contains($reference, ':')) { [$foreign_reference, $column_name] = explode(':', $reference); - $method_name = $is_model_fqn ? Str::afterLast($foreign_reference, '\\') : Str::beforeLast($column_name, '_id'); + $method_name = $is_model_fqn ? Str::afterLast(Str::beforeLast($foreign_reference,'.'), '\\') : Str::beforeLast($column_name, '_id'); if (Str::contains($foreign_reference, '.')) { [$class, $key] = explode('.', $foreign_reference); if ($key === 'id') { $key = null; - } else { - $method_name = $is_model_fqn ? Str::lower(Str::afterLast($class, '\\')) : Str::lower($class); } + $method_name = $is_model_fqn ? Str::lower(Str::afterLast($class, '\\')) : Str::lower($class); + } else { $class = $foreign_reference; } @@ -230,7 +230,7 @@ protected function buildRelationships(Model $model) if ($type === 'morphTo') { $relationship = sprintf('$this->%s()', $type); } elseif ($type === 'morphMany' || $type === 'morphOne') { - $relation = Str::lower(Str::singular($column_name)) . 'able'; + $relation = Str::lower($is_model_fqn ? Str::singular(Str::afterLast($column_name, '\\')) : Str::singular($column_name)) . 'able'; $relationship = sprintf('$this->%s(%s::class, \'%s\')', $type, $fqcn, $relation); } elseif (!is_null($key)) { $relationship = sprintf('$this->%s(%s::class, \'%s\', \'%s\')', $type, $fqcn, $column_name, $key); @@ -244,7 +244,7 @@ protected function buildRelationships(Model $model) if ($type === 'morphTo') { $method_name = Str::lower($class_name); } elseif (in_array($type, ['hasMany', 'belongsToMany', 'morphMany'])) { - $method_name = Str::plural($is_model_fqn ? Str::afterLast($column_name, '\\') : $column_name); + $method_name = Str::plural($is_model_fqn ? Str::afterLast($fqcn, '\\') : $column_name); } if (Blueprint::supportsReturnTypeHits()) { diff --git a/tests/Feature/Generators/ModelGeneratorTest.php b/tests/Feature/Generators/ModelGeneratorTest.php index 07f66595..291beb27 100644 --- a/tests/Feature/Generators/ModelGeneratorTest.php +++ b/tests/Feature/Generators/ModelGeneratorTest.php @@ -300,6 +300,38 @@ public function output_generates_relationships_added_with_full_model_namespace() $this->assertEquals(['created' => ['app/Recurrency.php']], $this->subject->output($tree)); } + /** + * @test + * @environment-setup useLaravel8 + */ + public function output_generates_morphone_morphmany_relation_string_when_using_fqn() + { + $this->files->expects('stub') + ->with($this->modelStub) + ->andReturn($this->stub($this->modelStub)); + $this->files->expects('stub') + ->with('model.fillable.stub') + ->andReturn($this->stub('model.fillable.stub')); + $this->files->expects('stub') + ->with('model.casts.stub') + ->andReturn($this->stub('model.casts.stub')); + $this->files->expects('stub') + ->with('model.method.stub') + ->andReturn($this->stub('model.method.stub')); + + $this->files->expects('exists') + ->with('app') + ->andReturnTrue(); + + $this->files->expects('put') + ->with('app/Flag.php', $this->fixture('models/model-relationships-morphone-morphmany-laravel8.php')); + + $tokens = $this->blueprint->parse($this->fixture('drafts/model-relationships-morphone-morphmany.yaml')); + $tree = $this->blueprint->analyze($tokens); + + $this->assertEquals(['created' => ['app/Flag.php']], $this->subject->output($tree)); + } + /** * @test * @environment-setup useLaravel8 diff --git a/tests/fixtures/drafts/model-relationships-morphone-morphmany.yaml b/tests/fixtures/drafts/model-relationships-morphone-morphmany.yaml new file mode 100644 index 00000000..00adad8b --- /dev/null +++ b/tests/fixtures/drafts/model-relationships-morphone-morphmany.yaml @@ -0,0 +1,6 @@ +models: + Flag: + user_id: id + relationships: + morphOne: \Other\Package\Order:stars, some:stars + morphMany: \Other\Package\Duration, \App\MyCustom\Folder\Transaction:line, some:line diff --git a/tests/fixtures/models/model-relationships-morphone-morphmany-laravel8.php b/tests/fixtures/models/model-relationships-morphone-morphmany-laravel8.php new file mode 100644 index 00000000..aa8434c4 --- /dev/null +++ b/tests/fixtures/models/model-relationships-morphone-morphmany-laravel8.php @@ -0,0 +1,61 @@ + 'integer', + 'user_id' => 'integer', + ]; + + + public function order() + { + return $this->morphOne(\Other\Package\Order::class, 'starable'); + } + + public function stars() + { + return $this->morphOne(\App\Some::class, 'starable'); + } + + public function durations() + { + return $this->morphMany(\Other\Package\Duration::class, 'durationable'); + } + + public function transactions() + { + return $this->morphMany(\App\MyCustom\Folder\Transaction::class, 'lineable'); + } + + public function lines() + { + return $this->morphMany(\App\Some::class, 'lineable'); + } + + public function user() + { + return $this->belongsTo(\App\User::class); + } +} From 8a37c2c2fe33fd2835b974e843412ffdbd6b119e Mon Sep 17 00:00:00 2001 From: Pedro X Date: Mon, 26 Apr 2021 14:13:45 +0100 Subject: [PATCH 4/7] style fixes --- src/Generators/ModelGenerator.php | 2 +- tests/Feature/Generators/ModelGeneratorTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index d93f402e..653e5f15 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -202,7 +202,7 @@ protected function buildRelationships(Model $model) if (Str::contains($reference, ':')) { [$foreign_reference, $column_name] = explode(':', $reference); - $method_name = $is_model_fqn ? Str::afterLast(Str::beforeLast($foreign_reference,'.'), '\\') : Str::beforeLast($column_name, '_id'); + $method_name = $is_model_fqn ? Str::afterLast(Str::beforeLast($foreign_reference, '.'), '\\') : Str::beforeLast($column_name, '_id'); if (Str::contains($foreign_reference, '.')) { [$class, $key] = explode('.', $foreign_reference); diff --git a/tests/Feature/Generators/ModelGeneratorTest.php b/tests/Feature/Generators/ModelGeneratorTest.php index 291beb27..39556ac1 100644 --- a/tests/Feature/Generators/ModelGeneratorTest.php +++ b/tests/Feature/Generators/ModelGeneratorTest.php @@ -300,7 +300,7 @@ public function output_generates_relationships_added_with_full_model_namespace() $this->assertEquals(['created' => ['app/Recurrency.php']], $this->subject->output($tree)); } - /** + /** * @test * @environment-setup useLaravel8 */ From 075199c93780ca69e937c1b4f37b4e7060aaa959 Mon Sep 17 00:00:00 2001 From: Pedro X Date: Mon, 26 Apr 2021 14:15:09 +0100 Subject: [PATCH 5/7] more style fixes --- src/Generators/ModelGenerator.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 653e5f15..03e7926c 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -211,7 +211,6 @@ protected function buildRelationships(Model $model) $key = null; } $method_name = $is_model_fqn ? Str::lower(Str::afterLast($class, '\\')) : Str::lower($class); - } else { $class = $foreign_reference; } From fb0665e253150079c81b9820b4ee4a06ce843d8d Mon Sep 17 00:00:00 2001 From: Pedro X Date: Mon, 26 Apr 2021 17:39:40 +0100 Subject: [PATCH 6/7] fix generated method names --- src/Generators/ModelGenerator.php | 4 ++-- .../model-relationships-morphone-morphmany-laravel8.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 03e7926c..e4d3331b 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -202,7 +202,7 @@ protected function buildRelationships(Model $model) if (Str::contains($reference, ':')) { [$foreign_reference, $column_name] = explode(':', $reference); - $method_name = $is_model_fqn ? Str::afterLast(Str::beforeLast($foreign_reference, '.'), '\\') : Str::beforeLast($column_name, '_id'); + $method_name = Str::beforeLast($column_name, '_id'); if (Str::contains($foreign_reference, '.')) { [$class, $key] = explode('.', $foreign_reference); @@ -243,7 +243,7 @@ protected function buildRelationships(Model $model) if ($type === 'morphTo') { $method_name = Str::lower($class_name); } elseif (in_array($type, ['hasMany', 'belongsToMany', 'morphMany'])) { - $method_name = Str::plural($is_model_fqn ? Str::afterLast($fqcn, '\\') : $column_name); + $method_name = Str::plural($is_model_fqn ? Str::afterLast($column_name, '\\') : $column_name); } if (Blueprint::supportsReturnTypeHits()) { diff --git a/tests/fixtures/models/model-relationships-morphone-morphmany-laravel8.php b/tests/fixtures/models/model-relationships-morphone-morphmany-laravel8.php index aa8434c4..236a7ad1 100644 --- a/tests/fixtures/models/model-relationships-morphone-morphmany-laravel8.php +++ b/tests/fixtures/models/model-relationships-morphone-morphmany-laravel8.php @@ -29,7 +29,7 @@ class Flag extends Model ]; - public function order() + public function stars() { return $this->morphOne(\Other\Package\Order::class, 'starable'); } @@ -44,7 +44,7 @@ public function durations() return $this->morphMany(\Other\Package\Duration::class, 'durationable'); } - public function transactions() + public function lines() { return $this->morphMany(\App\MyCustom\Folder\Transaction::class, 'lineable'); } From 4cd3a25ac44569bee1cc8c141ed4bff96d2ff440 Mon Sep 17 00:00:00 2001 From: Pedro X Date: Wed, 28 Apr 2021 13:16:59 +0100 Subject: [PATCH 7/7] enclose test case for described functionality --- tests/Feature/Generators/ModelGeneratorTest.php | 4 ++-- ...del-relationships-morphone-morphmany-with-fqn.yaml} | 4 ++-- ...tionships-morphone-morphmany-with-fqn-laravel8.php} | 10 ---------- 3 files changed, 4 insertions(+), 14 deletions(-) rename tests/fixtures/drafts/{model-relationships-morphone-morphmany.yaml => model-relationships-morphone-morphmany-with-fqn.yaml} (58%) rename tests/fixtures/models/{model-relationships-morphone-morphmany-laravel8.php => model-relationships-morphone-morphmany-with-fqn-laravel8.php} (82%) diff --git a/tests/Feature/Generators/ModelGeneratorTest.php b/tests/Feature/Generators/ModelGeneratorTest.php index 39556ac1..8a506d1e 100644 --- a/tests/Feature/Generators/ModelGeneratorTest.php +++ b/tests/Feature/Generators/ModelGeneratorTest.php @@ -324,9 +324,9 @@ public function output_generates_morphone_morphmany_relation_string_when_using_f ->andReturnTrue(); $this->files->expects('put') - ->with('app/Flag.php', $this->fixture('models/model-relationships-morphone-morphmany-laravel8.php')); + ->with('app/Flag.php', $this->fixture('models/model-relationships-morphone-morphmany-with-fqn-laravel8.php')); - $tokens = $this->blueprint->parse($this->fixture('drafts/model-relationships-morphone-morphmany.yaml')); + $tokens = $this->blueprint->parse($this->fixture('drafts/model-relationships-morphone-morphmany-with-fqn.yaml')); $tree = $this->blueprint->analyze($tokens); $this->assertEquals(['created' => ['app/Flag.php']], $this->subject->output($tree)); diff --git a/tests/fixtures/drafts/model-relationships-morphone-morphmany.yaml b/tests/fixtures/drafts/model-relationships-morphone-morphmany-with-fqn.yaml similarity index 58% rename from tests/fixtures/drafts/model-relationships-morphone-morphmany.yaml rename to tests/fixtures/drafts/model-relationships-morphone-morphmany-with-fqn.yaml index 00adad8b..ba781bf2 100644 --- a/tests/fixtures/drafts/model-relationships-morphone-morphmany.yaml +++ b/tests/fixtures/drafts/model-relationships-morphone-morphmany-with-fqn.yaml @@ -2,5 +2,5 @@ models: Flag: user_id: id relationships: - morphOne: \Other\Package\Order:stars, some:stars - morphMany: \Other\Package\Duration, \App\MyCustom\Folder\Transaction:line, some:line + morphOne: \Other\Package\Order:stars + morphMany: \Other\Package\Duration, \App\MyCustom\Folder\Transaction:line diff --git a/tests/fixtures/models/model-relationships-morphone-morphmany-laravel8.php b/tests/fixtures/models/model-relationships-morphone-morphmany-with-fqn-laravel8.php similarity index 82% rename from tests/fixtures/models/model-relationships-morphone-morphmany-laravel8.php rename to tests/fixtures/models/model-relationships-morphone-morphmany-with-fqn-laravel8.php index 236a7ad1..2c8444eb 100644 --- a/tests/fixtures/models/model-relationships-morphone-morphmany-laravel8.php +++ b/tests/fixtures/models/model-relationships-morphone-morphmany-with-fqn-laravel8.php @@ -34,11 +34,6 @@ public function stars() return $this->morphOne(\Other\Package\Order::class, 'starable'); } - public function stars() - { - return $this->morphOne(\App\Some::class, 'starable'); - } - public function durations() { return $this->morphMany(\Other\Package\Duration::class, 'durationable'); @@ -49,11 +44,6 @@ public function lines() return $this->morphMany(\App\MyCustom\Folder\Transaction::class, 'lineable'); } - public function lines() - { - return $this->morphMany(\App\Some::class, 'lineable'); - } - public function user() { return $this->belongsTo(\App\User::class);