Passed
Push — master ( cc4e78...1262d4 )
by Damian
04:08
created

SearchProductsQueryBuilderSpec::let()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 19
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace spec\BitBag\SyliusElasticsearchPlugin\QueryBuilder;
6
7
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
8
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\SearchPropertyNameResolverRegistryInterface;
9
use BitBag\SyliusElasticsearchPlugin\QueryBuilder\QueryBuilderInterface;
10
use BitBag\SyliusElasticsearchPlugin\QueryBuilder\SearchProductsQueryBuilder;
11
use Elastica\Query\BoolQuery;
12
use Elastica\Query\MultiMatch;
13
use Elastica\Query\Term;
14
use Elastica\Query\Terms;
15
use PhpSpec\ObjectBehavior;
16
use Sylius\Component\Locale\Context\LocaleContextInterface;
17
18
final class SearchProductsQueryBuilderSpec extends ObjectBehavior
19
{
20
    private $isEnabeldQuery;
21
    private $hasChannelQuery;
22
23
    function let(
24
        SearchPropertyNameResolverRegistryInterface $searchPropertyNameResolverRegistry,
25
        LocaleContextInterface $localeContext,
26
        QueryBuilderInterface $isEnabledQueryBuilder,
27
        QueryBuilderInterface $hasChannelQueryBuilder
28
    ): void {
29
        $localeContext->getLocaleCode()->willReturn('en_US');
30
        $searchPropertyNameResolverRegistry->getPropertyNameResolvers()->willReturn([]);
31
        $this->isEnabeldQuery = new Term();
32
        $this->isEnabeldQuery->setTerm('enabled', true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type array|string expected by parameter $value of Elastica\Query\Term::setTerm(). ( Ignorable by Annotation )

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

32
        $this->isEnabeldQuery->setTerm('enabled', /** @scrutinizer ignore-type */ true);
Loading history...
33
        $isEnabledQueryBuilder->buildQuery([])->willReturn($this->isEnabeldQuery);
0 ignored issues
show
Bug introduced by
The method willReturn() does not exist on Elastica\Query\AbstractQuery. ( Ignorable by Annotation )

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

33
        $isEnabledQueryBuilder->buildQuery([])->/** @scrutinizer ignore-call */ willReturn($this->isEnabeldQuery);

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...
34
        $this->hasChannelQuery = new Terms();
35
        $this->hasChannelQuery->setTerms('channels', ['web_us']);
36
        $hasChannelQueryBuilder->buildQuery([])->willReturn($this->hasChannelQuery);
37
        $this->beConstructedWith(
38
            $searchPropertyNameResolverRegistry,
39
            $localeContext,
40
            $isEnabledQueryBuilder,
41
            $hasChannelQueryBuilder
42
        );
43
    }
44
45
    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...
46
    {
47
        $this->shouldHaveType(SearchProductsQueryBuilder::class);
48
    }
49
50
    function it_implements_query_builder_interface(): void
51
    {
52
        $this->shouldHaveType(QueryBuilderInterface::class);
53
    }
54
55
    function it_throws_an_exception_if_query_is_not_present_in_data(): void
56
    {
57
        $this->shouldThrow(\RuntimeException::class)->during('buildQuery', [['not_relevant_key' => 'value']]);
58
    }
59
60
    function it_throws_an_exception_if_query_is_not_a_string(): void
61
    {
62
        $this->shouldThrow(\RuntimeException::class)->during('buildQuery', [['query' => new \stdClass()]]);
63
    }
64
65
    function it_builds_multi_match_query_with_provided_query_string(): void
66
    {
67
        $expectedMultiMatch = new MultiMatch();
68
        $expectedMultiMatch->setQuery('bmw');
69
        $expectedMultiMatch->setFuzziness('AUTO');
70
        $expectedMultiMatch->setFields([]);
71
        $expectedQuery = new BoolQuery();
72
        $expectedQuery->addMust($expectedMultiMatch);
73
        $expectedQuery->addFilter($this->isEnabeldQuery);
74
        $expectedQuery->addFilter($this->hasChannelQuery);
75
76
        $this->buildQuery(['query' => 'bmw'])->shouldBeLike($expectedQuery);
0 ignored issues
show
Bug introduced by
The method buildQuery() does not exist on spec\BitBag\SyliusElasti...roductsQueryBuilderSpec. 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

76
        $this->/** @scrutinizer ignore-call */ 
77
               buildQuery(['query' => 'bmw'])->shouldBeLike($expectedQuery);
Loading history...
77
    }
78
79
    function it_builds_multi_match_query_with_provided_query_string_and_fields_from_registry(
80
        SearchPropertyNameResolverRegistryInterface $searchPropertyNameResolverRegistry,
81
        ConcatedNameResolverInterface $firstPropertyNameResolver,
82
        ConcatedNameResolverInterface $secondPropertyNameResolver
83
    ): void {
84
        $firstPropertyNameResolver->resolvePropertyName('en_US')->shouldBeCalled()->willReturn('property_1_en_us');
85
        $secondPropertyNameResolver->resolvePropertyName('en_US')->shouldBeCalled()->willReturn('property_2_en_us');
86
        $searchPropertyNameResolverRegistry->getPropertyNameResolvers()->willReturn(
87
            [$firstPropertyNameResolver, $secondPropertyNameResolver]
88
        );
89
        $expectedMultiMatch = new MultiMatch();
90
        $expectedMultiMatch->setQuery('bmw');
91
        $expectedMultiMatch->setFuzziness('AUTO');
92
        $expectedMultiMatch->setFields(['property_1_en_us', 'property_2_en_us']);
93
        $expectedQuery = new BoolQuery();
94
        $expectedQuery->addMust($expectedMultiMatch);
95
        $expectedQuery->addFilter($this->isEnabeldQuery);
96
        $expectedQuery->addFilter($this->hasChannelQuery);
97
98
        $this->buildQuery(['query' => 'bmw'])->shouldBeLike($expectedQuery);
99
    }
100
}
101