From 45267e281fd1581f368f3e499448765c6ffcb45f Mon Sep 17 00:00:00 2001 From: Jimmie Johansson Date: Mon, 30 Sep 2024 13:45:22 +0200 Subject: [PATCH 1/3] Check if a model has a certain tag --- src/HasTags.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/HasTags.php b/src/HasTags.php index 4f5b24c..624b27f 100644 --- a/src/HasTags.php +++ b/src/HasTags.php @@ -302,4 +302,13 @@ protected function syncTagIds($ids, string | null $type = null, $detaching = tru $this->tags()->touchIfTouching(); } } + + public function hasTag($tag, string $type = null): bool + { + return $this->tags + ->when($type !== null, fn ($query) => $query->where('type', $type)) + ->contains(function ($modelTag) use ($tag) { + return $modelTag->name === $tag || $modelTag->id === $tag; + }); + } } From f23e8f68b2416d1b56e1cb552d2d46c4ee01f9fe Mon Sep 17 00:00:00 2001 From: Jimmie Johansson Date: Mon, 30 Sep 2024 13:48:50 +0200 Subject: [PATCH 2/3] Check that the hasTag method works. --- tests/HasTagsTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/HasTagsTest.php b/tests/HasTagsTest.php index a01553c..0f0f7fc 100644 --- a/tests/HasTagsTest.php +++ b/tests/HasTagsTest.php @@ -349,3 +349,27 @@ $tagsOfTypeA = $this->testModel->tagsWithType('typeA'); expect($tagsOfTypeA->pluck('name')->toArray())->toEqual(['tagA1']); }); + +it('can check if it has a tag', function () { + $model = TestModel::create(['name' => 'test model']); + $tag = Tag::findOrCreate('test-tag'); + $anotherTag = Tag::findOrCreate('another-tag'); + + $model->attachTag($tag); + + $this->assertTrue($model->hasTag('test-tag')); + $this->assertTrue($model->hasTag($tag->id)); + $this->assertFalse($model->hasTag('non-existing-tag')); + $this->assertFalse($model->hasTag($anotherTag->id)); +}); + +it('can check if it has a tag with type', function () { + $model = TestModel::create(['name' => 'test model']); + $tag = Tag::findOrCreate('test-tag', 'type1'); + $sameNameDifferentType = Tag::findOrCreate('test-tag', 'type2'); + + $model->attachTag($tag); + + $this->assertTrue($model->hasTag('test-tag', 'type1')); + $this->assertFalse($model->hasTag('test-tag', 'type2')); +}); From 5a563b86ff6b5101629b78421b4bebbdf14384df Mon Sep 17 00:00:00 2001 From: Jimmie Johansson Date: Mon, 30 Sep 2024 13:53:01 +0200 Subject: [PATCH 3/3] Change tests to Pest --- tests/HasTagsTest.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/HasTagsTest.php b/tests/HasTagsTest.php index 0f0f7fc..eb010e9 100644 --- a/tests/HasTagsTest.php +++ b/tests/HasTagsTest.php @@ -351,25 +351,23 @@ }); it('can check if it has a tag', function () { - $model = TestModel::create(['name' => 'test model']); $tag = Tag::findOrCreate('test-tag'); $anotherTag = Tag::findOrCreate('another-tag'); - $model->attachTag($tag); + $this->testModel->attachTag($tag); - $this->assertTrue($model->hasTag('test-tag')); - $this->assertTrue($model->hasTag($tag->id)); - $this->assertFalse($model->hasTag('non-existing-tag')); - $this->assertFalse($model->hasTag($anotherTag->id)); + expect($this->testModel->hasTag('test-tag'))->toBeTrue(); + expect($this->testModel->hasTag($tag->id))->toBeTrue(); + expect($this->testModel->hasTag('non-existing-tag'))->toBeFalse(); + expect($this->testModel->hasTag($anotherTag->id))->toBeFalse(); }); it('can check if it has a tag with type', function () { - $model = TestModel::create(['name' => 'test model']); $tag = Tag::findOrCreate('test-tag', 'type1'); $sameNameDifferentType = Tag::findOrCreate('test-tag', 'type2'); - $model->attachTag($tag); + $this->testModel->attachTag($tag); - $this->assertTrue($model->hasTag('test-tag', 'type1')); - $this->assertFalse($model->hasTag('test-tag', 'type2')); + expect($this->testModel->hasTag('test-tag', 'type1'))->toBeTrue(); + expect($this->testModel->hasTag('test-tag', 'type2'))->toBeFalse(); });