Skip to content

Commit

Permalink
rewrite testGetAverageRating to fix bug and make test better
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Jun 11, 2024
1 parent 1d36f61 commit e1050b1
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions tests/models/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ public function testValidate(): void
}


/**
* @throws Exception
*/
public function testGetRatingDistribution(): void
{
// Create a new product for testing
Expand Down Expand Up @@ -296,30 +299,41 @@ public function testUpdateProduct(): void
$this->assertEquals('Updated description', $updatedProduct->getDescription());
}

/**
* @throws Exception
*/
public function testGetAverageRating(): void
{
// Create a new product for testing
$product = self::createProduct();
// reset database as we don't want previously created reviews from setUp.
self::resetDatabase();

// Create mock review data with different ratings
$reviewsData = [
['rating' => 5],
['rating' => 4],
['rating' => 3],
['rating' => 2],
['rating' => 1],
];
// Insert mock review data into the database
foreach ($reviewsData as $reviewData) {
self::createReview($product, $this->dummy_client, $reviewData['rating'], true);
$this->dummy_product = self::createProduct();
$this->dummy_client = self::createClient();

// Create a random number of verified reviews with different ratings
$verifiedReviewRatings = [];
for ($i = 0; $i < self::$faker->numberBetween(0, 10); $i++) {
$rating = self::$faker->numberBetween(1, 5);
$verifiedReviewRatings[] = $rating;
self::createReview($this->dummy_product, $this->dummy_client, $rating, true);
}

// Note: $this->dummy_client can be a verified reviewer do not write unverified reviews with it

// Create a random number of unverified reviews with different ratings
for ($i = 0; $i < self::$faker->numberBetween(0, 10); $i++) {
$rating = self::$faker->numberBetween(1, 5);
self::createReview($this->dummy_product, self::createClient(), $rating);
}

// Retrieve the average rating for the product
$averageRating = $product->getAverageRating();
$averageRating = $this->dummy_product->getAverageRating();

// Assert that the average rating is accurate
$expectedAverageRating = (5 + 4 + 3 + 2 + 1) / count($reviewsData);

$this->assertEquals($expectedAverageRating, $averageRating);
$expectedAverageRating = count($verifiedReviewRatings) === 0 ? 0 : (float)array_sum(
$verifiedReviewRatings
) / count($verifiedReviewRatings);
$this->assertEqualsWithDelta($expectedAverageRating, $averageRating, 0.0001);
}

public function testGetReviews(): void
Expand Down

0 comments on commit e1050b1

Please sign in to comment.