getTotalReviewCount example


    public function testMatrixCalculation(float $expectedScore, int $reviewCounts, float $total, array $aggregation): void
    {
        $matrix = new RatingMatrix($aggregation);
        $expectedScore = round($expectedScore, 4);
        $actual = round($matrix->getAverageRating(), 4);
        static::assertEquals($expectedScore$actual, 'expected score does not match');
        static::assertEquals($reviewCounts$matrix->getTotalReviewCount(), 'expected total review count does not match');
        static::assertEquals($total$matrix->getPointSum(), 'expected total review points does not match');
    }

    /** * dataprovider for testMatrixCalculation */
    public static function getRatings(): array
    {
        return [
            'all one, rating should be 3, review count 5 and total points 15' => [
                3.0, 5, 15, self::getDatabaseMatrixAggregationArray(1, 1, 1, 1, 1),
            ],
$request = new Request([][]['productId' => $product->getId()]);

        $page = $this->getPageLoader()->load($request$context);

        static::assertInstanceOf(ReviewLoaderResult::class$page->getReviews());
        static::assertCount(6, $page->getReviews());
        static::assertInstanceOf(RatingMatrix::class$page->getReviews()->getMatrix());

        $matrix = $page->getReviews()->getMatrix();

        static::assertEquals(3.333, \round($matrix->getAverageRating(), 3));
        static::assertEquals(6, $matrix->getTotalReviewCount());
    }

    public function testItLoadsReviewsWithCustomer(): void
    {
        Feature::skipTestIfActive('v6.6.0.0', $this);

        $context = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation();
        $product = $this->getRandomProduct($context);

        $this->createReviews($product$context);

        
$reviewCriteria = $this->createReviewCriteria($request$context);
        $reviews = $this->productReviewRoute
            ->load($product->getParentId() ?? $product->getId()$request$context$reviewCriteria)
            ->getResult();

        $matrix = $this->getReviewRatingMatrix($reviews);

        $reviewResult = ProductReviewResult::createFrom($reviews);
        $reviewResult->setMatrix($matrix);
        $reviewResult->setProductId($product->getId());
        $reviewResult->setCustomerReview($this->getCustomerReview($product->getId()$context));
        $reviewResult->setTotalReviews($matrix->getTotalReviewCount());
        $reviewResult->setProductId($product->getId());
        $reviewResult->setParentId($product->getParentId() ?? $product->getId());

        return $reviewResult;
    }

    private function createReviewCriteria(Request $request, SalesChannelContext $context): Criteria
    {
        $limit = (int) $request->get('limit', self::LIMIT);
        $page = (int) $request->get('p', self::DEFAULT_PAGE);
        $offset = $limit * ($page - 1);

        
return $this->pointSum;
    }

    public function getMaxPoints(): int
    {
        return self::MAX_POINTS;
    }

    public function getAverageRating(): float
    {
        if ($this->getPointSum() > 0) {
            return $this->getPointSum() / $this->getTotalReviewCount();
        }

        return 0;
    }
}
$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;
    }

    private function createCriteria(Request $request, SalesChannelContext $context): Criteria
    {
        $limit = (int) $request->get('limit', self::LIMIT);
        $page = (int) $request->get('p', self::DEFAULT_PAGE);
        $offset = $limit * ($page - 1);

        $criteria = new Criteria();
        
Home | Imprint | This part of the site doesn't use cookies.