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

testDateHistogramCalendarAggregation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 9.52
c 0
b 0
f 0
cc 3
nc 3
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
        $this->_checkVersion('7.2');
25
26
        $agg = new DateHistogram('hist', 'created');
27
        $agg->setFixedInterval('1h');
28
29
        $query = new Query();
30
        $query->addAggregation($agg);
31
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
32
33
        $docCount = 0;
34
        $nonDocCount = 0;
35
        foreach ($results['buckets'] as $bucket) {
36
            if (1 == $bucket['doc_count']) {
37
                ++$docCount;
38
            } else {
39
                ++$nonDocCount;
40
            }
41
        }
42
        // 3 Documents that were added
43
        $this->assertEquals(3, $docCount);
44
        // 1 document that was generated in between for the missing hour
45
        $this->assertEquals(1, $nonDocCount);
46
    }
47
48
    /**
49
     * @group unit
50
     * @group legacy
51
     */
52
    public function testDateHistogramAggregationWithIntervalTriggersADeprecation(): void
53
    {
54
        $this->expectDeprecation('Since ruflin/elastica 7.1.0: Argument 3 passed to "__construct()" is deprecated, use "setDateInterval()" or "setCalendarInterval()" instead. It will be removed in 8.0.');
55
        new DateHistogram('hist', 'created', 'day');
56
    }
57
58
    /**
59
     * @group unit
60
     * @group legacy
61
     */
62
    public function testDateHistogramAggregationSetIntervalTriggersADeprecation(): void
63
    {
64
        $agg = new DateHistogram('hist', 'created');
65
66
        $this->expectDeprecation('Since ruflin/elastica 7.1.0: The "setInterval()" method is deprecated, use "setDateInterval()" or "setCalendarInterval()" instead. It will be removed in 8.0.');
67
68
        $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...
69
    }
70
71
    /**
72
     * @group functional
73
     */
74 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...
75
    {
76
        $this->_checkVersion('7.2');
77
78
        $agg = new DateHistogram('hist', 'created');
79
        $agg->setCalendarInterval('1h');
80
81
        $query = new Query();
82
        $query->addAggregation($agg);
83
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
84
85
        $docCount = 0;
86
        $nonDocCount = 0;
87
        foreach ($results['buckets'] as $bucket) {
88
            if (1 == $bucket['doc_count']) {
89
                ++$docCount;
90
            } else {
91
                ++$nonDocCount;
92
            }
93
        }
94
        // 3 Documents that were added
95
        $this->assertEquals(3, $docCount);
96
        // 1 document that was generated in between for the missing hour
97
        $this->assertEquals(1, $nonDocCount);
98
    }
99
100
    /**
101
     * @group unit
102
     */
103 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...
104
    {
105
        $agg = new DateHistogram('hist', 'created');
106
        $agg->setFixedInterval('1h');
107
108
        $agg->setOffset('3m');
109
110
        $expected = [
111
            'date_histogram' => [
112
                'field' => 'created',
113
                'offset' => '3m',
114
                'fixed_interval' => '1h',
115
            ],
116
        ];
117
118
        $this->assertEquals($expected, $agg->toArray());
119
120
        $this->assertInstanceOf(DateHistogram::class, $agg->setOffset('3m'));
121
    }
122
123
    /**
124
     * @group functional
125
     */
126 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...
127
    {
128
        $this->_checkVersion('7.2');
129
130
        $agg = new DateHistogram('hist', 'created');
131
        $agg->setFixedInterval('1m');
132
        $agg->setOffset('+40s');
133
134
        $query = new Query();
135
        $query->addAggregation($agg);
136
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
137
138
        $this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']);
139
    }
140
141
    /**
142
     * @group unit
143
     */
144 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...
145
    {
146
        $agg = new DateHistogram('hist', 'created');
147
        $agg->setFixedInterval('1h');
148
149
        $agg->setTimezone('-02:30');
150
151
        $expected = [
152
            'date_histogram' => [
153
                'field' => 'created',
154
                'time_zone' => '-02:30',
155
                'fixed_interval' => '1h',
156
            ],
157
        ];
158
159
        $this->assertEquals($expected, $agg->toArray());
160
161
        $this->assertInstanceOf(DateHistogram::class, $agg->setTimezone('-02:30'));
162
    }
163
164
    protected function _getIndexForTest(): Index
165
    {
166
        $index = $this->_createIndex();
167
        $index->setMapping(new Mapping([
168
            'created' => ['type' => 'date'],
169
        ]));
170
171
        $index->addDocuments([
172
            new Document(1, ['created' => '2014-01-29T00:20:00']),
173
            new Document(2, ['created' => '2014-01-29T02:20:00']),
174
            new Document(3, ['created' => '2014-01-29T03:20:00']),
175
        ]);
176
177
        $index->refresh();
178
179
        return $index;
180
    }
181
}
182