From 2373fe0294b0f61a41d93ee10e7d88a962f65fa2 Mon Sep 17 00:00:00 2001 From: Max Schwanekamp Date: Wed, 10 Jan 2024 17:06:54 -0800 Subject: [PATCH 1/3] Fix #142 - Use $model->getMorphClass() instead of get_class($class) in TagService::renameTags() --- src/Services/TagService.php | 2 +- tests/TagServiceTests.php | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Services/TagService.php b/src/Services/TagService.php index b96a7e0..91bf6e2 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -354,7 +354,7 @@ public function renameTags(string $oldName, string $newName, $class = null): int return $pivot ->where($relatedKeyName, $oldTag->getKey()) - ->where($relatedMorphType, get_class($class)) + ->where($relatedMorphType, $class->getMorphClass()) ->update([ $relatedKeyName => $newTag->getKey(), ]); diff --git a/tests/TagServiceTests.php b/tests/TagServiceTests.php index 9dbdd30..71d62e9 100644 --- a/tests/TagServiceTests.php +++ b/tests/TagServiceTests.php @@ -313,6 +313,32 @@ public function testRenamingTag(): void ); } + /** + * Test renaming a tag. + */ + public function testRenamingTagWithCustomMorphClass(): void + { + // Create a model with a custom morph class key + $morphModel = new class extends TestModel { + protected $attributes = [ + 'title' => 'testing morph model' + ]; + public function getMorphClass() + { + return 'test-morph-model'; // can any custom key that is different from the class name + } + }; + $morphModel->save(); + + $morphModel->tag('Apple'); + $morphModel->tag('Banana'); + $morphModel->tag('Cherry'); + + // Rename the tags just for one model class + $count = $this->service->renameTags('Apple', 'Apricot', $morphModel); + $this->assertEquals(1, $count); + } + /** * Test renaming a tag across all models. */ From 459d0a2093ab9bb7fc543f93e7f037e0539738f9 Mon Sep 17 00:00:00 2001 From: Max Schwanekamp Date: Wed, 10 Jan 2024 17:17:36 -0800 Subject: [PATCH 2/3] Update test method docblock --- tests/TagServiceTests.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TagServiceTests.php b/tests/TagServiceTests.php index 71d62e9..6c74b13 100644 --- a/tests/TagServiceTests.php +++ b/tests/TagServiceTests.php @@ -314,7 +314,7 @@ public function testRenamingTag(): void } /** - * Test renaming a tag. + * Test renaming a tag with a custom morph class key. */ public function testRenamingTagWithCustomMorphClass(): void { From 367981942650873750b0d23f979b9ee8ad019737 Mon Sep 17 00:00:00 2001 From: Max Schwanekamp Date: Wed, 10 Jan 2024 17:19:23 -0800 Subject: [PATCH 3/3] Update another comment --- tests/TagServiceTests.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TagServiceTests.php b/tests/TagServiceTests.php index 6c74b13..50fa2cd 100644 --- a/tests/TagServiceTests.php +++ b/tests/TagServiceTests.php @@ -334,7 +334,7 @@ public function getMorphClass() $morphModel->tag('Banana'); $morphModel->tag('Cherry'); - // Rename the tags just for one model class + // Rename the tags the morphModel class should have exactly 1 update $count = $this->service->renameTags('Apple', 'Apricot', $morphModel); $this->assertEquals(1, $count); }