Completed
Push — master ( 9146c8...39ecb4 )
by Nicolas
04:52
created

ScriptTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 80
Duplicated Lines 28.75 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 23
loc 80
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testAggregationScript() 12 12 1
A testAggregationScriptAsString() 11 11 1
A testSetScript() 0 27 1
A _getIndexForTest() 0 15 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\Sum;
6
use Elastica\Document;
7
use Elastica\Index;
8
use Elastica\Query;
9
use Elastica\Script\Script;
10
11
/**
12
 * @internal
13
 */
14
class ScriptTest extends BaseAggregationTest
15
{
16
    /**
17
     * @group functional
18
     */
19 View Code Duplication
    public function testAggregationScript(): 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 Sum('sum');
22
        $script = new Script("return doc['price'].value", null, Script::LANG_PAINLESS);
23
        $agg->setScript($script);
24
25
        $query = new Query();
26
        $query->addAggregation($agg);
27
        $results = $this->_getIndexForTest()->search($query)->getAggregation('sum');
28
29
        $this->assertEquals(5 + 8 + 1 + 3, $results['value']);
30
    }
31
32
    /**
33
     * @group functional
34
     */
35 View Code Duplication
    public function testAggregationScriptAsString(): 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...
36
    {
37
        $agg = new Sum('sum');
38
        $agg->setScript(new Script("doc['price'].value", null, Script::LANG_PAINLESS));
39
40
        $query = new Query();
41
        $query->addAggregation($agg);
42
        $results = $this->_getIndexForTest()->search($query)->getAggregation('sum');
43
44
        $this->assertEquals(5 + 8 + 1 + 3, $results['value']);
45
    }
46
47
    /**
48
     * @group unit
49
     */
50
    public function testSetScript(): void
51
    {
52
        $aggregation = 'sum';
53
        $string = "doc['price'].value";
54
        $params = [
55
            'param1' => 'one',
56
            'param2' => 1,
57
        ];
58
        $lang = Script::LANG_PAINLESS;
59
60
        $agg = new Sum($aggregation);
61
        $script = new Script($string, $params, $lang);
62
        $agg->setScript($script);
63
64
        $array = $agg->toArray();
65
66
        $expected = [
67
            $aggregation => [
68
                'script' => [
69
                    'source' => $string,
70
                    'params' => $params,
71
                    'lang' => $lang,
72
                ],
73
            ],
74
        ];
75
        $this->assertEquals($expected, $array);
76
    }
77
78
    protected function _getIndexForTest(): Index
79
    {
80
        $index = $this->_createIndex();
81
82
        $index->addDocuments([
83
            new Document('1', ['price' => 5]),
84
            new Document('2', ['price' => 8]),
85
            new Document('3', ['price' => 1]),
86
            new Document('4', ['price' => 3]),
87
        ]);
88
89
        $index->refresh();
90
91
        return $index;
92
    }
93
}
94