Completed
Pull Request — master (#1875)
by romain
02:58
created

MaxTest::testMaxAggregationWithMissing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica\Test\Aggregation;
4
5
use Elastica\Aggregation\Max;
6
use Elastica\Document;
7
use Elastica\Index;
8
use Elastica\Query;
9
use Elastica\Script\Script;
10
11
/**
12
 * @internal
13
 */
14
class MaxTest extends BaseAggregationTest
15
{
16
    private const MAX_PRICE = 8;
17
18
    /**
19
     * @group unit
20
     */
21
    public function testToArray(): void
22
    {
23
        $expected = [
24
            'max' => [
25
                'field' => 'price',
26
                'script' => [
27
                    'source' => '_value * params.conversion_rate',
28
                    'params' => [
29
                        'conversion_rate' => 1.2,
30
                    ],
31
                ],
32
            ],
33
            'aggs' => [
34
                'subagg' => ['max' => ['field' => 'foo']],
35
            ],
36
        ];
37
38
        $agg = new Max('max_price_in_euros');
39
        $agg->setField('price');
40
        $agg->setScript(new Script('_value * params.conversion_rate', ['conversion_rate' => 1.2]));
41
        $max = new Max('subagg');
42
        $max->setField('foo');
43
        $agg->addAggregation($max);
44
45
        $this->assertEquals($expected, $agg->toArray());
46
    }
47
48
    /**
49
     * @group functional
50
     */
51
    public function testMaxAggregation(): void
52
    {
53
        $index = $this->_getIndexForTest();
54
55
        $agg = new Max('max_price');
56
        $agg->setField('price');
57
58
        $query = new Query();
59
        $query->addAggregation($agg);
60
        $results = $index->search($query)->getAggregation('max_price');
61
62
        $this->assertEquals(self::MAX_PRICE, $results['value']);
63
64
        // test using a script
65
        $agg->setScript(new Script('_value * params.conversion_rate', ['conversion_rate' => 1.2], Script::LANG_PAINLESS));
66
        $query = new Query();
67
        $query->addAggregation($agg);
68
        $results = $index->search($query)->getAggregation('max_price');
69
70
        $this->assertEquals(self::MAX_PRICE * 1.2, $results['value']);
71
    }
72
73
    /**
74
     * @group functional
75
     */
76
    public function testMaxAggregationWithMissing(): void
77
    {
78
        // feature is buggy on version prior 7.5;
79
        $this->_checkVersion('7.5');
80
81
        $index = $this->_getIndexForTest();
82
83
        $agg = new Max('max_price');
84
        $agg->setField('price');
85
        $agg->setMissing(42);
86
87
        $query = new Query();
88
        $query->addAggregation($agg);
89
        $results = $index->search($query)->getAggregation('max_price');
90
91
        $this->assertEquals(42, $results['value']);
92
    }
93
94
    protected function _getIndexForTest(): Index
95
    {
96
        $index = $this->_createIndex();
97
98
        $index->addDocuments([
99
            new Document(1, ['price' => 5]),
100
            new Document(2, ['price' => self::MAX_PRICE]),
101
            new Document(3, ['price' => 1]),
102
            new Document(4, ['price' => 3]),
103
            new Document(5, ['anything' => 'anything']),
104
        ]);
105
106
        $index->refresh();
107
108
        return $index;
109
    }
110
}
111