9
9
10
10
use Magento \Elasticsearch \Model \Adapter \FieldMapper \Product \AttributeAdapter ;
11
11
use Magento \Elasticsearch \Model \Adapter \FieldMapper \Product \AttributeProvider ;
12
- use Magento \Elasticsearch \Model \Adapter \FieldMapper \Product \FieldProvider \FieldName \ResolverInterface
13
- as FieldNameResolver ;
14
12
use Magento \Elasticsearch \SearchAdapter \Query \Builder \Sort ;
15
- use Magento \Framework \ Search \ RequestInterface ;
16
- use Magento \Framework \TestFramework \ Unit \ Helper \ ObjectManager ;
13
+ use Magento \Elasticsearch \ SearchAdapter \ Query \ Builder \ Sort \ ExpressionBuilderInterface as SortExpressionBuilder ;
14
+ use Magento \Framework \Search \ Request ;
17
15
use PHPUnit \Framework \MockObject \MockObject ;
18
16
use PHPUnit \Framework \TestCase ;
19
17
20
18
class SortTest extends TestCase
21
19
{
22
20
/**
23
- * @var AttributeProvider
21
+ * @var AttributeProvider|MockObject
24
22
*/
25
- private $ attributeAdapterProvider ;
23
+ private $ attributeAdapterProviderMock ;
26
24
27
25
/**
28
- * @var FieldNameResolver
26
+ * @var SortExpressionBuilder|MockObject
29
27
*/
30
- private $ fieldNameResolver ;
28
+ private $ sortExpressionBuilderMock ;
31
29
32
30
/**
33
31
* @var Sort
@@ -39,232 +37,40 @@ class SortTest extends TestCase
39
37
*/
40
38
protected function setUp (): void
41
39
{
42
- $ this ->attributeAdapterProvider = $ this ->getMockBuilder (AttributeProvider::class)
43
- ->disableOriginalConstructor ()
44
- ->onlyMethods (['getByAttributeCode ' ])
45
- ->getMock ();
46
- $ this ->fieldNameResolver = $ this ->getMockBuilder (FieldNameResolver::class)
47
- ->disableOriginalConstructor ()
48
- ->onlyMethods (['getFieldName ' ])
49
- ->getMock ();
50
-
51
- $ this ->sortBuilder = (new ObjectManager ($ this ))->getObject (
52
- Sort::class,
53
- [
54
- 'attributeAdapterProvider ' => $ this ->attributeAdapterProvider ,
55
- 'fieldNameResolver ' => $ this ->fieldNameResolver ,
56
- ]
57
- );
40
+ $ this ->attributeAdapterProviderMock = $ this ->createMock (AttributeProvider::class);
41
+ $ this ->sortExpressionBuilderMock = $ this ->createMock (SortExpressionBuilder::class);
42
+ $ this ->sortBuilder = new Sort ($ this ->attributeAdapterProviderMock , $ this ->sortExpressionBuilderMock );
58
43
}
59
44
60
45
/**
61
- * @SuppressWarnings(PHPMD.UnusedLocalVariable)
62
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
63
- * @dataProvider getSortProvider
64
- * @param array $sortItems
65
- * @param $isSortable
66
- * @param $isFloatType
67
- * @param $isIntegerType
68
- * @param $fieldName
69
- * @param array $expected
46
+ * @return void
70
47
*/
71
- public function testGetSort (
72
- array $ sortItems ,
73
- $ isSortable ,
74
- $ isFloatType ,
75
- $ isIntegerType ,
76
- $ isComplexType ,
77
- $ fieldName ,
78
- array $ expected
79
- ) {
80
- /** @var MockObject|RequestInterface $request */
81
- $ request = $ this ->getMockBuilder (RequestInterface::class)
82
- ->disableOriginalConstructor ()
83
- ->addMethods (['getSort ' ])
84
- ->getMockForAbstractClass ();
85
- $ request ->expects ($ this ->any ())
48
+ public function testGetSort (): void
49
+ {
50
+ $ request = $ this ->createMock (Request::class);
51
+ $ sortItems = [
52
+ [
53
+ 'field ' => 'some_attribute ' ,
54
+ 'direction ' => 'DESC ' ,
55
+ ]
56
+ ];
57
+ $ request ->expects (self ::once ())
86
58
->method ('getSort ' )
87
59
->willReturn ($ sortItems );
88
- $ attributeMock = $ this ->getMockBuilder (AttributeAdapter::class)
89
- ->disableOriginalConstructor ()
90
- ->onlyMethods (['isSortable ' , 'isFloatType ' , 'isIntegerType ' , 'isComplexType ' ])
91
- ->getMock ();
92
- $ attributeMock ->expects ($ this ->any ())
93
- ->method ('isSortable ' )
94
- ->willReturn ($ isSortable );
95
- $ attributeMock ->expects ($ this ->any ())
96
- ->method ('isFloatType ' )
97
- ->willReturn ($ isFloatType );
98
- $ attributeMock ->expects ($ this ->any ())
99
- ->method ('isIntegerType ' )
100
- ->willReturn ($ isIntegerType );
101
- $ attributeMock ->expects ($ this ->any ())
102
- ->method ('isComplexType ' )
103
- ->willReturn ($ isComplexType );
104
- $ this ->attributeAdapterProvider ->expects ($ this ->any ())
60
+ $ attributeAdapterMock = $ this ->createMock (AttributeAdapter::class);
61
+ $ this ->attributeAdapterProviderMock ->expects (self ::once ())
105
62
->method ('getByAttributeCode ' )
106
- ->with ($ this ->anything ())
107
- ->willReturn ($ attributeMock );
108
- $ this ->fieldNameResolver ->expects ($ this ->any ())
109
- ->method ('getFieldName ' )
110
- ->with ($ this ->anything ())
111
- ->willReturnCallback (
112
- function ($ attribute , $ context ) use ($ fieldName ) {
113
- if (empty ($ context )) {
114
- return $ fieldName ;
115
- } elseif ($ context ['type ' ] === 'sort ' ) {
116
- return 'sort_ ' . $ fieldName ;
117
- }
118
- }
119
- );
63
+ ->with ('some_attribute ' )
64
+ ->willReturn ($ attributeAdapterMock );
65
+ $ sortExpression = ['some_attribute ' => ['order ' => 'desc ' ]];
66
+ $ this ->sortExpressionBuilderMock ->expects (self ::once ())
67
+ ->method ('build ' )
68
+ ->with ($ attributeAdapterMock , 'desc ' , $ request )
69
+ ->willReturn ($ sortExpression );
120
70
121
- $ this -> assertEquals (
122
- $ expected ,
71
+ self :: assertEquals (
72
+ [ $ sortExpression ] ,
123
73
$ this ->sortBuilder ->getSort ($ request )
124
74
);
125
75
}
126
-
127
- /**
128
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
129
- * @return array
130
- */
131
- public function getSortProvider ()
132
- {
133
- return [
134
- [
135
- [
136
- [
137
- 'field ' => 'entity_id ' ,
138
- 'direction ' => 'DESC '
139
- ]
140
- ],
141
- false ,
142
- false ,
143
- false ,
144
- false ,
145
- null ,
146
- []
147
- ],
148
- [
149
- [
150
- [
151
- 'field ' => 'entity_id ' ,
152
- 'direction ' => 'DESC '
153
- ],
154
- [
155
- 'field ' => 'price ' ,
156
- 'direction ' => 'DESC '
157
- ],
158
- ],
159
- false ,
160
- false ,
161
- false ,
162
- false ,
163
- 'price ' ,
164
- [
165
- [
166
- 'price ' => [
167
- 'order ' => 'desc '
168
- ]
169
- ]
170
- ]
171
- ],
172
- [
173
- [
174
- [
175
- 'field ' => 'entity_id ' ,
176
- 'direction ' => 'DESC '
177
- ],
178
- [
179
- 'field ' => 'price ' ,
180
- 'direction ' => 'DESC '
181
- ],
182
- ],
183
- true ,
184
- true ,
185
- true ,
186
- false ,
187
- 'price ' ,
188
- [
189
- [
190
- 'price ' => [
191
- 'order ' => 'desc '
192
- ]
193
- ]
194
- ]
195
- ],
196
- [
197
- [
198
- [
199
- 'field ' => 'entity_id ' ,
200
- 'direction ' => 'DESC '
201
- ],
202
- [
203
- 'field ' => 'name ' ,
204
- 'direction ' => 'DESC '
205
- ],
206
- ],
207
- true ,
208
- false ,
209
- false ,
210
- false ,
211
- 'name ' ,
212
- [
213
- [
214
- 'name.sort_name ' => [
215
- 'order ' => 'desc '
216
- ]
217
- ]
218
- ]
219
- ],
220
- [
221
- [
222
- [
223
- 'field ' => 'entity_id ' ,
224
- 'direction ' => 'DESC '
225
- ],
226
- [
227
- 'field ' => 'not_eav_attribute ' ,
228
- 'direction ' => 'DESC '
229
- ],
230
- ],
231
- false ,
232
- false ,
233
- false ,
234
- false ,
235
- 'not_eav_attribute ' ,
236
- [
237
- [
238
- 'not_eav_attribute ' => [
239
- 'order ' => 'desc '
240
- ]
241
- ]
242
- ]
243
- ],
244
- [
245
- [
246
- [
247
- 'field ' => 'entity_id ' ,
248
- 'direction ' => 'DESC '
249
- ],
250
- [
251
- 'field ' => 'color ' ,
252
- 'direction ' => 'DESC '
253
- ],
254
- ],
255
- true ,
256
- false ,
257
- false ,
258
- true ,
259
- 'color ' ,
260
- [
261
- [
262
- 'color_value.sort_color ' => [
263
- 'order ' => 'desc '
264
- ]
265
- ]
266
- ]
267
- ]
268
- ];
269
- }
270
76
}
0 commit comments