Completed
Pull Request — master (#1875)
by romain
02:35
created

testDateRangeAggregationWithMissing()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
c 0
b 0
f 0
rs 9.6333
cc 4
nc 4
nop 0
1
<?php
2
3
namespace Elastica\Test\Aggregation;
4
5
use Elastica\Aggregation\DateRange;
6
use Elastica\Document;
7
use Elastica\Exception\ResponseException;
8
use Elastica\Index;
9
use Elastica\Mapping;
10
use Elastica\Query;
11
12
/**
13
 * @internal
14
 */
15
class DateRangeTest extends BaseAggregationTest
16
{
17
    /**
18
     * @group unit
19
     */
20 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...
21
    {
22
        $agg = (new DateRange('name'))
23
            ->setField('date')
24
            ->setTimezone('CET')
25
        ;
26
27
        $expected = [
28
            'date_range' => [
29
                'field' => 'date',
30
                'time_zone' => 'CET',
31
            ],
32
        ];
33
34
        $this->assertEquals($expected, $agg->toArray());
35
    }
36
37
    /**
38
     * @group functional
39
     */
40 View Code Duplication
    public function testDateRangeAggregation(): 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...
41
    {
42
        $agg = new DateRange('date');
43
        $agg->setField('created');
44
        $agg->addRange(1390958535000)->addRange(null, 1390958535000);
45
46
        $query = new Query();
47
        $query->addAggregation($agg);
48
        $results = $this->_getIndexForTest()->search($query)->getAggregation('date');
49
50
        foreach ($results['buckets'] as $bucket) {
51
            if (\array_key_exists('to', $bucket)) {
52
                $this->assertEquals(1, $bucket['doc_count']);
53
            } elseif (\array_key_exists('from', $bucket)) {
54
                $this->assertEquals(2, $bucket['doc_count']);
55
            }
56
        }
57
    }
58
59
    /**
60
     * @group functional
61
     */
62 View Code Duplication
    public function testDateRangeAggregationWithMissing(): 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...
63
    {
64
        $agg = new DateRange('date');
65
        $agg->setField('created');
66
        $agg->addRange(1390958535000)->addRange(null, 1390958535000);
67
        $agg->setMissing(1390958534000);
68
69
        $query = new Query();
70
        $query->addAggregation($agg);
71
        $results = $this->_getIndexForTest()->search($query)->getAggregation('date');
72
73
        foreach ($results['buckets'] as $bucket) {
74
            if (\array_key_exists('to', $bucket)) {
75
                $this->assertEquals(2, $bucket['doc_count']);
76
            } elseif (\array_key_exists('from', $bucket)) {
77
                $this->assertEquals(2, $bucket['doc_count']);
78
            }
79
        }
80
    }
81
82
    /**
83
     * @group functional
84
     */
85 View Code Duplication
    public function testDateRangeKeyedAggregation(): 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...
86
    {
87
        $agg = new DateRange('date');
88
        $agg->setField('created');
89
        $agg->setKeyed();
90
        $agg->addRange(1390958535000)->addRange(null, 1390958535000);
91
92
        $query = new Query();
93
        $query->addAggregation($agg);
94
        $results = $this->_getIndexForTest()->search($query)->getAggregation('date');
95
96
        $expected = [
97
            '*-1390958535000',
98
            '1390958535000-*',
99
        ];
100
        $this->assertSame($expected, \array_keys($results['buckets']));
101
    }
102
103
    /**
104
     * @group functional
105
     */
106
    public function testDateRangeSetFormat(): void
107
    {
108
        $agg = new DateRange('date');
109
        $agg->setField('created');
110
        $agg->addRange(1390958535000)->addRange(null, 1390958535000);
111
        $agg->setFormat('epoch_millis');
112
113
        $query = new Query();
114
        $query->addAggregation($agg);
115
        $results = $this->_getIndexForTest()->search($query)->getAggregation('date');
116
117
        $this->assertEquals('1390958535000', $results['buckets'][0]['to_as_string']);
118
    }
119
120
    /**
121
     * @group functional
122
     */
123
    public function testDateRangeSetFormatAccordingToFormatTargetField(): void
124
    {
125
        $agg = new DateRange('date');
126
        $agg->setField('created');
127
        $agg->addRange(1390958535000)->addRange(null, 1390958535000);
128
        $agg->setFormat('m-d-y');
129
130
        $query = new Query();
131
        $query->addAggregation($agg);
132
133
        try {
134
            $results = $this->_getIndexForTest()->search($query)->getAggregation('date');
0 ignored issues
show
Unused Code introduced by
$results is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
135
            $this->fail('Should throw exception to and from parameters in date_range aggregation are interpreted according of the target field');
136
        } catch (ResponseException $e) {
137
            $error = $e->getResponse()->getFullError();
138
139
            $this->assertSame('search_phase_execution_exception', $error['type']);
140
            $this->assertStringStartsWith('failed to parse date field', $error['root_cause'][0]['reason']);
141
        }
142
    }
143
144 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...
145
    {
146
        $index = $this->_createIndex();
147
        $index->setMapping(new Mapping([
148
            'created' => ['type' => 'date', 'format' => 'epoch_millis'],
149
        ]));
150
151
        $index->addDocuments([
152
            new Document(1, ['created' => 1390962135000]),
153
            new Document(2, ['created' => 1390965735000]),
154
            new Document(3, ['created' => 1390954935000]),
155
            new Document(4, ['anything' => 'anything']),
156
        ]);
157
158
        $index->refresh();
159
160
        return $index;
161
    }
162
}
163