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

DateHistogramTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 128
Duplicated Lines 13.28 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 17
loc 128
rs 10
c 0
b 0
f 0

6 Methods

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