Completed
Pull Request — master (#1876)
by romain
02:52
created

testDateHistogramKeyedAggregation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
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
    public function testDateHistogramAggregation(): void
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
    public function testDateHistogramKeyedAggregation(): void
46
    {
47
        $agg = new DateHistogram('hist', 'created', '1h');
48
        $agg->setKeyed();
49
50
        $query = new Query();
51
        $query->addAggregation($agg);
52
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
53
54
        $expected = [
55
            '2014-01-29T00:00:00Z',
56
            '2014-01-29T01:00:00Z',
57
            '2014-01-29T02:00:00Z',
58
            '2014-01-29T03:00:00Z',
59
        ];
60
        $this->assertSame($expected, array_keys($results['buckets']));
61
    }
62
63
    /**
64
     * @group unit
65
     */
66
    public function testSetOffset(): void
67
    {
68
        $agg = new DateHistogram('hist', 'created', '1h');
69
70
        $agg->setOffset('3m');
71
72
        $expected = [
73
            'date_histogram' => [
74
                'field' => 'created',
75
                'interval' => '1h',
76
                'offset' => '3m',
77
            ],
78
        ];
79
80
        $this->assertEquals($expected, $agg->toArray());
81
82
        $this->assertInstanceOf(DateHistogram::class, $agg->setOffset('3m'));
83
    }
84
85
    /**
86
     * @group functional
87
     */
88
    public function testSetOffsetWorks(): void
89
    {
90
        $this->_checkVersion('1.5');
91
92
        $agg = new DateHistogram('hist', 'created', '1m');
93
        $agg->setOffset('+40s');
94
95
        $query = new Query();
96
        $query->addAggregation($agg);
97
        $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
98
99
        $this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']);
100
    }
101
102
    /**
103
     * @group unit
104
     */
105
    public function testSetTimezone(): void
106
    {
107
        $agg = new DateHistogram('hist', 'created', '1h');
108
109
        $agg->setTimezone('-02:30');
110
111
        $expected = [
112
            'date_histogram' => [
113
                'field' => 'created',
114
                'interval' => '1h',
115
                'time_zone' => '-02:30',
116
            ],
117
        ];
118
119
        $this->assertEquals($expected, $agg->toArray());
120
121
        $this->assertInstanceOf(DateHistogram::class, $agg->setTimezone('-02:30'));
122
    }
123
124
    protected function _getIndexForTest(): Index
125
    {
126
        $index = $this->_createIndex();
127
        $index->setMapping(new Mapping([
128
            'created' => ['type' => 'date'],
129
        ]));
130
131
        $index->addDocuments([
132
            new Document(1, ['created' => '2014-01-29T00:20:00']),
133
            new Document(2, ['created' => '2014-01-29T02:20:00']),
134
            new Document(3, ['created' => '2014-01-29T03:20:00']),
135
        ]);
136
137
        $index->refresh();
138
139
        return $index;
140
    }
141
}
142