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

TermsTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 149
Duplicated Lines 55.03 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 82
loc 149
c 0
b 0
f 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testIncludePattern() 0 7 1
A testIncludeWithPartitions() 10 10 1
A testExcludePattern() 0 7 1
A testExcludeExactMatch() 7 7 1
A testTermsAggregation() 12 12 1
A testTermsSetOrder() 12 12 1
A testIncludeExactMatch() 7 7 1
A testTermsWithMissingAggregation() 13 13 1
A testTermsSetOrders() 0 17 1
A getIndex() 21 21 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\Terms;
6
use Elastica\Document;
7
use Elastica\Index;
8
use Elastica\Mapping;
9
use Elastica\Query;
10
11
/**
12
 * @internal
13
 */
14
class TermsTest extends BaseAggregationTest
15
{
16
    /**
17
     * @group unit
18
     */
19
    public function testIncludePattern(): void
20
    {
21
        $agg = new Terms('terms');
22
        $agg->setInclude('pattern*');
23
24
        $this->assertSame('pattern*', $agg->getParam('include'));
25
    }
26
27
    /**
28
     * @group unit
29
     */
30 View Code Duplication
    public function testIncludeExactMatch(): 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...
31
    {
32
        $agg = new Terms('terms');
33
        $agg->setIncludeAsExactMatch(['first', 'second']);
34
35
        $this->assertSame(['first', 'second'], $agg->getParam('include'));
36
    }
37
38
    /**
39
     * @group unit
40
     */
41 View Code Duplication
    public function testIncludeWithPartitions(): 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 Terms('terms');
44
        $agg->setIncludeWithPartitions(1, 23);
45
46
        $this->assertSame([
47
            'partition' => 1,
48
            'num_partitions' => 23,
49
        ], $agg->getParam('include'));
50
    }
51
52
    /**
53
     * @group unit
54
     */
55
    public function testExcludePattern(): void
56
    {
57
        $agg = new Terms('terms');
58
        $agg->setExclude('pattern*');
59
60
        $this->assertSame('pattern*', $agg->getParam('exclude'));
61
    }
62
63
    /**
64
     * @group unit
65
     */
66 View Code Duplication
    public function testExcludeExactMatch(): 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...
67
    {
68
        $agg = new Terms('terms');
69
        $agg->setExcludeAsExactMatch(['first', 'second']);
70
71
        $this->assertSame(['first', 'second'], $agg->getParam('exclude'));
72
    }
73
74
    /**
75
     * @group functional
76
     */
77 View Code Duplication
    public function testTermsAggregation(): 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...
78
    {
79
        $agg = new Terms('terms');
80
        $agg->setField('color');
81
82
        $query = new Query();
83
        $query->addAggregation($agg);
84
        $results = $this->getIndex()->search($query)->getAggregation('terms');
85
86
        $this->assertEquals(2, $results['buckets'][0]['doc_count']);
87
        $this->assertEquals('blue', $results['buckets'][0]['key']);
88
    }
89
90
    /**
91
     * @group functional
92
     */
93 View Code Duplication
    public function testTermsSetOrder(): 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...
94
    {
95
        $agg = new Terms('terms');
96
        $agg->setField('color');
97
        $agg->setOrder('_count', 'asc');
98
99
        $query = new Query();
100
        $query->addAggregation($agg);
101
        $results = $this->getIndex()->search($query)->getAggregation('terms');
102
103
        $this->assertEquals('blue', $results['buckets'][2]['key']);
104
    }
105
106 View Code Duplication
    public function testTermsWithMissingAggregation(): 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...
107
    {
108
        $agg = new Terms('terms');
109
        $agg->setField('color');
110
        $agg->setMissing('blue');
111
112
        $query = new Query();
113
        $query->addAggregation($agg);
114
        $results = $this->getIndex()->search($query)->getAggregation('terms');
115
116
        $this->assertEquals(3, $results['buckets'][0]['doc_count']);
117
        $this->assertEquals('blue', $results['buckets'][0]['key']);
118
    }
119
120
    /**
121
     * @group functional
122
     */
123
    public function testTermsSetOrders(): void
124
    {
125
        $agg = new Terms('terms');
126
        $agg->setField('color');
127
        $agg->setOrders([
128
            ['_count' => 'asc'], // 1. red,   2. green, 3. blue
129
            ['_key' => 'asc'],   // 1. green, 2. red,   3. blue
130
        ]);
131
132
        $query = new Query();
133
        $query->addAggregation($agg);
134
        $results = $this->getIndex()->search($query)->getAggregation('terms');
135
136
        $this->assertSame('green', $results['buckets'][0]['key']);
137
        $this->assertSame('red', $results['buckets'][1]['key']);
138
        $this->assertSame('blue', $results['buckets'][2]['key']);
139
    }
140
141 View Code Duplication
    private function getIndex(): 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...
142
    {
143
        $index = $this->_createIndex();
144
145
        $mapping = new Mapping([
146
            'color' => ['type' => 'keyword'],
147
        ]);
148
        $index->setMapping($mapping);
149
150
        $index->addDocuments([
151
            new Document(1, ['color' => 'blue']),
152
            new Document(2, ['color' => 'blue']),
153
            new Document(3, ['color' => 'red']),
154
            new Document(4, ['color' => 'green']),
155
            new Document(5, ['anything' => 'anything']),
156
        ]);
157
158
        $index->refresh();
159
160
        return $index;
161
    }
162
}
163