|
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); |
|
|
|
|
|
|
33
|
|
|
$isEnabledQueryBuilder->buildQuery([])->willReturn($this->isEnabeldQuery); |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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
|
|
|
|