Completed
Push — master ( e67539...af5ca5 )
by Kamil
120:57 queued 90:47
created

AverageRatingCalculatorSpec   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 40
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A it_implements_average_price_calculator_interface() 0 4 1
A it_calculates_average_price() 0 15 1
A it_returns_zero_if_given_reviewable_object_has_no_reviews() 0 6 1
A it_returns_zero_if_given_reviewable_object_has_reviews_but_none_of_them_is_accepted() 0 9 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace spec\Sylius\Component\Review\Calculator;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use PhpSpec\ObjectBehavior;
18
use Sylius\Component\Review\Calculator\ReviewableRatingCalculatorInterface;
19
use Sylius\Component\Review\Model\ReviewableInterface;
20
use Sylius\Component\Review\Model\ReviewInterface;
21
22
final class AverageRatingCalculatorSpec extends ObjectBehavior
23
{
24
    function it_implements_average_price_calculator_interface(): void
25
    {
26
        $this->shouldImplement(ReviewableRatingCalculatorInterface::class);
27
    }
28
29
    function it_calculates_average_price(
30
        ReviewableInterface $reviewable,
31
        ReviewInterface $review1,
32
        ReviewInterface $review2
33
    ): void {
34
        $reviewable->getReviews()->willReturn(new ArrayCollection([$review1->getWrappedObject(), $review2->getWrappedObject()]));
35
36
        $review1->getStatus()->willReturn(ReviewInterface::STATUS_ACCEPTED);
37
        $review2->getStatus()->willReturn(ReviewInterface::STATUS_ACCEPTED);
38
39
        $review1->getRating()->willReturn(4);
40
        $review2->getRating()->willReturn(5);
41
42
        $this->calculate($reviewable)->shouldReturn(4.5);
43
    }
44
45
    function it_returns_zero_if_given_reviewable_object_has_no_reviews(ReviewableInterface $reviewable): void
46
    {
47
        $reviewable->getReviews()->willReturn(new ArrayCollection([]))->shouldBeCalled();
48
49
        $this->calculate($reviewable)->shouldReturn(0.0);
50
    }
51
52
    function it_returns_zero_if_given_reviewable_object_has_reviews_but_none_of_them_is_accepted(
53
        ReviewableInterface $reviewable,
54
        ReviewInterface $review
55
    ): void {
56
        $reviewable->getReviews()->willReturn(new ArrayCollection([$review->getWrappedObject()]));
57
        $review->getStatus()->willReturn(ReviewInterface::STATUS_NEW);
58
59
        $this->calculate($reviewable)->shouldReturn(0.0);
60
    }
61
}
62