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

StatsTest::testStatsAggregationWithMissing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
c 0
b 0
f 0
rs 9.7333
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica\Test\Aggregation;
4
5
use Elastica\Aggregation\Stats;
6
use Elastica\Document;
7
use Elastica\Index;
8
use Elastica\Query;
9
10
/**
11
 * @internal
12
 */
13
class StatsTest extends BaseAggregationTest
14
{
15
    /**
16
     * @group functional
17
     */
18
    public function testStatsAggregation(): void
19
    {
20
        $agg = new Stats('stats');
21
        $agg->setField('price');
22
23
        $query = new Query();
24
        $query->addAggregation($agg);
25
        $results = $this->_getIndexForTest()->search($query)->getAggregation('stats');
26
27
        $this->assertEquals(4, $results['count']);
28
        $this->assertEquals(1, $results['min']);
29
        $this->assertEquals(8, $results['max']);
30
        $this->assertEquals((5 + 8 + 1 + 3) / 4.0, $results['avg']);
31
        $this->assertEquals((5 + 8 + 1 + 3), $results['sum']);
32
    }
33
34
    /**
35
     * @group functional
36
     */
37 View Code Duplication
    public function testStatsAggregationWithMissing(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $agg = new Stats('stats');
40
        $agg->setField('price');
41
        $agg->setMissing(10);
42
43
        $query = new Query();
44
        $query->addAggregation($agg);
45
        $results = $this->_getIndexForTest()->search($query)->getAggregation('stats');
46
47
        $this->assertEquals(5, $results['count']);
48
        $this->assertEquals(1, $results['min']);
49
        $this->assertEquals(10, $results['max']);
50
        $this->assertEquals((5 + 8 + 1 + 3 + 10) / 5.0, $results['avg']);
51
        $this->assertEquals((5 + 8 + 1 + 3 + 10), $results['sum']);
52
    }
53
54 View Code Duplication
    protected function _getIndexForTest(): Index
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        $index = $this->_createIndex();
57
58
        $index->addDocuments([
59
            new Document(1, ['price' => 5]),
60
            new Document(2, ['price' => 8]),
61
            new Document(3, ['price' => 1]),
62
            new Document(4, ['price' => 3]),
63
            new Document(5, ['anything' => 'anything']),
64
        ]);
65
66
        $index->refresh();
67
68
        return $index;
69
    }
70
}
71