Completed
Pull Request — master (#1875)
by romain
09:47
created

HistogramTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 83
Duplicated Lines 57.83 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 48
loc 83
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testHistogramAggregation() 14 14 1
A testHistogramAggregationWithMissing() 15 15 1
A testHistogramKeyedAggregation() 19 19 1
A _getIndexForTest() 0 20 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Elastica\Test\Aggregation;
4
5
use Elastica\Aggregation\Histogram;
6
use Elastica\Document;
7
use Elastica\Index;
8
use Elastica\Query;
9
10
/**
11
 * @internal
12
 */
13
class HistogramTest extends BaseAggregationTest
14
{
15
    /**
16
     * @group functional
17
     */
18 View Code Duplication
    public function testHistogramAggregation(): 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...
19
    {
20
        $agg = new Histogram('hist', 'price', 10);
21
        $agg->setMinimumDocumentCount(0); // should return empty buckets
22
23
        $query = new Query();
24
        $query->addAggregation($agg);
25
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
26
27
        $buckets = $results['buckets'];
28
        $this->assertCount(5, $buckets);
29
        $this->assertEquals(30, $buckets[3]['key']);
30
        $this->assertEquals(2, $buckets[3]['doc_count']);
31
    }
32
33
    /**
34
     * @group functional
35
     */
36 View Code Duplication
    public function testHistogramAggregationWithMissing(): 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...
37
    {
38
        $agg = new Histogram('hist', 'price', 10);
39
        $agg->setMinimumDocumentCount(0); // should return empty buckets
40
        $agg->setMissing(37);
41
42
        $query = new Query();
43
        $query->addAggregation($agg);
44
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
45
46
        $buckets = $results['buckets'];
47
        $this->assertCount(5, $buckets);
48
        $this->assertEquals(30, $buckets[3]['key']);
49
        $this->assertEquals(3, $buckets[3]['doc_count']);
50
    }
51
52
    /**
53
     * @group functional
54
     */
55 View Code Duplication
    public function testHistogramKeyedAggregation(): 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...
56
    {
57
        $agg = new Histogram('hist', 'price', 10);
58
        $agg->setMinimumDocumentCount(0); // should return empty buckets
59
        $agg->setKeyed();
60
61
        $query = new Query();
62
        $query->addAggregation($agg);
63
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
64
65
        $expected = [
66
            '0.0',
67
            '10.0',
68
            '20.0',
69
            '30.0',
70
            '40.0',
71
        ];
72
        $this->assertSame($expected, \array_keys($results['buckets']));
73
    }
74
75
    protected function _getIndexForTest(): Index
76
    {
77
        $index = $this->_createIndex();
78
79
        $index->addDocuments([
80
            new Document(1, ['price' => 5, 'color' => 'blue']),
81
            new Document(2, ['price' => 8, 'color' => 'blue']),
82
            new Document(3, ['price' => 1, 'color' => 'red']),
83
            new Document(4, ['price' => 30, 'color' => 'green']),
84
            new Document(5, ['price' => 40, 'color' => 'red']),
85
            new Document(6, ['price' => 35, 'color' => 'green']),
86
            new Document(7, ['price' => 42, 'color' => 'red']),
87
            new Document(8, ['price' => 41, 'color' => 'blue']),
88
            new Document(9, ['color' => 'yellow']),
89
        ]);
90
91
        $index->refresh();
92
93
        return $index;
94
    }
95
}
96