Completed
Pull Request — master (#1874)
by romain
04:35 queued 01:40
created

testDateHistogramAggregationSetIntervalTriggersADeprecation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
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
        $this->_checkVersion('7.2');
22
23
        $agg = new DateHistogram('hist', 'created');
24
        $agg->setFixedInterval('1h');
25
26
        $query = new Query();
27
        $query->addAggregation($agg);
28
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
29
30
        $docCount = 0;
31
        $nonDocCount = 0;
32
        foreach ($results['buckets'] as $bucket) {
33
            if (1 == $bucket['doc_count']) {
34
                ++$docCount;
35
            } else {
36
                ++$nonDocCount;
37
            }
38
        }
39
        // 3 Documents that were added
40
        $this->assertEquals(3, $docCount);
41
        // 1 document that was generated in between for the missing hour
42
        $this->assertEquals(1, $nonDocCount);
43
    }
44
45
    /**
46
     * @group unit
47
     * @group legacy
48
     */
49
    public function testDateHistogramAggregationWithIntervalTriggersADeprecation(): void
50
    {
51
        $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.');
0 ignored issues
show
Unused Code introduced by
The call to DateHistogramTest::expectDeprecation() has too many arguments starting with 'Since ruflin/elastica 7...ill be removed in 8.0.'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
52
        new DateHistogram('hist', 'created', 'day');
53
    }
54
55
    /**
56
     * @group unit
57
     * @group legacy
58
     */
59
    public function testDateHistogramAggregationSetIntervalTriggersADeprecation(): void
60
    {
61
        $agg = new DateHistogram('hist', 'created');
62
63
        $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.');
0 ignored issues
show
Unused Code introduced by
The call to DateHistogramTest::expectDeprecation() has too many arguments starting with 'Since ruflin/elastica 7...ill be removed in 8.0.'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
64
65
        $agg->setInterval('day');
66
    }
67
68
    /**
69
     * @group functional
70
     */
71 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...
72
    {
73
        $this->_checkVersion('7.2');
74
75
        $agg = new DateHistogram('hist', 'created');
76
        $agg->setCalendarInterval('1h');
77
78
        $query = new Query();
79
        $query->addAggregation($agg);
80
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
81
82
        $docCount = 0;
83
        $nonDocCount = 0;
84
        foreach ($results['buckets'] as $bucket) {
85
            if (1 == $bucket['doc_count']) {
86
                ++$docCount;
87
            } else {
88
                ++$nonDocCount;
89
            }
90
        }
91
        // 3 Documents that were added
92
        $this->assertEquals(3, $docCount);
93
        // 1 document that was generated in between for the missing hour
94
        $this->assertEquals(1, $nonDocCount);
95
    }
96
97
    /**
98
     * @group unit
99
     */
100 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...
101
    {
102
        $agg = new DateHistogram('hist', 'created');
103
        $agg->setFixedInterval('1h');
104
105
        $agg->setOffset('3m');
106
107
        $expected = [
108
            'date_histogram' => [
109
                'field' => 'created',
110
                'offset' => '3m',
111
                'fixed_interval' => '1h',
112
            ],
113
        ];
114
115
        $this->assertEquals($expected, $agg->toArray());
116
117
        $this->assertInstanceOf(DateHistogram::class, $agg->setOffset('3m'));
118
    }
119
120
    /**
121
     * @group functional
122
     */
123 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...
124
    {
125
        $this->_checkVersion('7.2');
126
127
        $agg = new DateHistogram('hist', 'created');
128
        $agg->setFixedInterval('1m');
129
        $agg->setOffset('+40s');
130
131
        $query = new Query();
132
        $query->addAggregation($agg);
133
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
134
135
        $this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']);
136
    }
137
138
    /**
139
     * @group unit
140
     */
141 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...
142
    {
143
        $agg = new DateHistogram('hist', 'created');
144
        $agg->setFixedInterval('1h');
145
146
        $agg->setTimezone('-02:30');
147
148
        $expected = [
149
            'date_histogram' => [
150
                'field' => 'created',
151
                'time_zone' => '-02:30',
152
                'fixed_interval' => '1h',
153
            ],
154
        ];
155
156
        $this->assertEquals($expected, $agg->toArray());
157
158
        $this->assertInstanceOf(DateHistogram::class, $agg->setTimezone('-02:30'));
159
    }
160
161
    protected function _getIndexForTest(): Index
162
    {
163
        $index = $this->_createIndex();
164
        $index->setMapping(new Mapping([
165
            'created' => ['type' => 'date'],
166
        ]));
167
168
        $index->addDocuments([
169
            new Document(1, ['created' => '2014-01-29T00:20:00']),
170
            new Document(2, ['created' => '2014-01-29T02:20:00']),
171
            new Document(3, ['created' => '2014-01-29T03:20:00']),
172
        ]);
173
174
        $index->refresh();
175
176
        return $index;
177
    }
178
}
179