Passed
Pull Request — master (#69)
by Manuele
04:01
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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 13 1
A it_is_initializable() 0 3 1
A it_implements_facet_interface() 0 3 1
A it_returns_histogram_aggregation_for_price_field() 0 8 1
A it_returns_money_formatted_bucket_label() 0 10 1
A it_returns_bool_query_made_of_ranges_based_on_selected_histograms() 0 10 1
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);
0 ignored issues
show
Bug introduced by
The method willReturn() does not exist on Sylius\Component\Channel\Model\ChannelInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        $shopperContext->getChannel()->/** @scrutinizer ignore-call */ willReturn($channel);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
        $this->beConstructedWith(
31
            $channelPricingNameResolver,
32
            $moneyFormatter,
33
            $shopperContext,
34
            $this->interval
35
        );
36
    }
37
38
    function it_is_initializable(): void
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method getAggregation() does not exist on spec\BitBag\SyliusElasti...in\Facet\PriceFacetSpec. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        $this->/** @scrutinizer ignore-call */ 
56
               getAggregation()->shouldBeLike($expectedHistogram);
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method getQuery() does not exist on spec\BitBag\SyliusElasti...in\Facet\PriceFacetSpec. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        $this->/** @scrutinizer ignore-call */ 
68
               getQuery($selectedHistograms)->shouldBeLike($expectedQuery);
Loading history...
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)');
0 ignored issues
show
Bug introduced by
The method getBucketLabel() does not exist on spec\BitBag\SyliusElasti...in\Facet\PriceFacetSpec. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        $this->/** @scrutinizer ignore-call */ 
80
               getBucketLabel(['key' => 1000000, 'doc_count' => 6])->shouldBe('$10,000.00 - $20,000.00 (6)');
Loading history...
80
    }
81
}
82