RatingMatrix example

->getResult();

        $reviews = StorefrontSearchResult::createFrom($reviews);

        $this->eventDispatcher->dispatch(new ProductReviewsLoadedEvent($reviews$context$request));

        $reviewResult = ReviewLoaderResult::createFrom($reviews);
        $reviewResult->setProductId($request->get('productId'));
        $reviewResult->setParentId($request->get('parentId'));

        $aggregation = $reviews->getAggregations()->get('ratingMatrix');
        $matrix = new RatingMatrix([]);

        if ($aggregation instanceof TermsResult) {
            $matrix = new RatingMatrix($aggregation->getBuckets());
        }
        $reviewResult->setMatrix($matrix);
        $reviewResult->setCustomerReview($this->getCustomerReview($productId$context));
        $reviewResult->setTotalReviews($matrix->getTotalReviewCount());

        return $reviewResult;
    }

    

class RatingMatrixTest extends TestCase
{
    /** * check that constants are defined as expected * * @group reviews */
    public function testThatConstantsAreSetLikeExpected(): void
    {
        $matrix = new RatingMatrix([]);
        static::assertEquals(5, $matrix->getMaxPoints());
    }

    /** * check that matrix calculates correctly * * @dataProvider getRatings * * @group reviews */
    public function testMatrixCalculation(float $expectedScore, int $reviewCounts, float $total, array $aggregation): void
    {
->getResult()->getEntities()->first();
    }

    /** * @param EntitySearchResult<ProductReviewCollection> $reviews */
    private function getReviewRatingMatrix(EntitySearchResult $reviews): RatingMatrix
    {
        $aggregation = $reviews->getAggregations()->get('ratingMatrix');

        if ($aggregation instanceof TermsResult) {
            return new RatingMatrix($aggregation->getBuckets());
        }

        return new RatingMatrix([]);
    }
}
Home | Imprint | This part of the site doesn't use cookies.