Completed
Push — master ( 0d45ed...ea3502 )
by Nicolas
02:38
created

tests/Aggregation/WeightedAvgTest.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Elastica\Test\Aggregation;
4
5
use Elastica\Aggregation\WeightedAvg;
6
use Elastica\Document;
7
use Elastica\Exception\InvalidException;
8
use Elastica\Index;
9
use Elastica\Query;
10
11
/**
12
 * @internal
13
 */
14
class WeightedAvgTest extends BaseAggregationTest
15
{
16
    /**
17
     * @group functional
18
     */
19
    public function testWeightedAvgAggregation(): void
20
    {
21
        $this->_checkVersion('6.4');
22
23
        $agg = new WeightedAvg('weighted');
24
        $agg->setValue('price');
25
        $weightWhenMissing = 2;
26
        $agg->setWeight('weight', $weightWhenMissing);
27
28
        $query = new Query();
29
        $query->addAggregation($agg);
30
31
        $resultSet = $this->_getIndexForTest()->search($query);
32
        $results = $resultSet->getAggregations();
33
34
        $this->assertTrue($resultSet->hasAggregations());
35
        $this->assertEquals((5 * 3 + 8 + 1 + 3 * $weightWhenMissing) / 7.0, $results['weighted']['value']);
36
    }
37
38
    /**
39
     * @group unit
40
     */
41 View Code Duplication
    public function testItsNotPossibleToMixValueFieldAndScript(): void
0 ignored issues
show
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...
42
    {
43
        $agg = new WeightedAvg('weighted');
44
        $agg->setValue('price');
45
46
        $this->expectExceptionObject(new InvalidException('Weighted Average aggregation with a value mixing field and script is not possible.'));
47
        $agg->setValueScript('doc.price.value + 1');
48
    }
49
50
    /**
51
     * @group unit
52
     */
53 View Code Duplication
    public function testItsNotPossibleToMixValueScriptAndField(): void
0 ignored issues
show
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...
54
    {
55
        $agg = new WeightedAvg('weighted');
56
        $agg->setValueScript('doc.price.value + 1');
57
58
        $this->expectExceptionObject(new InvalidException('Weighted Average aggregation with a value mixing field and script is not possible.'));
59
        $agg->setValue('price');
60
    }
61
62
    /**
63
     * @group unit
64
     */
65 View Code Duplication
    public function testItsNotPossibleToMixWeightFieldAndScript(): void
0 ignored issues
show
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...
66
    {
67
        $agg = new WeightedAvg('weighted');
68
        $agg->setWeight('weight');
69
70
        $this->expectExceptionObject(new InvalidException('Weighted Average aggregation with a weight mixing field and script is not possible.'));
71
        $agg->setWeightScript('doc.weight.value + 1');
72
    }
73
74
    /**
75
     * @group unit
76
     */
77 View Code Duplication
    public function testItsNotPossibleToMixWeightScriptAndField(): void
0 ignored issues
show
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...
78
    {
79
        $agg = new WeightedAvg('weighted');
80
        $agg->setWeightScript('doc.weight.value + 1');
81
82
        $this->expectExceptionObject(new InvalidException('Weighted Average aggregation with a weight mixing field and script is not possible.'));
83
        $agg->setWeight('weight');
84
    }
85
86 View Code Duplication
    protected function _getIndexForTest(): Index
0 ignored issues
show
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...
87
    {
88
        $index = $this->_createIndex();
89
        $index->addDocuments([
90
            new Document(1, ['price' => 5, 'weight' => 3]),
91
            new Document(2, ['price' => 8, 'weight' => 1]),
92
            new Document(3, ['price' => 1, 'weight' => 1]),
93
            new Document(4, ['price' => 3]),
94
        ]);
95
96
        $index->refresh();
97
98
        return $index;
99
    }
100
}
101