Passed
Pull Request — master (#69)
by Manuele
04:16
created

it_is_initializable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace spec\BitBag\SyliusElasticsearchPlugin\QueryBuilder;
5
6
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
7
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\SearchPropertyNameResolverRegistryInterface;
8
use BitBag\SyliusElasticsearchPlugin\QueryBuilder\QueryBuilderInterface;
9
use BitBag\SyliusElasticsearchPlugin\QueryBuilder\SearchProductsQueryBuilder;
10
use Elastica\Query\MultiMatch;
11
use PhpSpec\ObjectBehavior;
12
use Sylius\Component\Locale\Context\LocaleContextInterface;
13
14
final class SearchProductsQueryBuilderSpec extends ObjectBehavior
15
{
16
    function let(
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...
17
        SearchPropertyNameResolverRegistryInterface $searchPropertyNameResolverRegistry,
18
        LocaleContextInterface $localeContext
19
    ) {
20
        $localeContext->getLocaleCode()->willReturn('en_US');
21
        $searchPropertyNameResolverRegistry->getPropertyNameResolvers()->willReturn([]);
22
        $this->beConstructedWith($searchPropertyNameResolverRegistry, $localeContext);
23
    }
24
25
    function it_is_initializable()
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...
26
    {
27
        $this->shouldHaveType(SearchProductsQueryBuilder::class);
28
    }
29
30
    function it_implements_query_builder_interface()
31
    {
32
        $this->shouldHaveType(QueryBuilderInterface::class);
33
    }
34
35
    function it_throws_an_exception_if_query_is_not_present_in_data()
36
    {
37
        $this->shouldThrow(\RuntimeException::class)->during('buildQuery', [['not_relevant_key' => 'value']]);
38
    }
39
40
    function it_throws_an_exception_if_query_is_not_a_string()
41
    {
42
        $this->shouldThrow(\RuntimeException::class)->during('buildQuery', [['query' => new \stdClass()]]);
43
    }
44
45
    function it_builds_multi_match_query_with_provided_query_string()
46
    {
47
        $expectedQuery = new MultiMatch();
48
        $expectedQuery->setQuery('bmw');
49
        $expectedQuery->setFuzziness('AUTO');
50
        $expectedQuery->setFields([]);
51
52
        $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

52
        $this->/** @scrutinizer ignore-call */ 
53
               buildQuery(['query' => 'bmw'])->shouldBeLike($expectedQuery);
Loading history...
53
    }
54
55
    function it_builds_multi_match_query_with_provided_query_string_and_fields_from_registry(
56
        SearchPropertyNameResolverRegistryInterface $searchPropertyNameResolverRegistry,
57
        ConcatedNameResolverInterface $firstPropertyNameResolver,
58
        ConcatedNameResolverInterface $secondPropertyNameResolver
59
    ) {
60
        $firstPropertyNameResolver->resolvePropertyName('en_US')->shouldBeCalled()->willReturn('property_1_en_us');
61
        $secondPropertyNameResolver->resolvePropertyName('en_US')->shouldBeCalled()->willReturn('property_2_en_us');
62
        $searchPropertyNameResolverRegistry->getPropertyNameResolvers()->willReturn(
63
            [$firstPropertyNameResolver, $secondPropertyNameResolver]
64
        );
65
        $expectedQuery = new MultiMatch();
66
        $expectedQuery->setQuery('bmw');
67
        $expectedQuery->setFuzziness('AUTO');
68
        $expectedQuery->setFields(['property_1_en_us', 'property_2_en_us']);
69
70
        $this->buildQuery(['query' => 'bmw'])->shouldBeLike($expectedQuery);
71
    }
72
}
73