Completed
Pull Request — master (#1874)
by romain
02:42
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 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...
114
    {
115
        $agg = new DateHistogram('hist', 'created', '1h');
116
        $agg->setKeyed();
117
118
        $query = new Query();
119
        $query->addAggregation($agg);
120
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
121
122
        $expected = [
123
            '2014-01-29T00:00:00.000Z',
124
            '2014-01-29T01:00:00.000Z',
125
            '2014-01-29T02:00:00.000Z',
126
            '2014-01-29T03:00:00.000Z',
127
        ];
128
        $this->assertSame($expected, \array_keys($results['buckets']));
129
    }
130
131
    /**
132
     * @group unit
133
     */
134 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...
135
    {
136
        $agg = new DateHistogram('hist', 'created');
137
        $agg->setFixedInterval('1h');
138
139
        $agg->setOffset('3m');
140
141
        $expected = [
142
            'date_histogram' => [
143
                'field' => 'created',
144
                'offset' => '3m',
145
                'fixed_interval' => '1h',
146
            ],
147
        ];
148
149
        $this->assertEquals($expected, $agg->toArray());
150
151
        $this->assertInstanceOf(DateHistogram::class, $agg->setOffset('3m'));
152
    }
153
154
    /**
155
     * @group functional
156
     */
157
    public function testSetOffsetWorks(): void
158
    {
159
        $agg = new DateHistogram('hist', 'created');
160
        $agg->setOffset('+40s');
161
162
        $version = $this->_getVersion();
163
164
        if (\version_compare($version, '7.2') < 0) {
165
            $agg->setParam('interval', '1m');
166
        } else {
167
            $agg->setFixedInterval('1m');
168
        }
169
170
        $query = new Query();
171
        $query->addAggregation($agg);
172
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
173
174
        $this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']);
175
    }
176
177
    /**
178
     * @group unit
179
     */
180 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...
181
    {
182
        $agg = new DateHistogram('hist', 'created');
183
        $agg->setFixedInterval('1h');
184
185
        $agg->setTimezone('-02:30');
186
187
        $expected = [
188
            'date_histogram' => [
189
                'field' => 'created',
190
                'time_zone' => '-02:30',
191
                'fixed_interval' => '1h',
192
            ],
193
        ];
194
195
        $this->assertEquals($expected, $agg->toArray());
196
197
        $this->assertInstanceOf(DateHistogram::class, $agg->setTimezone('-02:30'));
198
    }
199
200
    protected function _getIndexForTest(): Index
201
    {
202
        $index = $this->_createIndex();
203
        $index->setMapping(new Mapping([
204
            'created' => ['type' => 'date'],
205
        ]));
206
207
        $index->addDocuments([
208
            new Document(1, ['created' => '2014-01-29T00:20:00']),
209
            new Document(2, ['created' => '2014-01-29T02:20:00']),
210
            new Document(3, ['created' => '2014-01-29T03:20:00']),
211
        ]);
212
213
        $index->refresh();
214
215
        return $index;
216
    }
217
}
218