|
3 | 3 | namespace Ensi\LaravelElasticQuery\Tests\Unit\Filtering; |
4 | 4 |
|
5 | 5 | use Ensi\LaravelElasticQuery\Contracts\BoolQuery; |
| 6 | +use Ensi\LaravelElasticQuery\Contracts\MatchOptions; |
| 7 | +use Ensi\LaravelElasticQuery\Contracts\MatchType; |
| 8 | +use Ensi\LaravelElasticQuery\Contracts\MultiMatchOptions; |
6 | 9 | use Ensi\LaravelElasticQuery\Filtering\BoolQueryBuilder; |
7 | 10 | use Ensi\LaravelElasticQuery\Tests\AssertsArray; |
8 | 11 | use Ensi\LaravelElasticQuery\Tests\Unit\UnitTestCase; |
@@ -129,4 +132,53 @@ public function provideWhereOperators(): array |
129 | 132 | '<=' => ['<=', ['range' => ['rating' => ['lte' => 5]]]], |
130 | 133 | ]; |
131 | 134 | } |
| 135 | + |
| 136 | + /** |
| 137 | + * @dataProvider provideMatch |
| 138 | + */ |
| 139 | + public function testMatch(string|MatchOptions $options, array $expected): void |
| 140 | + { |
| 141 | + $dsl = BoolQueryBuilder::make()->whereMatch('name', 'foo', $options)->toDSL(); |
| 142 | + |
| 143 | + $this->assertArrayFragment(array_merge(['query' => 'foo'], $expected), $dsl); |
| 144 | + } |
| 145 | + |
| 146 | + public function provideMatch(): array |
| 147 | + { |
| 148 | + return [ |
| 149 | + 'operator' => ['and', ['operator' => 'and']], |
| 150 | + 'fuzziness' => [MatchOptions::make(fuzziness: 'AUTO'), ['fuzziness' => 'AUTO']], |
| 151 | + 'minimum_should_match' => [ |
| 152 | + MatchOptions::make(minimumShouldMatch: '50%'), |
| 153 | + ['minimum_should_match' => '50%'], |
| 154 | + ], |
| 155 | + 'many options' => [ |
| 156 | + MatchOptions::make(operator: 'or', fuzziness: '2', minimumShouldMatch: '30%'), |
| 157 | + ['minimum_should_match' => '30%', 'fuzziness' => '2', 'operator' => 'or'], |
| 158 | + ], |
| 159 | + ]; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @dataProvider provideMultiMatch |
| 164 | + */ |
| 165 | + public function testMultiMatch(string|MultiMatchOptions|null $options, array $expected): void |
| 166 | + { |
| 167 | + $dsl = BoolQueryBuilder::make()->whereMultiMatch(['foo', 'bar'], 'baz', $options)->toDSL(); |
| 168 | + |
| 169 | + $this->assertArrayFragment(array_merge(['query' => 'baz', 'fields' => ['foo', 'bar']], $expected), $dsl); |
| 170 | + } |
| 171 | + |
| 172 | + public function provideMultiMatch(): array |
| 173 | + { |
| 174 | + return [ |
| 175 | + 'type as string' => [MatchType::CROSS_FIELDS, ['type' => MatchType::CROSS_FIELDS]], |
| 176 | + 'type in options' => [MultiMatchOptions::make(MatchType::PHRASE), ['type' => MatchType::PHRASE]], |
| 177 | + 'fuzziness' => [MultiMatchOptions::make(fuzziness: 'AUTO'), ['fuzziness' => 'AUTO']], |
| 178 | + 'multiple options' => [ |
| 179 | + MultiMatchOptions::make(type: MatchType::MOST_FIELDS, fuzziness: '3', minimumShouldMatch: '30%'), |
| 180 | + ['minimum_should_match' => '30%', 'fuzziness' => '3', 'type' => MatchType::MOST_FIELDS], |
| 181 | + ], |
| 182 | + ]; |
| 183 | + } |
132 | 184 | } |
0 commit comments