Skip to content

Commit db49865

Browse files
committed
feat: add getLatestReceivedReviews
1 parent af61aa6 commit db49865

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/Traits/CanBeReviewed.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Fajarwz\LaravelReview\Models\Review;
66
use Fajarwz\LaravelReview\Models\ReviewSummary;
7+
use Illuminate\Database\Eloquent\Collection;
78
use Illuminate\Database\Eloquent\Model;
89
use Illuminate\Database\Eloquent\Relations\HasMany;
910
use Illuminate\Database\Eloquent\Relations\MorphOne;
@@ -61,6 +62,23 @@ public function getReceivedReview(Model $model, bool $includeUnapproved = false)
6162
return $query->first();
6263
}
6364

65+
/**
66+
* Get the current model latest received reviews of the specified model.
67+
*
68+
* @param \Illuminate\Database\Eloquent\Model|null $model
69+
* @param bool $includeUnapproved
70+
*/
71+
public function getLatestReceivedReviews(?Model $model = null, bool $includeUnapproved = false): Collection
72+
{
73+
$query = $this->receivedReviews($model);
74+
75+
if ($includeUnapproved) {
76+
$query->withUnapproved();
77+
}
78+
79+
return $query->orderByDesc('created_at')->get();
80+
}
81+
6482
/**
6583
* Returns the review summary for this model.
6684
*/

tests/CanBeReviewedTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,42 @@ public function test_getReceivedReview_can_retrieve_unapproved_review_when_inclu
148148
$this->assertEquals(3, $review->rating);
149149
}
150150

151+
public function test_getLatestReceivedReviews_retrieves_latest_approved_reviews_by_default()
152+
{
153+
$newMentee = Mentor::factory()->create();
154+
155+
$newReview = $newMentee->review($this->mentor, 4, 'nice!!!');
156+
157+
$reviews = $this->mentor->getLatestReceivedReviews();
158+
159+
$this->assertCount(3, $reviews);
160+
$this->assertEquals($reviews->first()->id, $newReview->id);
161+
}
162+
163+
public function test_getLatestReceivedReviews_not_returning_unapproved_reviews_by_default()
164+
{
165+
$newMentee = Mentor::factory()->create();
166+
167+
$newReview = $newMentee->review($this->mentor, 4, 'nice!!!', isApproved: false);
168+
169+
$reviews = $this->mentor->getLatestReceivedReviews();
170+
171+
$this->assertCount(2, $reviews);
172+
$this->assertNotEquals($reviews->first()->id, $newReview->id);
173+
}
174+
175+
public function test_getLatestReceivedReviews_can_retrieves_unapproved_reviews_when_includeUnapproved_is_true()
176+
{
177+
$newMentee = Mentor::factory()->create();
178+
179+
$newReview = $newMentee->review($this->mentor, 4, 'nice!!!', isApproved: false);
180+
181+
$reviews = $this->mentor->getLatestReceivedReviews(includeUnapproved: true);
182+
183+
$this->assertCount(3, $reviews);
184+
$this->assertEquals($reviews->first()->id, $newReview->id);
185+
}
186+
151187
public function test_a_reviewable_can_display_its_review_summary()
152188
{
153189
$this->assertTrue($this->mentor->reviewSummary->exists());

0 commit comments

Comments
 (0)