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

MaxTest::testMaxAggregationWithMissing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.7998
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
        $index = $this->_getIndexForTest();
79
80
        $agg = new Max('max_price');
81
        $agg->setField('price');
82
        $agg->setMissing(42);
83
84
        $query = new Query();
85
        $query->addAggregation($agg);
86
        $results = $index->search($query)->getAggregation('max_price');
87
88
        $this->assertEquals(42, $results['value']);
89
    }
90
91
    protected function _getIndexForTest(): Index
92
    {
93
        $index = $this->_createIndex();
94
95
        $index->addDocuments([
96
            new Document(1, ['price' => 5]),
97
            new Document(2, ['price' => self::MAX_PRICE]),
98
            new Document(3, ['price' => 1]),
99
            new Document(4, ['price' => 3]),
100
            new Document(5, ['anything' => 'anything']),
101
        ]);
102
103
        $index->refresh();
104
105
        return $index;
106
    }
107
}
108