From 23240b4f8e9e2fc4a0d41c7f02bd657f4c85f273 Mon Sep 17 00:00:00 2001 From: Chris Mellor Date: Sun, 10 Nov 2024 18:31:40 +0000 Subject: [PATCH] refactor: add new method for better accessibility - replace code with new method in test - update docs --- README.md | 6 +----- src/Concerns/HasAchievements.php | 6 ++++++ tests/Concerns/HasAchievementsTest.php | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8265ec4..ce52ff2 100644 --- a/README.md +++ b/README.md @@ -436,11 +436,7 @@ $user->achievementsWithProgress()->get(); Check Achievements that have a certain amount of progression: ```php -$user->achievements - ->first() - ->pivot() - ->withProgress(25) - ->get(); +$user->achievementsWithSpecificProgress(25)->get(); ``` ### Increase Achievement Progression diff --git a/src/Concerns/HasAchievements.php b/src/Concerns/HasAchievements.php index ab32484..84ef520 100644 --- a/src/Concerns/HasAchievements.php +++ b/src/Concerns/HasAchievements.php @@ -71,6 +71,12 @@ public function achievementsWithProgress(): BelongsToMany ->wherePivotNotNull(column: 'progress'); } + public function achievementsWithSpecificProgress(int $progress): BelongsToMany + { + return $this->achievements() + ->wherePivot(column: 'progress', operator: '>=', value: $progress); + } + public function secretAchievements(): BelongsToMany { return $this->belongsToMany(related: config(key: 'level-up.models.achievement')) diff --git a/tests/Concerns/HasAchievementsTest.php b/tests/Concerns/HasAchievementsTest.php index 88aa1ba..2ba9ebc 100644 --- a/tests/Concerns/HasAchievementsTest.php +++ b/tests/Concerns/HasAchievementsTest.php @@ -80,7 +80,7 @@ $this->user->grantAchievement($this->achievement, 50); $this->user->grantAchievement(Achievement::factory()->create(), 50); - expect($this->user)->getUserAchievements()->first()->pivot->withProgress(50)->toHaveCount(2); + expect($this->user->achievementsWithSpecificProgress(50)->get())->toHaveCount(count: 2); }); it(description: 'can increment the progress of an Achievement', closure: function (): void {