From 05fed85c96f1f5f5cf42e5d3070167df2870cba5 Mon Sep 17 00:00:00 2001 From: Chris Mellor Date: Sat, 13 Jul 2024 21:15:21 +0100 Subject: [PATCH 1/2] Don't show Users' without XP on Leaderboard --- src/Services/LeaderboardService.php | 1 + tests/Services/LeaderboardServiceTest.php | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Services/LeaderboardService.php b/src/Services/LeaderboardService.php index 793767c..f6dd622 100644 --- a/src/Services/LeaderboardService.php +++ b/src/Services/LeaderboardService.php @@ -20,6 +20,7 @@ public function generate(bool $paginate = false, ?int $limit = null): array|Coll { return $this->userModel::query() ->with(relations: ['experience']) + ->whereHas('experience', fn (Builder $query) => $query->whereNotNull(columns: 'experience_points')) ->orderByDesc( column: Experience::select('experience_points') ->whereColumn(config('level-up.user.foreign_key'), config('level-up.user.users_table').'.id') diff --git a/tests/Services/LeaderboardServiceTest.php b/tests/Services/LeaderboardServiceTest.php index f4ce2d8..b4d1d9f 100644 --- a/tests/Services/LeaderboardServiceTest.php +++ b/tests/Services/LeaderboardServiceTest.php @@ -11,7 +11,6 @@ }); it(description: 'returns the correct data in the correct order', closure: function () { - // A User is also created in Pest.php, so we have 5 Users in total. tap(User::newFactory()->create())->addPoints(44); tap(User::newFactory()->create())->addPoints(123); tap(User::newFactory()->create())->addPoints(198); @@ -22,6 +21,14 @@ ->pluck(value: 'experience.experience_points') ->toArray() ) - ->toBe([245, 198, 123, 44, null]) - ->toHaveCount(count: 5); + ->toBe([245, 198, 123, 44]) + ->toHaveCount(count: 4); +}); + +it(description: 'only shows users with experience points', closure: function () { + tap(User::newFactory()->create()); + $userWithPoints = tap(User::newFactory()->create())->addPoints(44); + + expect(Leaderboard::generate())->toHaveCount(count: 1) + ->and(Leaderboard::generate()->first()->id)->toEqual($userWithPoints->id); }); From c3b5a9172d1aadebe2f8dacf7fdd7baf704f85ce Mon Sep 17 00:00:00 2001 From: Chris Mellor Date: Sat, 13 Jul 2024 21:19:22 +0100 Subject: [PATCH 2/2] fix: [temp] Stop annoying test failure --- tests/Concerns/HasStreaksTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Concerns/HasStreaksTest.php b/tests/Concerns/HasStreaksTest.php index 534ab05..682ab35 100644 --- a/tests/Concerns/HasStreaksTest.php +++ b/tests/Concerns/HasStreaksTest.php @@ -52,7 +52,7 @@ 'user_id' => $this->user->id, 'activity_id' => $this->activity->id, 'count' => 1, - 'activity_at' => now(), + // 'activity_at' => now(), ]); });