Completed
Pull Request — master (#1876)
by romain
02:39 queued 13s
created

RangeTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 91
Duplicated Lines 30.77 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testRangeAggregation() 12 12 1
A testRangeKeyedAggregation() 16 16 1
A testRangeAggregationWithKey() 0 31 1
A _getIndexForTest() 0 17 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\Range;
6
use Elastica\Document;
7
use Elastica\Index;
8
use Elastica\Query;
9
10
/**
11
 * @internal
12
 */
13
class RangeTest extends BaseAggregationTest
14
{
15
    /**
16
     * @group functional
17
     */
18 View Code Duplication
    public function testRangeAggregation(): 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 Range('range');
21
        $agg->setField('price');
22
        $agg->addRange(1.5, 5);
23
24
        $query = new Query();
25
        $query->addAggregation($agg);
26
        $results = $this->_getIndexForTest()->search($query)->getAggregation('range');
27
28
        $this->assertEquals(2, $results['buckets'][0]['doc_count']);
29
    }
30
31
    /**
32
     * @group functional
33
     */
34 View Code Duplication
    public function testRangeKeyedAggregation(): 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...
35
    {
36
        $agg = new Range('range');
37
        $agg->setField('price');
38
        $agg->addRange(1.5, 5);
39
        $agg->setKeyed();
40
41
        $query = new Query();
42
        $query->addAggregation($agg);
43
        $results = $this->_getIndexForTest()->search($query)->getAggregation('range');
44
45
        $expected = [
46
            '1.5-5.0',
47
        ];
48
        $this->assertSame($expected, \array_keys($results['buckets']));
49
    }
50
51
    /**
52
     * @group unit
53
     */
54
    public function testRangeAggregationWithKey(): void
55
    {
56
        $agg = new Range('range');
57
        $agg->setField('price');
58
        $agg->addRange(null, 50, 'cheap');
59
        $agg->addRange(50, 100, 'average');
60
        $agg->addRange(100, null, 'expensive');
61
62
        $expected = [
63
            'range' => [
64
                'field' => 'price',
65
                'ranges' => [
66
                    [
67
                        'to' => 50,
68
                        'key' => 'cheap',
69
                    ],
70
                    [
71
                        'from' => 50,
72
                        'to' => 100,
73
                        'key' => 'average',
74
                    ],
75
                    [
76
                        'from' => 100,
77
                        'key' => 'expensive',
78
                    ],
79
                ],
80
            ],
81
        ];
82
83
        $this->assertEquals($expected, $agg->toArray());
84
    }
85
86
    protected function _getIndexForTest(): Index
87
    {
88
        $index = $this->_createIndex();
89
90
        $index->addDocuments([
91
            new Document(1, ['price' => 5]),
92
            new Document(2, ['price' => 8]),
93
            new Document(3, ['price' => 1]),
94
            new Document(4, ['price' => 3]),
95
            new Document(5, ['price' => 1.5]),
96
            new Document(6, ['price' => 2]),
97
        ]);
98
99
        $index->refresh();
100
101
        return $index;
102
    }
103
}
104