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

IpRangeTest::testIpRangeKeyedAggregation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
            '*-192.168.1.200',
62
            '192.168.1.0/24',
63
            '192.168.1.101-*',
64
        ];
65
        $this->assertSame($expected, \array_keys($results['buckets']));
66
    }
67
68
    protected function _getIndexForTest(): Index
69
    {
70
        $index = $this->_createIndex();
71
        $index->setMapping(new Mapping([
72
            'address' => ['type' => 'ip'],
73
        ]));
74
75
        $index->addDocuments([
76
            new Document(1, ['address' => '192.168.1.100']),
77
            new Document(2, ['address' => '192.168.1.150']),
78
            new Document(3, ['address' => '192.168.1.200']),
79
        ]);
80
81
        $index->refresh();
82
83
        return $index;
84
    }
85
}
86