Completed
Pull Request — master (#1875)
by romain
03:31 queued 01:02
created

MinTest::testMinAggregationWithMissing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
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\Min;
6
use Elastica\Document;
7
use Elastica\Index;
8
use Elastica\Query;
9
10
/**
11
 * @internal
12
 */
13
class MinTest extends BaseAggregationTest
14
{
15
    private const MIN_PRICE = 1;
16
17
    /**
18
     * @group functional
19
     */
20
    public function testMinAggregation(): void
21
    {
22
        $agg = new Min('min_price');
23
        $agg->setField('price');
24
25
        $query = new Query();
26
        $query->addAggregation($agg);
27
        $results = $this->_getIndexForTest()->search($query)->getAggregation('min_price');
28
29
        $this->assertEquals(self::MIN_PRICE, $results['value']);
30
    }
31
32
    /**
33
     * @group functional
34
     */
35
    public function testMinAggregationWithMissing(): void
36
    {
37
        // feature is buggy on version prior 7.5;
38
        $this->_checkVersion('7.5');
39
40
        $agg = new Min('min_price');
41
        $agg->setField('price');
42
        $agg->setMissing(-42);
43
44
        $query = new Query();
45
        $query->addAggregation($agg);
46
        $results = $this->_getIndexForTest()->search($query)->getAggregation('min_price');
47
48
        $this->assertEquals(-42, $results['value']);
49
    }
50
51
    protected function _getIndexForTest(): Index
52
    {
53
        $index = $this->_createIndex();
54
55
        $index->addDocuments([
56
            new Document(1, ['price' => 5]),
57
            new Document(2, ['price' => 8]),
58
            new Document(3, ['price' => self::MIN_PRICE]),
59
            new Document(4, ['price' => 3]),
60
            new Document(5, ['anything' => 'anything']),
61
        ]);
62
63
        $index->refresh();
64
65
        return $index;
66
    }
67
}
68