-
Notifications
You must be signed in to change notification settings - Fork 196
/
FunctionScoreQueryTest.php
112 lines (103 loc) · 3.34 KB
/
FunctionScoreQueryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Compound;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\Query\Compound\FunctionScoreQuery;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
/**
* Tests for FunctionScoreQuery.
*/
class FunctionScoreQueryTest extends \PHPUnit\Framework\TestCase
{
/**
* Data provider for testAddRandomFunction.
*
* @return array
*/
public function addRandomFunctionProvider()
{
return [
// Case #0. No seed.
[
'seed' => null,
'expectedArray' => [
'query' => null,
'functions' => [
[
'random_score' => new \stdClass(),
],
],
],
],
// Case #1. With seed.
[
'seed' => 'someSeed',
'expectedArray' => [
'query' => null,
'functions' => [
[
'random_score' => [ 'seed' => 'someSeed'],
],
],
],
],
];
}
/**
* Tests addRandomFunction method.
*
* @param mixed $seed
* @param array $expectedArray
*
* @dataProvider addRandomFunctionProvider
*/
public function testAddRandomFunction($seed, $expectedArray)
{
/** @var MatchAllQuery|MockObject $matchAllQuery */
$matchAllQuery = $this->getMockBuilder(MatchAllQuery::class)->getMock();
$functionScoreQuery = new FunctionScoreQuery($matchAllQuery);
$functionScoreQuery->addRandomFunction($seed);
$this->assertEquals(['function_score' => $expectedArray], $functionScoreQuery->toArray());
}
/**
* Tests default argument values.
*/
public function testAddFieldValueFactorFunction()
{
/** @var BuilderInterface|MockObject $builderInterface */
$builderInterface = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface');
$functionScoreQuery = new FunctionScoreQuery($builderInterface);
$functionScoreQuery->addFieldValueFactorFunction('field1', 2);
$functionScoreQuery->addFieldValueFactorFunction('field2', 1.5, 'ln');
$this->assertEquals(
[
'query' => null,
'functions' => [
[
'field_value_factor' => [
'field' => 'field1',
'factor' => 2,
'modifier' => 'none',
],
],
[
'field_value_factor' => [
'field' => 'field2',
'factor' => 1.5,
'modifier' => 'ln',
],
],
],
],
$functionScoreQuery->toArray()['function_score']
);
}
}