Code Duplication    Length = 30-33 lines in 3 locations

tests/Query/FunctionScoreTest.php 3 locations

@@ 198-227 (lines=30) @@
195
    /**
196
     * @group functional
197
     */
198
    public function testWeight(): void
199
    {
200
        $filter = new Term(['price' => 4.5]);
201
        $query = new FunctionScore();
202
        $query->addWeightFunction(5.0, $filter);
203
204
        $expected = [
205
            'function_score' => [
206
                'functions' => [
207
                    [
208
                        'weight' => 5.0,
209
                        'filter' => [
210
                            'term' => [
211
                                'price' => 4.5,
212
                            ],
213
                        ],
214
                    ],
215
                ],
216
            ],
217
        ];
218
219
        $this->assertEquals($expected, $query->toArray());
220
221
        $response = $this->_getIndexForTest()->search($query);
222
        $results = $response->getResults();
223
224
        // the document with price = 4.5 should be scored highest
225
        $result0 = $results[0]->getData();
226
        $this->assertEquals("Mr. Frostie's", $result0['name']);
227
    }
228
229
    /**
230
     * @group functional
@@ 232-261 (lines=30) @@
229
    /**
230
     * @group functional
231
     */
232
    public function testWeightWithLegacyFilter(): void
233
    {
234
        $filter = new Term(['price' => 4.5]);
235
        $query = new FunctionScore();
236
        $query->addWeightFunction(5.0, $filter);
237
238
        $expected = [
239
            'function_score' => [
240
                'functions' => [
241
                    [
242
                        'weight' => 5.0,
243
                        'filter' => [
244
                            'term' => [
245
                                'price' => 4.5,
246
                            ],
247
                        ],
248
                    ],
249
                ],
250
            ],
251
        ];
252
253
        $this->assertEquals($expected, $query->toArray());
254
255
        $response = $this->_getIndexForTest()->search($query);
256
        $results = $response->getResults();
257
258
        // the document with price = 4.5 should be scored highest
259
        $result0 = $results[0]->getData();
260
        $this->assertEquals("Mr. Frostie's", $result0['name']);
261
    }
262
263
    /**
264
     * @group functional
@@ 417-449 (lines=33) @@
414
    /**
415
     * @group functional
416
     */
417
    public function testRandomScoreWithoutField(): void
418
    {
419
        $filter = new Term(['price' => 4.5]);
420
        $query = new FunctionScore();
421
        $query->addRandomScoreFunction(2, $filter);
422
423
        $expected = [
424
            'function_score' => [
425
                'functions' => [
426
                    [
427
                        'random_score' => [
428
                            'seed' => 2,
429
                        ],
430
                        'filter' => [
431
                            'term' => [
432
                                'price' => 4.5,
433
                            ],
434
                        ],
435
                    ],
436
                ],
437
            ],
438
        ];
439
440
        $this->assertEquals($expected, $query->toArray());
441
442
        $response = $this->_getIndexForTest()->search($query);
443
        $results = $response->getResults();
444
445
        // the document with the random score should have a score > 1, means it is the first result
446
        $result0 = $results[0]->getData();
447
448
        $this->assertEquals("Miller's Field", $result0['name']);
449
    }
450
451
    /**
452
     * @group functional