Skip to content

Commit 8d62ee0

Browse files
committed
#110876 add tests
1 parent eff61d1 commit 8d62ee0

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

src/Concerns/DecoratesBoolQuery.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Ensi\LaravelElasticQuery\Concerns;
44

55
use Closure;
6+
use Ensi\LaravelElasticQuery\Contracts\BoolQuery;
67
use Ensi\LaravelElasticQuery\Contracts\MatchOptions;
78
use Ensi\LaravelElasticQuery\Contracts\MultiMatchOptions;
89
use Ensi\LaravelElasticQuery\Contracts\WildcardOptions;
@@ -107,7 +108,7 @@ public function orWhereWildcard(string $field, string $query, ?WildcardOptions $
107108
return $this;
108109
}
109110

110-
public function addMustBool(): BoolQueryBuilder
111+
public function addMustBool(): BoolQuery
111112
{
112113
return $this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
113114
}

src/Contracts/BoolQuery.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,13 @@ public function whereNotNull(string $field): static;
2525

2626
public function whereMatch(string $field, string $query, string|MatchOptions $operator = 'or'): static;
2727

28+
public function orWhereMatch(string $field, string $query, string|MatchOptions $operator = 'or'): static;
29+
2830
public function whereMultiMatch(array $fields, string $query, string|MultiMatchOptions|null $type = null): static;
31+
32+
public function whereWildcard(string $field, string $query, ?WildcardOptions $options = null): static;
33+
34+
public function orWhereWildcard(string $field, string $query, ?WildcardOptions $options = null): static;
35+
36+
public function addMustBool(): BoolQuery;
2937
}

src/Filtering/BoolQueryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public function makeWildcard(string $field, string $query, ?WildcardOptions $opt
202202
return new Wildcard($this->absolutePath($field), $query, $options ?: new WildcardOptions());
203203
}
204204

205-
public function addMustBool(): static
205+
public function addMustBool(): BoolQueryBuilder
206206
{
207207
$boolCriteria = static::make();
208208
$this->must->add($boolCriteria);

tests/Unit/Filtering/BoolQueryTest.php

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function testMatch(string|MatchOptions $options, array $expected): void
141141
{
142142
$dsl = BoolQueryBuilder::make()->whereMatch('name', 'foo', $options)->toDSL();
143143

144-
$this->assertArrayFragment(array_merge(['query' => 'foo'], $expected), $dsl);
144+
$this->assertArrayFragment(['must' => [["match" => ['name' => array_merge(['query' => 'foo'], $expected)]]]], $dsl);
145145
}
146146

147147
public function provideMatch(): array
@@ -160,6 +160,32 @@ public function provideMatch(): array
160160
];
161161
}
162162

163+
/**
164+
* @dataProvider provideOrMatch
165+
*/
166+
public function testOrMatch(string|MatchOptions $options, array $expected): void
167+
{
168+
$dsl = BoolQueryBuilder::make()->orWhereMatch('name', 'foo', $options)->toDSL();
169+
170+
$this->assertArrayFragment(['should' => [["match" => ['name' => array_merge(['query' => 'foo'], $expected)]]]], $dsl);
171+
}
172+
173+
public function provideOrMatch(): array
174+
{
175+
return [
176+
'operator' => ['and', ['operator' => 'and']],
177+
'fuzziness' => [MatchOptions::make(fuzziness: 'AUTO'), ['fuzziness' => 'AUTO']],
178+
'minimum_should_match' => [
179+
MatchOptions::make(minimumShouldMatch: '50%'),
180+
['minimum_should_match' => '50%'],
181+
],
182+
'many options' => [
183+
MatchOptions::make(operator: 'or', fuzziness: '2', minimumShouldMatch: '30%'),
184+
['minimum_should_match' => '30%', 'fuzziness' => '2', 'operator' => 'or'],
185+
],
186+
];
187+
}
188+
163189
/**
164190
* @dataProvider provideMultiMatch
165191
*/
@@ -190,7 +216,7 @@ public function testWildcard(?WildcardOptions $options, array $expected): void
190216
{
191217
$dsl = BoolQueryBuilder::make()->whereWildcard('foo', '%value%', $options)->toDSL();
192218

193-
$this->assertArrayFragment(['wildcard' => ['foo' => array_merge(['value' => '%value%'], $expected)]], $dsl);
219+
$this->assertArrayFragment(['must' => [['wildcard' => ['foo' => array_merge(['value' => '%value%'], $expected)]]]], $dsl);
194220
}
195221

196222
public function provideWildcard(): array
@@ -201,4 +227,40 @@ public function provideWildcard(): array
201227
'rewrite options' => [WildcardOptions::make(rewrite: 'constant_score'), ['rewrite' => 'constant_score']],
202228
];
203229
}
230+
231+
/**
232+
* @dataProvider provideOrWildcard
233+
*/
234+
public function testOrWildcard(?WildcardOptions $options, array $expected): void
235+
{
236+
$dsl = BoolQueryBuilder::make()->orWhereWildcard('foo', '%value%', $options)->toDSL();
237+
238+
$this->assertArrayFragment(['should' => [['wildcard' => ['foo' => array_merge(['value' => '%value%'], $expected)]]]], $dsl);
239+
}
240+
241+
public function provideOrWildcard(): array
242+
{
243+
return [
244+
'empty options' => [WildcardOptions::make(0, false), ['boost' => 0, 'case_insensitive' => false]],
245+
'full options' => [WildcardOptions::make(0.5, true), ['boost' => 0.5, 'case_insensitive' => true]],
246+
'rewrite options' => [WildcardOptions::make(rewrite: 'constant_score'), ['rewrite' => 'constant_score']],
247+
];
248+
}
249+
250+
public function testAddMustBool(): void
251+
{
252+
$builder = BoolQueryBuilder::make();
253+
$builder->where('mustName', 'value');
254+
$builder->addMustBool()->orWhereWildcard('wildcardName', 'wildcardValue')->orWhereMatch('matchName', 'matchValue');
255+
256+
$dsl = $builder->toDSL();
257+
258+
$this->assertArrayFragment([
259+
'filter' => [["term" => ['mustName' => 'value']]],
260+
'must' => [["bool" => ['should' => [
261+
['wildcard' => ['wildcardName' => ['value' => 'wildcardValue']]],
262+
["match" => ['matchName' => ['query' => 'matchValue', 'operator' => 'or']]],
263+
]]]],
264+
], $dsl);
265+
}
204266
}

0 commit comments

Comments
 (0)