Completed
Pull Request — master (#1804)
by Pierre
02:49
created

CompositeTest::testCompositeWithSizeAggregation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 9.408
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\Composite;
6
use Elastica\Aggregation\Terms;
7
use Elastica\Document;
8
use Elastica\Index;
9
use Elastica\Query;
10
11
/**
12
 * @internal
13
 */
14
class CompositeTest extends BaseAggregationTest
15
{
16
    /**
17
     * @group unit
18
     */
19
    public function testSize(): void
20
    {
21
        $composite = new Composite('products');
22
        $composite->setSize(200);
23
        $this->assertEquals(200, $composite->getParam('size'));
24
25
        $expected = [
26
            'composite' => [
27
                'size' => 200,
28
            ],
29
        ];
30
        $this->assertEquals($expected, $composite->toArray());
31
    }
32
33
    /**
34
     * @group unit
35
     */
36
    public function testAddSource(): void
37
    {
38
        $expected = [
39
            'composite' => [
40
                'sources' => [
41
                    [
42
                        'product' => [
43
                            'terms' => [
44
                                'field' => 'product_id',
45
                            ],
46
                        ],
47
                    ],
48
                ],
49
            ],
50
        ];
51
52
        $composite = new Composite('products');
53
        $composite->addSource((new Terms('product'))->setField('product_id'));
54
        $this->assertEquals($expected, $composite->toArray());
55
    }
56
57
    /**
58
     * @group unit
59
     */
60
    public function testAddAfter(): void
61
    {
62
        $checkpoint = ['checkpointproduct' => 'checkpoint'];
63
        $expected = [
64
            'composite' => [
65
                'after' => $checkpoint,
66
            ],
67
        ];
68
69
        $composite = new Composite('products');
70
        $composite->addAfter($checkpoint);
71
        $this->assertEquals($expected, $composite->toArray());
72
    }
73
74
    /**
75
     * @group functional
76
     */
77
    public function testCompositeNoAfterAggregation(): void
78
    {
79
        $composite = new Composite('products');
80
        $composite->addSource((new Terms('color'))->setField('color.keyword'));
81
82
        $query = new Query();
83
        $query->addAggregation($composite);
84
85
        $results = $this->_getIndexForTest()->search($query)->getAggregation('products');
86
        $expected = [
87
            'after_key' => [
88
                'color' => 'red',
89
            ],
90
            'buckets' => [
91
                [
92
                    'key' => [
93
                        'color' => 'blue',
94
                    ],
95
                    'doc_count' => 2,
96
                ],
97
                [
98
                    'key' => [
99
                        'color' => 'green',
100
                    ],
101
                    'doc_count' => 1,
102
                ],
103
                [
104
                    'key' => [
105
                        'color' => 'red',
106
                    ],
107
                    'doc_count' => 1,
108
                ],
109
            ],
110
        ];
111
112
        $this->assertEquals($expected, $results);
113
    }
114
115
    /**
116
     * @group functional
117
     */
118
    public function testCompositeWithSizeAggregation(): void
119
    {
120
        $composite = new Composite('products');
121
        $composite->setSize(2);
122
        $composite->addSource((new Terms('color'))->setField('color.keyword'));
123
124
        $query = new Query();
125
        $query->addAggregation($composite);
126
127
        $results = $this->_getIndexForTest()->search($query)->getAggregation('products');
128
        $expected = [
129
            'after_key' => [
130
                'color' => 'green',
131
            ],
132
            'buckets' => [
133
                [
134
                    'key' => [
135
                        'color' => 'blue',
136
                    ],
137
                    'doc_count' => 2,
138
                ],
139
                [
140
                    'key' => [
141
                        'color' => 'green',
142
                    ],
143
                    'doc_count' => 1,
144
                ],
145
            ],
146
        ];
147
148
        $this->assertEquals($expected, $results);
149
    }
150
151
    /**
152
     * @group functional
153
     */
154
    public function testCompositeWithAfterAggregation(): void
155
    {
156
        $composite = new Composite('products');
157
        $composite->setSize(2);
158
        $composite->addSource((new Terms('color'))->setField('color.keyword'));
159
        $composite->addAfter(['color' => 'green']);
160
        $query = new Query();
161
        $query->addAggregation($composite);
162
163
        $results = $this->_getIndexForTest()->search($query)->getAggregation('products');
164
        $expected = [
165
            'after_key' => [
166
                'color' => 'red',
167
            ],
168
            'buckets' => [
169
                [
170
                    'key' => [
171
                        'color' => 'red',
172
                    ],
173
                    'doc_count' => 1,
174
                ],
175
            ],
176
        ];
177
178
        $this->assertEquals($expected, $results);
179
    }
180
181 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...
182
    {
183
        $index = $this->_createIndex();
184
185
        $index->addDocuments([
186
            new Document(1, ['price' => 5, 'color' => 'blue']),
187
            new Document(2, ['price' => 5, 'color' => 'blue']),
188
            new Document(3, ['price' => 3, 'color' => 'red']),
189
            new Document(4, ['price' => 3, 'color' => 'green']),
190
        ]);
191
192
        $index->refresh();
193
194
        return $index;
195
    }
196
}
197