function it_returns_zero_if_given_reviewable_object_has_reviews_but_none_of_them_is_accepted(ArrayCollection $reviews, \Iterator $iterator, ReviewableInterface $reviewable, ReviewInterface $review) { $reviewable->getReviews()->willReturn($reviews)->shouldBeCalled(); $reviews->getIterator()->willReturn($iterator); $iterator->rewind()->shouldBeCalled(); $iterator->valid()->willReturn(true, true, false)->shouldBeCalled(); $iterator->current()->willReturn($review); $review->getStatus()->willReturn(ReviewInterface::STATUS_NEW)->shouldBeCalled(); $iterator->next()->shouldBeCalled(); $this->calculate($reviewable)->shouldReturn(0); }
/** * {@inheritdoc} */ public function calculate(ReviewableInterface $reviewable) { $sum = 0; $reviewsNumber = 0; $reviews = $reviewable->getReviews(); foreach ($reviews as $review) { if (ReviewInterface::STATUS_ACCEPTED === $review->getStatus()) { ++$reviewsNumber; $sum += $review->getRating(); } } return 0 !== $reviewsNumber ? $sum / $reviewsNumber : 0; }
function it_returns_zero_if_given_reviewable_object_has_reviews_but_none_of_them_is_accepted(ReviewableInterface $reviewable, ReviewInterface $review) { $reviewable->getReviews()->willReturn([$review]); $review->getStatus()->willReturn(ReviewInterface::STATUS_NEW); $this->calculate($reviewable)->shouldReturn(0); }