Completed
Pull Request — master (#1876)
by romain
02:52
created

IpRangeTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 13.04 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 9
loc 69
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testIpRangeAggregation() 9 23 4
A testIpRangeKeyedAggregation() 0 18 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\IpRange;
6
use Elastica\Document;
7
use Elastica\Index;
8
use Elastica\Mapping;
9
use Elastica\Query;
10
11
/**
12
 * @internal
13
 */
14
class IpRangeTest extends BaseAggregationTest
15
{
16
    /**
17
     * @group functional
18
     */
19
    public function testIpRangeAggregation(): void
20
    {
21
        $agg = new IpRange('ip', 'address');
22
        $agg->addRange('192.168.1.101');
23
        $agg->addRange(null, '192.168.1.200');
24
25
        $cidrRange = '192.168.1.0/24';
26
        $agg->addMaskRange($cidrRange);
27
28
        $query = new Query();
29
        $query->addAggregation($agg);
30
        $results = $this->_getIndexForTest()->search($query)->getAggregation('ip');
31
32 View Code Duplication
        foreach ($results['buckets'] as $bucket) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
33
            if (\array_key_exists('key', $bucket) && $bucket['key'] == $cidrRange) {
34
                // the CIDR mask
35
                $this->assertEquals(3, $bucket['doc_count']);
36
            } else {
37
                // the normal ip ranges
38
                $this->assertEquals(2, $bucket['doc_count']);
39
            }
40
        }
41
    }
42
43
    /**
44
     * @group functional
45
     */
46
    public function testIpRangeKeyedAggregation(): void
47
    {
48
        $agg = new IpRange('ip', 'address');
49
        $agg->addRange('192.168.1.101');
50
        $agg->addRange(null, '192.168.1.200');
51
        $agg->setKeyed();
52
53
        $cidrRange = '192.168.1.0/24';
54
        $agg->addMaskRange($cidrRange);
55
56
        $query = new Query();
57
        $query->addAggregation($agg);
58
        $results = $this->_getIndexForTest()->search($query)->getAggregation('ip');
59
60
        $expected = [
61
        ];
62
        $this->assertSame($expected, array_keys($results['buckets']));
63
    }
64
65
    protected function _getIndexForTest(): Index
66
    {
67
        $index = $this->_createIndex();
68
        $index->setMapping(new Mapping([
69
            'address' => ['type' => 'ip'],
70
        ]));
71
72
        $index->addDocuments([
73
            new Document(1, ['address' => '192.168.1.100']),
74
            new Document(2, ['address' => '192.168.1.150']),
75
            new Document(3, ['address' => '192.168.1.200']),
76
        ]);
77
78
        $index->refresh();
79
80
        return $index;
81
    }
82
}
83