Completed
Pull Request — master (#1875)
by romain
09:47
created

DateHistogramTest::_getIndexForTest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

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