Completed
Push — master ( 9146c8...39ecb4 )
by Nicolas
04:52
created

SignificantTermsTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 89
Duplicated Lines 40.45 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 36
loc 89
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testSignificantTermsAggregationWithBackgroundFilterWithLegacyFilter() 18 18 1
A testSignificantTermsAggregation() 0 17 1
A testSignificantTermsAggregationWithBackgroundFilter() 18 18 1
A _getIndexForTest() 0 21 2

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\SignificantTerms;
6
use Elastica\Document;
7
use Elastica\Index;
8
use Elastica\Mapping;
9
use Elastica\Query;
10
use Elastica\Query\Terms;
11
12
/**
13
 * @internal
14
 */
15
class SignificantTermsTest extends BaseAggregationTest
16
{
17
    /**
18
     * @group functional
19
     */
20
    public function testSignificantTermsAggregation(): void
21
    {
22
        $agg = new SignificantTerms('significantTerms');
23
        $agg->setField('temperature');
24
        $agg->setSize(1);
25
26
        $termsQuery = new Terms('color', ['blue', 'red', 'green', 'yellow', 'white']);
27
28
        $query = new Query($termsQuery);
29
        $query->addAggregation($agg);
30
        $results = $this->_getIndexForTest()->search($query)->getAggregation('significantTerms');
31
32
        $this->assertCount(1, $results['buckets']);
33
        $this->assertEquals(63, $results['buckets'][0]['doc_count']);
34
        $this->assertEquals(79, $results['buckets'][0]['bg_count']);
35
        $this->assertEquals('1500', $results['buckets'][0]['key']);
36
    }
37
38
    /**
39
     * @group functional
40
     */
41 View Code Duplication
    public function testSignificantTermsAggregationWithBackgroundFilter(): 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...
42
    {
43
        $agg = new SignificantTerms('significantTerms');
44
        $agg->setField('temperature');
45
        $agg->setSize(1);
46
        $termsFilter = new Terms('color', ['blue', 'red', 'green', 'yellow']);
47
        $agg->setBackgroundFilter($termsFilter);
48
49
        $termsQuery = new Terms('color', ['blue', 'red', 'green', 'yellow', 'white']);
50
51
        $query = new Query($termsQuery);
52
        $query->addAggregation($agg);
53
        $results = $this->_getIndexForTest()->search($query)->getAggregation('significantTerms');
54
55
        $this->assertEquals(15, $results['buckets'][0]['doc_count']);
56
        $this->assertEquals(12, $results['buckets'][0]['bg_count']);
57
        $this->assertEquals('4500', $results['buckets'][0]['key']);
58
    }
59
60
    /**
61
     * @group functional
62
     */
63 View Code Duplication
    public function testSignificantTermsAggregationWithBackgroundFilterWithLegacyFilter(): 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...
64
    {
65
        $agg = new SignificantTerms('significantTerms');
66
        $agg->setField('temperature');
67
        $agg->setSize(1);
68
        $termsFilter = new Terms('color', ['blue', 'red', 'green', 'yellow']);
69
        $agg->setBackgroundFilter($termsFilter);
70
71
        $termsQuery = new Terms('color', ['blue', 'red', 'green', 'yellow', 'white']);
72
73
        $query = new Query($termsQuery);
74
        $query->addAggregation($agg);
75
        $results = $this->_getIndexForTest()->search($query)->getAggregation('significantTerms');
76
77
        $this->assertEquals(15, $results['buckets'][0]['doc_count']);
78
        $this->assertEquals(12, $results['buckets'][0]['bg_count']);
79
        $this->assertEquals('4500', $results['buckets'][0]['key']);
80
    }
81
82
    protected function _getIndexForTest(): Index
83
    {
84
        $index = $this->_createIndex();
85
        $colors = ['blue', 'blue', 'red', 'red', 'green', 'yellow', 'white', 'cyan', 'magenta'];
86
        $temperatures = [1500, 1500, 1500, 1500, 2500, 3500, 4500, 5500, 6500, 7500, 7500, 8500, 9500];
87
88
        $mapping = new Mapping([
89
            'color' => ['type' => 'keyword'],
90
            'temperature' => ['type' => 'keyword'],
91
        ]);
92
        $index->setMapping($mapping);
93
94
        $docs = [];
95
        for ($i = 0; $i < 250; ++$i) {
96
            $docs[] = new Document($i, ['color' => $colors[$i % \count($colors)], 'temperature' => $temperatures[$i % \count($temperatures)]]);
97
        }
98
        $index->addDocuments($docs);
99
        $index->refresh();
100
101
        return $index;
102
    }
103
}
104