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