Passed
Push — master ( 979f6d...757493 )
by US
08:39 queued 11s
created

PriceFacetSpec   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 62
rs 10
wmc 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace spec\BitBag\SyliusElasticsearchPlugin\Facet;
6
7
use BitBag\SyliusElasticsearchPlugin\Facet\FacetInterface;
8
use BitBag\SyliusElasticsearchPlugin\Facet\PriceFacet;
9
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
10
use Elastica\Aggregation\Histogram;
11
use Elastica\Query\BoolQuery;
12
use Elastica\Query\Range;
13
use PhpSpec\ObjectBehavior;
14
use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatterInterface;
15
use Sylius\Component\Core\Context\ShopperContextInterface;
16
use Sylius\Component\Core\Model\Channel;
17
18
final class PriceFacetSpec extends ObjectBehavior
19
{
20
    private $interval = 1000000;
21
22
    function let(
23
        ConcatedNameResolverInterface $channelPricingNameResolver,
24
        MoneyFormatterInterface $moneyFormatter,
25
        ShopperContextInterface $shopperContext
26
    ): void {
27
        $channel = new Channel();
28
        $channel->setCode('web_us');
29
        $shopperContext->getChannel()->willReturn($channel);
30
        $this->beConstructedWith(
31
            $channelPricingNameResolver,
32
            $moneyFormatter,
33
            $shopperContext,
34
            $this->interval
35
        );
36
    }
37
38
    function it_is_initializable(): void
39
    {
40
        $this->shouldHaveType(PriceFacet::class);
41
    }
42
43
    function it_implements_facet_interface(): void
44
    {
45
        $this->shouldHaveType(FacetInterface::class);
46
    }
47
48
    function it_returns_histogram_aggregation_for_price_field(
49
        ConcatedNameResolverInterface $channelPricingNameResolver
50
    ): void {
51
        $channelPricingNameResolver->resolvePropertyName('web_us')->shouldBeCalled()->willReturn('price_web_us');
52
        $expectedHistogram = new Histogram('price', 'price_web_us', $this->interval);
53
        $expectedHistogram->setMinimumDocumentCount(1);
54
55
        $this->getAggregation()->shouldBeLike($expectedHistogram);
56
    }
57
58
    function it_returns_bool_query_made_of_ranges_based_on_selected_histograms(
59
        ConcatedNameResolverInterface $channelPricingNameResolver
60
    ): void {
61
        $channelPricingNameResolver->resolvePropertyName('web_us')->shouldBeCalled()->willReturn('price_web_us');
62
63
        $selectedHistograms = [1000000, 4000000];
64
        $expectedQuery = new BoolQuery();
65
        $expectedQuery->addShould(new Range('price_web_us', ['gte' => 1000000, 'lte' => 2000000]));
66
        $expectedQuery->addShould(new Range('price_web_us', ['gte' => 4000000, 'lte' => 5000000]));
67
        $this->getQuery($selectedHistograms)->shouldBeLike($expectedQuery);
68
    }
69
70
    function it_returns_money_formatted_bucket_label(
71
        MoneyFormatterInterface $moneyFormatter,
72
        ShopperContextInterface $shopperContext
73
    ): void {
74
        $shopperContext->getCurrencyCode()->willReturn('USD');
75
        $shopperContext->getLocaleCode()->willReturn('en_US');
76
        $moneyFormatter->format(1000000, 'USD', 'en_US')->shouldBeCalled()->willReturn('$10,000.00');
77
        $moneyFormatter->format(2000000, 'USD', 'en_US')->shouldBeCalled()->willReturn('$20,000.00');
78
79
        $this->getBucketLabel(['key' => 1000000, 'doc_count' => 6])->shouldBe('$10,000.00 - $20,000.00 (6)');
80
    }
81
}
82