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

MinTest::testMinAggregationWithMissing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.8666
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
        $agg = new Min('min_price');
38
        $agg->setField('price');
39
        $agg->setMissing(-42);
40
41
        $query = new Query();
42
        $query->addAggregation($agg);
43
        $results = $this->_getIndexForTest()->search($query)->getAggregation('min_price');
44
45
        $this->assertEquals(-42, $results['value']);
46
    }
47
48
    protected function _getIndexForTest(): Index
49
    {
50
        $index = $this->_createIndex();
51
52
        $index->addDocuments([
53
            new Document(1, ['price' => 5]),
54
            new Document(2, ['price' => 8]),
55
            new Document(3, ['price' => self::MIN_PRICE]),
56
            new Document(4, ['price' => 3]),
57
            new Document(5, []),
58
        ]);
59
60
        $index->refresh();
61
62
        return $index;
63
    }
64
}
65