Completed
Pull Request — master (#1847)
by
unknown
03:08
created

DateRangeTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 23.53 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 9
dl 24
loc 102
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetTimezone() 0 15 1
A testDateRangeAggregation() 7 18 4
A testDateRangeSetFormat() 0 13 1
A testDateRangeSetFormatAccordingToFormatTargetField() 0 20 2
A _getIndexForTest() 17 17 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function testSetTimezone(): void
21
    {
22
        $agg = (new DateRange('date'))
23
            ->setTimezone('CET')
24
        ;
25
26
        $expected = [
27
            'date_range' => [
28
                'field' => 'date',
29
                'time_zone' => 'CET',
30
            ],
31
        ];
32
33
        $this->assertEquals($expected, $agg->toArray());
34
    }
35
36
    /**
37
     * @group functional
38
     */
39
    public function testDateRangeAggregation(): void
40
    {
41
        $agg = new DateRange('date');
42
        $agg->setField('created');
43
        $agg->addRange(1390958535000)->addRange(null, 1390958535000);
44
45
        $query = new Query();
46
        $query->addAggregation($agg);
47
        $results = $this->_getIndexForTest()->search($query)->getAggregation('date');
48
49 View Code Duplication
        foreach ($results['buckets'] as $bucket) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
50
            if (\array_key_exists('to', $bucket)) {
51
                $this->assertEquals(1, $bucket['doc_count']);
52
            } elseif (\array_key_exists('from', $bucket)) {
53
                $this->assertEquals(2, $bucket['doc_count']);
54
            }
55
        }
56
    }
57
58
    /**
59
     * @group functional
60
     */
61
    public function testDateRangeSetFormat(): void
62
    {
63
        $agg = new DateRange('date');
64
        $agg->setField('created');
65
        $agg->addRange(1390958535000)->addRange(null, 1390958535000);
66
        $agg->setFormat('epoch_millis');
67
68
        $query = new Query();
69
        $query->addAggregation($agg);
70
        $results = $this->_getIndexForTest()->search($query)->getAggregation('date');
71
72
        $this->assertEquals('1390958535000', $results['buckets'][0]['to_as_string']);
73
    }
74
75
    /**
76
     * @group functional
77
     */
78
    public function testDateRangeSetFormatAccordingToFormatTargetField(): void
79
    {
80
        $agg = new DateRange('date');
81
        $agg->setField('created');
82
        $agg->addRange(1390958535000)->addRange(null, 1390958535000);
83
        $agg->setFormat('m-d-y');
84
85
        $query = new Query();
86
        $query->addAggregation($agg);
87
88
        try {
89
            $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...
90
            $this->fail('Should throw exception to and from parameters in date_range aggregation are interpreted according of the target field');
91
        } catch (ResponseException $e) {
92
            $error = $e->getResponse()->getFullError();
93
94
            $this->assertSame('search_phase_execution_exception', $error['type']);
95
            $this->assertStringStartsWith('failed to parse date field', $error['root_cause'][0]['reason']);
96
        }
97
    }
98
99 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...
100
    {
101
        $index = $this->_createIndex();
102
        $index->setMapping(new Mapping([
103
            'created' => ['type' => 'date', 'format' => 'epoch_millis'],
104
        ]));
105
106
        $index->addDocuments([
107
            new Document(1, ['created' => 1390962135000]),
108
            new Document(2, ['created' => 1390965735000]),
109
            new Document(3, ['created' => 1390954935000]),
110
        ]);
111
112
        $index->refresh();
113
114
        return $index;
115
    }
116
}
117