Completed
Pull Request — master (#1874)
by romain
02:48
created

testDateHistogramCalendarAggregation()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 30

Duplication

Lines 30
Ratio 100 %

Importance

Changes 0
Metric Value
dl 30
loc 30
rs 9.44
c 0
b 0
f 0
cc 4
nc 6
nop 0
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
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
11
12
/**
13
 * @internal
14
 */
15
class DateHistogramTest extends BaseAggregationTest
16
{
17
    use ExpectDeprecationTrait;
18
19
    /**
20
     * @group functional
21
     */
22 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...
23
    {
24
        $agg = new DateHistogram('hist', 'created');
25
26
        $version = $this->_getVersion();
27
28
        if (\version_compare($version, '7.2') < 0) {
29
            $agg->setParam('interval', '1h');
30
        } else {
31
            $agg->setFixedInterval('1h');
32
        }
33
34
        $query = new Query();
35
        $query->addAggregation($agg);
36
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
37
38
        $docCount = 0;
39
        $nonDocCount = 0;
40
        foreach ($results['buckets'] as $bucket) {
41
            if (1 == $bucket['doc_count']) {
42
                ++$docCount;
43
            } else {
44
                ++$nonDocCount;
45
            }
46
        }
47
        // 3 Documents that were added
48
        $this->assertEquals(3, $docCount);
49
        // 1 document that was generated in between for the missing hour
50
        $this->assertEquals(1, $nonDocCount);
51
    }
52
53
    /**
54
     * @group unit
55
     * @group legacy
56
     */
57
    public function testDateHistogramAggregationWithIntervalTriggersADeprecation(): void
58
    {
59
        $this->expectDeprecation('Since ruflin/elastica 7.1.0: Argument 3 passed to "Elastica\Aggregation\DateHistogram::__construct()" is deprecated, use "setDateInterval()" or "setCalendarInterval()" instead. It will be removed in 8.0.');
60
        new DateHistogram('hist', 'created', 'day');
61
    }
62
63
    /**
64
     * @group unit
65
     * @group legacy
66
     */
67
    public function testDateHistogramAggregationSetIntervalTriggersADeprecation(): void
68
    {
69
        $agg = new DateHistogram('hist', 'created');
70
71
        $this->expectDeprecation('Since ruflin/elastica 7.1.0: The "Elastica\Aggregation\DateHistogram::setInterval()" method is deprecated, use "setDateInterval()" or "setCalendarInterval()" instead. It will be removed in 8.0.');
72
73
        $agg->setInterval('day');
0 ignored issues
show
Deprecated Code introduced by
The method Elastica\Aggregation\DateHistogram::setInterval() has been deprecated with message: Deprecated since 7.1.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
74
    }
75
76
    /**
77
     * @group functional
78
     */
79 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...
80
    {
81
        $agg = new DateHistogram('hist', 'created');
82
83
        $version = $this->_getVersion();
84
85
        if (\version_compare($version, '7.2') < 0) {
86
            $agg->setParam('interval', '1h');
87
        } else {
88
            $agg->setCalendarInterval('1h');
89
        }
90
91
        $query = new Query();
92
        $query->addAggregation($agg);
93
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
94
95
        $docCount = 0;
96
        $nonDocCount = 0;
97
        foreach ($results['buckets'] as $bucket) {
98
            if (1 == $bucket['doc_count']) {
99
                ++$docCount;
100
            } else {
101
                ++$nonDocCount;
102
            }
103
        }
104
        // 3 Documents that were added
105
        $this->assertEquals(3, $docCount);
106
        // 1 document that was generated in between for the missing hour
107
        $this->assertEquals(1, $nonDocCount);
108
    }
109
110
    /**
111
     * @group functional
112
     */
113 View Code Duplication
    public function testDateHistogramAggregationWithMissing(): 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...
114
    {
115
        $agg = new DateHistogram('hist', 'created');
116
117
        $version = $this->_getVersion();
118
119
        if (\version_compare($version, '7.2') < 0) {
120
            $agg->setParam('interval', '1h');
121
        } else {
122
            $agg->setFixedInterval('1h');
123
        }
124
125
        $agg->setMissing('2014-01-29T04:20:00');
126
127
        $query = new Query();
128
        $query->addAggregation($agg);
129
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
130
131
        $docCount = 0;
132
        $nonDocCount = 0;
133
        foreach ($results['buckets'] as $bucket) {
134
            if (1 == $bucket['doc_count']) {
135
                ++$docCount;
136
            } else {
137
                ++$nonDocCount;
138
            }
139
        }
140
        // 3 Documents that were added
141
        $this->assertEquals(4, $docCount);
142
        // 1 document that was generated in between for the missing hour
143
        $this->assertEquals(1, $nonDocCount);
144
    }
145
146
    /**
147
     * @group functional
148
     */
149
    public function testDateHistogramKeyedAggregation(): void
150
    {
151
        $agg = new DateHistogram('hist', 'created');
152
153
        $version = $this->_getVersion();
154
155
        if (\version_compare($version, '7.2') < 0) {
156
            $agg->setParam('interval', '1h');
157
        } else {
158
            $agg->setFixedInterval('1h');
159
        }
160
161
        $agg->setKeyed();
162
163
        $query = new Query();
164
        $query->addAggregation($agg);
165
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
166
167
        $expected = [
168
            '2014-01-29T00:00:00.000Z',
169
            '2014-01-29T01:00:00.000Z',
170
            '2014-01-29T02:00:00.000Z',
171
            '2014-01-29T03:00:00.000Z',
172
        ];
173
        $this->assertSame($expected, \array_keys($results['buckets']));
174
    }
175
176
    /**
177
     * @group unit
178
     */
179 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...
180
    {
181
        $agg = new DateHistogram('hist', 'created');
182
        $agg->setFixedInterval('1h');
183
184
        $agg->setOffset('3m');
185
186
        $expected = [
187
            'date_histogram' => [
188
                'field' => 'created',
189
                'offset' => '3m',
190
                'fixed_interval' => '1h',
191
            ],
192
        ];
193
194
        $this->assertEquals($expected, $agg->toArray());
195
196
        $this->assertInstanceOf(DateHistogram::class, $agg->setOffset('3m'));
197
    }
198
199
    /**
200
     * @group functional
201
     */
202
    public function testSetOffsetWorks(): void
203
    {
204
        $agg = new DateHistogram('hist', 'created');
205
        $agg->setOffset('+40s');
206
207
        $version = $this->_getVersion();
208
209
        if (\version_compare($version, '7.2') < 0) {
210
            $agg->setParam('interval', '1m');
211
        } else {
212
            $agg->setFixedInterval('1m');
213
        }
214
215
        $query = new Query();
216
        $query->addAggregation($agg);
217
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
218
219
        $this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']);
220
    }
221
222
    /**
223
     * @group unit
224
     */
225 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...
226
    {
227
        $agg = new DateHistogram('hist', 'created');
228
        $agg->setFixedInterval('1h');
229
230
        $agg->setTimezone('-02:30');
231
232
        $expected = [
233
            'date_histogram' => [
234
                'field' => 'created',
235
                'time_zone' => '-02:30',
236
                'fixed_interval' => '1h',
237
            ],
238
        ];
239
240
        $this->assertEquals($expected, $agg->toArray());
241
242
        $this->assertInstanceOf(DateHistogram::class, $agg->setTimezone('-02:30'));
243
    }
244
245 View Code Duplication
    protected function _getIndexForTest(): Index
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...
246
    {
247
        $index = $this->_createIndex();
248
        $index->setMapping(new Mapping([
249
            'created' => ['type' => 'date'],
250
        ]));
251
252
        $index->addDocuments([
253
            new Document(1, ['created' => '2014-01-29T00:20:00']),
254
            new Document(2, ['created' => '2014-01-29T02:20:00']),
255
            new Document(3, ['created' => '2014-01-29T03:20:00']),
256
            new Document(4, ['anything' => 'anything']),
257
        ]);
258
259
        $index->refresh();
260
261
        return $index;
262
    }
263
}
264