Completed
Pull Request — master (#1874)
by romain
03:52
created

DateHistogramTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 138
Duplicated Lines 71.01 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testDateHistogramAggregation() 23 23 3
A testDateHistogramCalendarAggregation() 23 23 3
A testSetOffset() 19 19 1
A testSetOffsetWorks() 14 14 1
A testSetTimezone() 19 19 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\DateHistogram;
6
use Elastica\Document;
7
use Elastica\Index;
8
use Elastica\Mapping;
9
use Elastica\Query;
10
11
/**
12
 * @internal
13
 */
14
class DateHistogramTest extends BaseAggregationTest
15
{
16
    /**
17
     * @group functional
18
     */
19 View Code Duplication
    public function testDateHistogramAggregation(): 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...
20
    {
21
        $agg = new DateHistogram('hist', 'created');
22
        $agg->setFixedInterval('1h');
23
24
        $query = new Query();
25
        $query->addAggregation($agg);
26
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
27
28
        $docCount = 0;
29
        $nonDocCount = 0;
30
        foreach ($results['buckets'] as $bucket) {
31
            if (1 == $bucket['doc_count']) {
32
                ++$docCount;
33
            } else {
34
                ++$nonDocCount;
35
            }
36
        }
37
        // 3 Documents that were added
38
        $this->assertEquals(3, $docCount);
39
        // 1 document that was generated in between for the missing hour
40
        $this->assertEquals(1, $nonDocCount);
41
    }
42
43
    /**
44
     * @group functional
45
     */
46 View Code Duplication
    public function testDateHistogramCalendarAggregation(): 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...
47
    {
48
        $agg = new DateHistogram('hist', 'created');
49
        $agg->setCalendarInterval('1h');
50
51
        $query = new Query();
52
        $query->addAggregation($agg);
53
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
54
55
        $docCount = 0;
56
        $nonDocCount = 0;
57
        foreach ($results['buckets'] as $bucket) {
58
            if (1 == $bucket['doc_count']) {
59
                ++$docCount;
60
            } else {
61
                ++$nonDocCount;
62
            }
63
        }
64
        // 3 Documents that were added
65
        $this->assertEquals(3, $docCount);
66
        // 1 document that was generated in between for the missing hour
67
        $this->assertEquals(1, $nonDocCount);
68
    }
69
70
    /**
71
     * @group unit
72
     */
73 View Code Duplication
    public function testSetOffset(): 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...
74
    {
75
        $agg = new DateHistogram('hist', 'created');
76
        $agg->setFixedInterval('1h');
77
78
        $agg->setOffset('3m');
79
80
        $expected = [
81
            'date_histogram' => [
82
                'field' => 'created',
83
                'interval' => '1h',
84
                'offset' => '3m',
85
            ],
86
        ];
87
88
        $this->assertEquals($expected, $agg->toArray());
89
90
        $this->assertInstanceOf(DateHistogram::class, $agg->setOffset('3m'));
91
    }
92
93
    /**
94
     * @group functional
95
     */
96 View Code Duplication
    public function testSetOffsetWorks(): 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...
97
    {
98
        $this->_checkVersion('1.5');
99
100
        $agg = new DateHistogram('hist', 'created');
101
        $agg->setFixedInterval('1m');
102
        $agg->setOffset('+40s');
103
104
        $query = new Query();
105
        $query->addAggregation($agg);
106
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
107
108
        $this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']);
109
    }
110
111
    /**
112
     * @group unit
113
     */
114 View Code Duplication
    public function testSetTimezone(): 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...
115
    {
116
        $agg = new DateHistogram('hist', 'created');
117
        $agg->setFixedInterval('1h');
118
119
        $agg->setTimezone('-02:30');
120
121
        $expected = [
122
            'date_histogram' => [
123
                'field' => 'created',
124
                'interval' => '1h',
125
                'time_zone' => '-02:30',
126
            ],
127
        ];
128
129
        $this->assertEquals($expected, $agg->toArray());
130
131
        $this->assertInstanceOf(DateHistogram::class, $agg->setTimezone('-02:30'));
132
    }
133
134
    protected function _getIndexForTest(): Index
135
    {
136
        $index = $this->_createIndex();
137
        $index->setMapping(new Mapping([
138
            'created' => ['type' => 'date'],
139
        ]));
140
141
        $index->addDocuments([
142
            new Document(1, ['created' => '2014-01-29T00:20:00']),
143
            new Document(2, ['created' => '2014-01-29T02:20:00']),
144
            new Document(3, ['created' => '2014-01-29T03:20:00']),
145
        ]);
146
147
        $index->refresh();
148
149
        return $index;
150
    }
151
}
152